标签:des c style class blog code
Description
Input
Output
Sample Input
1 5 5 A 1 2 D 1 2 A 1 2 D 2 4 A 1 4
Sample Output
Not sure yet. In different gangs. In the same gang.
1 #include"iostream" 2 using namespace std; 3 int p[100004],rela[100004]; 4 int t,n,m,a,b; 5 char flag; 6 void make() 7 { 8 for(int i=1;i<=n;i++) 9 { 10 p[i]=i; 11 rela[i]=0; 12 } 13 } 14 int find(int x) 15 { 16 int temp=p[x]; 17 if(x==p[x]) 18 return x; 19 p[x]=find(p[x]); 20 rela[x]=(rela[x]==rela[temp])?0:1; 21 return p[x]; 22 } 23 void unionset(int x,int y,int px,int py) 24 { 25 p[px]=py; 26 rela[px]=(rela[x]==rela[y])?1:0; 27 } 28 int main() 29 { 30 cin>>t; 31 while(t--) 32 { 33 cin>>n>>m; 34 make(); 35 while(m--) 36 { 37 getchar(); 38 scanf("%c%d%d",&flag,&a,&b); 39 int pa=find(a); 40 int pb=find(b); 41 if(flag==‘A‘) 42 { 43 if(pa!=pb) 44 { 45 cout<<"Not sure yet."<<endl; 46 continue; 47 } 48 if(rela[a]==rela[b]) 49 { 50 cout<<"In the same gang."<<endl; 51 continue; 52 } 53 cout<<"In different gangs."<<endl; 54 continue; 55 } 56 if(flag==‘D‘) 57 { 58 if(p[a]!=p[b]) 59 unionset(a,b,pa,pb); 60 } 61 } 62 } 63 return 0; 64 }
Find them, Catch them,布布扣,bubuko.com
标签:des c style class blog code
原文地址:http://www.cnblogs.com/767355675hutaishi/p/3763477.html