图论
借用图片 https://www.acwing.com/solution/content/7917/
1
2
3
4
import java.util.*;
public class Main {
static final int N = 10010;
static int[] b = new int[N];
static boolean[] st = new boolean[N];
static int n;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for (int i = 1; i <= n; i++) b[i] = sc.nextInt();
int cnt = 0;
//类似邻接表的实现
for (int i = 1; i <= n; i++)
if (!st[i]) {
cnt++;
for (int j = i; !st[j]; j = b[j])
st[j] = true;
}
System.out.println(n - cnt);
}
}