Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #include <string>
- using namespace std;
- int main()
- {
- int n;
- cin >> n;
- vector<int> v(n);
- for(int i = 0; i < n; i++) {
- cin >> v[i];
- v[i]--;
- }
- vector<bool> b(n);
- vector<string> s;
- int ind = 0;
- for(int i = 0; i < n; i++) {
- if(b[i] == false) {
- int j = i;
- b[j] = true;
- string str(to_string(j + 1) + " -> " + to_string(v[j] + 1));
- while(v[j] != i){
- j = v[j];
- b[j] = true;
- if(v[j] != i) str += " -> " + to_string(v[j] + 1);
- }
- s.push_back(str);
- ind++;
- }
- }
- cout << ind << endl;
- for(int i = 0; i < ind; i++) {
- cout << s[i] << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement