|
- #include<cstdio>
- const int N=30000;
- int sum[N+1];
- int under[N+1];
- int f[N+1];
- int p;
- int cnt;
- int ans[1000001];
- int getf(int num)
- {
- if(num==f[num])
- {
- return num;
- }
- int t=getf(f[num]);
- under[num]+=under[f[num]];
- f[num]=t;
- return f[num];
- }
- void merge(int a,int b)
- {
- int t1=getf(a);
- int t2=getf(b);
- if(t1!=t2)
- {
- f[t2]=t1;
- under[t2]=sum[t1];
- sum[t1]+=sum[t2];
- }
- }
- int main()
- {
- freopen("testdata.in","r",stdin);
- freopen("testme.out","w",stdout);
- for(int i=1;i<=N;i++)
- {
- sum[i]=1;
- under[i]=0;
- f[i]=i;
- }
- scanf("%d",&p);
- for(int i=1;i<=p;i++)
- {
- char miao;
- scanf("%c",&miao);
- scanf("%c",&miao);
- if(miao=='M')
- {
- int x,y;
- scanf("%d%d",&x,&y);
- merge(y,x);
- }
- else
- {
- if(miao=='C')
- {
- int x;
- scanf("%d",&x);
- getf(x);
- printf("%d\n",under[x]);
- }
- }
- }
- return 0;
- }
复制代码 |
|