标签:
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 20392 | Accepted: 7148 | |
Case Time Limit: 1000MS |
Description
Input
Output
Sample Input
6 M 1 6 C 1 M 2 4 M 2 6 C 3 C 4
Sample Output
1 0 2
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<string> #include<algorithm> #include<queue> #include<vector> #include<stack> using namespace std; #define maxn 30010 int n; int dist[maxn],fa[maxn],rnk[maxn]; char opt[2]; int find(int x) { if(fa[x]==x) return fa[x]; int temp=fa[x]; fa[x]=find(fa[x]); dist[x]+=dist[temp]; return fa[x]; } int main() { int x,y; while(scanf("%d",&n)!=EOF) { for(int i=0;i<maxn;i++) { fa[i]=i; dist[i]=0; rnk[i]=1; } for(int i=0;i<n;i++) { scanf("%s",opt); if(opt[0]==‘M‘) { scanf("%d%d",&x,&y); int fx,fy; fx=find(x),fy=find(y); if(fx!=fy) { fa[fy]=fx; dist[fy]=rnk[fx]; rnk[fx]+=rnk[fy]; } } else if(opt[0]==‘C‘) { scanf("%d",&x); printf("%d\n",rnk[find(x)]-dist[x]-1); } } } return 0; }
标签:
原文地址:http://www.cnblogs.com/a972290869/p/4393713.html