标签:and can describes sea game body ref put tab
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 24130 | Accepted: 8468 | |
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
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 5 using namespace std; 6 7 int up[300005];//该数上面的数 8 int n[300005];//该集合总数 9 int a[300005];//保存他的父节点 10 11 int father(int t){ 12 if(t==a[t]){ 13 return t; 14 } 15 int fa=a[t]; 16 a[t]=father(a[t]); 17 up[t]+=up[fa]; 18 19 return a[t]; 20 } 21 22 int main() 23 { 24 int p; 25 //char c; 26 int t1,t2; 27 int t3; 28 while(scanf("%d",&p)!=EOF){ 29 for(int i=0;i<=p;i++){ 30 a[i]=i; 31 up[i]=0; 32 n[i]=1; 33 } 34 char s[10]; 35 for(int i=0;i<p;i++){ 36 scanf("%s",s); 37 if(s[0]==‘M‘){ 38 scanf("%d%d",&t1,&t2); 39 int fa=father(t1); 40 int fb=father(t2); 41 if(fa!=fb){ 42 a[fb]=fa; 43 up[fb]+=n[fa]; 44 n[fa]+=n[fb]; 45 } 46 }else{ 47 scanf("%d",&t3); 48 int ans=father(t3); 49 printf("%d\n",n[ans]-up[t3]-1); 50 } 51 } 52 } 53 return 0; 54 }
标签:and can describes sea game body ref put tab
原文地址:http://www.cnblogs.com/TWS-YIFEI/p/6028866.html