#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
using namespace std;
int arr[100010];
map<int, int> visited;
int dfs(int _index) {
int count = 0, index = _index;
while (visited[index] == 0) {
visited[index] = 1;
count += 1;
index = arr[index];
}
return count - 1;
}
int swap_count(int n) {
int count = 0;
for(int i = 1; i <= n; i++) {
if (visited[i] == 1) {
continue;
}
count += dfs(i);
}
return count;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &arr[i]);
}
printf("%d\n", swap_count(n));
return 0;
}
Arpit's Newsletter read by 15000+ engineers
🔥 Thrice a week, in your inbox, an essay about system design, distributed systems, microservices, programming languages internals, or a deep dive on some super-clever algorithm, or just a few tips on building highly scalable distributed systems.