1 #include<cstdio>
2 #include<cstdlib>
3 #include<cmath>
4 #include<cstring>
5 #include<algorithm>
6 #include<iostream>
7 #include<vector>
8 #include<map>
9 #include<set>
10 #include<queue>
11 #include<string>
12 #define inf 1000000000
13 #define maxn 1000000+100
14 #define maxm 500+100
15 #define eps 1e-10
16 #define ll long long
17 #define pa pair<int,int>
18 #define for0(i,n) for(int i=0;i<=(n);i++)
19 #define for1(i,n) for(int i=1;i<=(n);i++)
20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
22 #define mod 1000000007
23 using namespace std;
24 inline int read()
25 {
26 int x=0,f=1;char ch=getchar();
27 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
28 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}
29 return x*f;
30 }
31 int n,rt,ban,ans,a[maxn],fa[maxn],head[maxn],f[maxn],g[maxn];
32 bool v[maxn];
33 struct edge{int go,next;}e[maxn];
34 inline void dfs(int x)
35 {
36 v[x]=1;
37 if(v[a[x]])rt=x;else dfs(a[x]);
38 }
39 inline void dp(int x)
40 {
41 f[x]=1;g[x]=inf;v[x]=1;
42 if(x==ban)g[x]=0;
43 for(int i=head[x];i;i=e[i].next)
44 if(i!=rt&&e[i].go!=fa[x])
45 {
46 int y=e[i].go;
47 fa[y]=x;
48 dp(y);
49 g[x]+=min(f[y],g[y]);
50 g[x]=min(g[x],f[x]+f[y]-1);//这里巧妙的避免了使用临时变量来存储f[y]与g[y]的最小差值
51 f[x]+=min(f[y],g[y]);
52 }
53 }
54 int main()
55 {
56 freopen("input.txt","r",stdin);
57 freopen("output.txt","w",stdout);
58 n=read();int x;
59 for1(i,n)e[i].go=i,a[i]=x=read(),e[i].next=head[x],head[x]=i;
60 for1(i,n)
61 if(!v[i])
62 {
63 dfs(i);//找出这棵基环树
64 ban=a[rt];//选取rt作为x,a[rt]作为y,g[y]=0
65 dp(rt);
66 int tmp=f[rt];//限定rt必须选
67 ban=0;
68 dp(rt);
69 tmp=min(tmp,g[rt]);//不用选rt
70 ans+=tmp;
71 }
72 printf("%d\n",n-ans);
73 return 0;
74 }