fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int MAX_SIZE = 1000;
  5. int main() {
  6. int n, v[MAX_SIZE + 1];
  7. cin >> n;
  8. int zone = 0;
  9. int counter = 0;
  10. for (int i = 1; i <= n; ++i) {
  11. cin >> v[i];
  12. }
  13. int destination = 1;
  14. for(int i = 1, j = 1; destination <= n && v[i] != 0; ++destination ) {
  15.  
  16. int flag = 1;
  17.  
  18. if(v[j] != i ) {
  19.  
  20. j = v[j];
  21. flag = 0;
  22.  
  23. }
  24.  
  25.  
  26. if(v[j] == i) {
  27. cout<< j << " "<< v[j] <<" \n";
  28. ++zone;
  29. destination = 1;
  30. j = 1;
  31. ++i;
  32. v[i] = 0;
  33.  
  34. }
  35.  
  36. //
  37. }
  38. cout << zone ;
  39. return 0;
  40. }
Success #stdin #stdout 0s 5300KB
stdin
11
11 1 7 8 10 3 9 2 5 6 4 
stdout
2 1 
1