标签:
/* Author:2486 Memory: 952 KB Time: 170 MS Language: C++ (g++ 4.7.2) Result: Accepted */ #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn=100000+5; int par[maxn],sum[maxn]; int n,m,x,y; char op[5]; void init(int x) { for(int i=0; i<=x; i++) { par[i]=i; sum[i]=1; } } int find(int x) { return par[x]==x?x:par[x]=find(par[x]); } bool same(int x,int y) { return find(x)==find(y); } void unite(int x,int y) { x=find(x); y=find(y); if(x==y)return; par[x]=y; sum[y]+=sum[x]; } int main() { int cases=0; //freopen("D://imput.txt","r",stdin); while(~scanf("%d%d",&n,&m)) { init(n); cases++; if(cases!=1)printf("\n"); printf("Case %d:\n",cases); for(int i=0; i<m; i++) { scanf("%s",op); if(op[0]=='Q') { scanf("%d",&x); printf("%d\n",sum[find(x)]); } else { scanf("%d%d",&x,&y); unite(x,y); } } } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/qq_18661257/article/details/46790007