标签:class include style find pig 2-2 ret png int
1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 5 #define up(a,b,c) for(register int c=a;c<=b;c++) 6 7 int n,fa[1000005],x,tot; 8 9 inline int Read(){ 10 int x = 0,f = 1; 11 char ch = getchar(); 12 while(ch < ‘0‘ || ch >‘9‘){ 13 if(ch==‘-‘) f = -1; 14 ch = getchar(); 15 } 16 while(ch >= ‘0‘ && ch <= ‘9‘){ 17 x = x*10 + ch - ‘0‘; 18 ch = getchar(); 19 } 20 return x*f; 21 } 22 23 int find(int x){ 24 return x == fa[x] ? x : fa[x] = find(fa[x]); 25 } 26 27 void un(int x,int y){ 28 x = find(x); 29 y = find(y); 30 if(x!=y) fa[x]=y; 31 } 32 33 int main(){ 34 n = Read(); 35 up(1,n,i) fa[i]=i; 36 up(1,n,i){ 37 x = Read(); 38 un(x,i); 39 } 40 up(1,n,i) tot+=(fa[i]==i); 41 printf("%d\n",tot); 42 return 0; 43 }
标签:class include style find pig 2-2 ret png int
原文地址:http://www.cnblogs.com/OIerLYF/p/7496012.html