BZOJ 1529 ska piggy banks

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了BZOJ 1529 ska piggy banks相关的知识,希望对你有一定的参考价值。

技术分享

 

 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 }

 

以上是关于BZOJ 1529 ska piggy banks的主要内容,如果未能解决你的问题,请参考以下文章

bzoj1529: [POI2005]ska Piggy banks

BZOJ1529 [POI2005]ska Piggy banks

Bzoj1529/POI2005 ska Piggy banks

BZOJ 1529 [POI2005]ska Piggy banks(并查集)

BZOJ 1529: [POI2005]ska Piggy banks [并查集]

luogu P3420 [POI2005]SKA-Piggy Banks