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 }

 

转载于:https://www.cnblogs.com/OIerLYF/p/7496012.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值