标签:detail mes sum init else ret ++ name std
zz:https://blog.csdn.net/scut_pein/article/details/8660719?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task
#include <iostream> #include <cstdio> using namespace std; #define maxn 200018 int father[maxn],idx[maxn],num[maxn];//num来多少个,idx才存 long long int sum[maxn]; int n,m,cnt; int find(int x) { if(x==father[x])return x; return find(father[x]); } void init() { for(int i=1;i<=n;i++) { father[i]=idx[i]=sum[i]=i; num[i]=1; } cnt=n; } void Union(int p,int q) { int pp=find(idx[p]),qq=find(idx[q]); father[pp]=qq; num[qq]+=num[pp]; sum[qq]+=sum[pp]; } void Delete(int p) { int pp=idx[p]; sum[find(pp)]-=p; num[find(pp)]--; idx[p]=++cnt;sum[idx[p]]=p;num[idx[p]]=1;father[idx[p]]=idx[p]; } int main() { while(scanf("%d%d",&n,&m)==2) { init(); int ope,p,q; for(int i=1;i<=m;i++) { scanf("%d",&ope); if(ope==1) { scanf("%d%d",&p,&q); if(find(idx[p])==find(idx[q]))continue; else Union(p,q); } else if(ope==2) { scanf("%d%d",&p,&q); if(find(idx[p])!=find(idx[q])) { Delete(p); Union(p,q); } } else { int u; scanf("%d",&u); int fuck=find(idx[u]); printf("%d %lld\n",num[fuck],sum[fuck]);//这里我用I64d既然WA。。。 } } } return 0; } ———————————————— 版权声明:本文为CSDN博主「SCUT_Pein」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/scut_pein/article/details/8660719
标签:detail mes sum init else ret ++ name std
原文地址:https://www.cnblogs.com/cutemush/p/12466364.html