// // main.cpp // Richard // // Created by 邵金杰 on 16/7/1. // Copyright © 2016年 邵金杰. All rights reserved. // #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn=30000+1000; int pa[maxn],sum[maxn],under[maxn]; int getroot(int a) { if(pa[a]==a) return a; int t=getroot(pa[a]); under[a]+=under[pa[a]]; pa[a]=t; return pa[a]; } void merge(int a,int b) { int root1=getroot(a); int root2=getroot(b); if(root1==root2) return ; pa[root2]=root1; under[root2]=sum[root1]; sum[root1]+=sum[root2]; } int main() { int t,x,y; char cmd[10]; cin>>t; for(int i=0;i<maxn;i++) {sum[i]=1;pa[i]=i;under[i]=0;} while(t--) { scanf("%s",cmd); if(cmd[0]=='M') {cin>>x>>y;merge(x,y);} else {cin>>x;getroot(x);cout<<under[x]<<endl;} } } |
Double click to view unformatted code.