P3420 [POI2005]SKA-Piggy Banks
P3420 [POI2005]SKA-Piggy Banks
套路题,a通过某种关系和其他的点建立关系。这种题不是环(dfs就可以了),就是并查集找连通块。这种题要建图,画图,就很清楚了。
1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<algorithm> 5 #include<cmath> 6 #include<ctime> 7 #include<cstring> 8 #define inf 2147483647 9 #define For(i,a,b) for(register long long i=a;i<=b;i++) 10 #define p(a) putchar(a) 11 #define g() getchar() 12 //by war 13 //2017.10.23 14 using namespace std; 15 long long n; 16 long long d[1000010]; 17 long long x; 18 long long ans; 19 void in(long long &x) 20 { 21 long long y=1; 22 char c=g();x=0; 23 while(c<'0'||c>'9') 24 { 25 if(c=='-') 26 y=-1; 27 c=g(); 28 } 29 while(c<='9'&&c>='0')x=x*10+c-'0',c=g(); 30 x*=y; 31 } 32 void o(long long x) 33 { 34 if(x<0) 35 { 36 p('-'); 37 x=-x; 38 } 39 if(x>9)o(x/10); 40 p(x%10+'0'); 41 } 42 43 long long find(long long x) 44 { 45 if(d[x]==x)return x; 46 d[x]=find(d[x]); 47 return d[x]; 48 } 49 50 int main() 51 { 52 in(n); 53 For(i,1,n) 54 d[i]=i; 55 For(i,1,n) 56 { 57 in(x); 58 d[find(i)]=find(x); 59 } 60 For(i,1,n) 61 { 62 if(d[i]==i) 63 ans++; 64 } 65 o(ans); 66 return 0; 67 }