intfindSet(int fast, int slow, bool moveSlow = false, int step = 0){ if (fast == a[fast]) { isLoop = 0; return fast; } else { fast = a[fast]; if (moveSlow) { slow = a[slow]; } else { if (step && fast == slow) { // loop isLoop = 1; return fast; } } return findSet(fast, slow, !moveSlow, step + 1); } }
voidmergeSet(int x){ if (x == a[x]) { return; } int t = a[x]; a[x] = x; mergeSet(t); }
intmain(){ // std::ios::sync_with_stdio(false); // std::cin.tie(0); int n; cin >> n; int ans = 0; for (int i = 1; i <= n; i++) { //cin >> a[i]; scanf("%d", a + i); if (a[i] == i) { ans += 1; } } for (int i = 1; i <= n; i++) { if (a[i] == i) { continue; } findSet(i, i); if (isLoop) { // new loop ans += 1; } // compress mergeSet(i); } cout << ans; return0; }