标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 41913 | Accepted: 12879 |
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.
Source
1 /*by SilverN*/ 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 using namespace std; 8 const int mxn=120000; 9 int fa[mxn<<1]; 10 char c[5]; 11 int n,m; 12 int find(int x){ 13 if(fa[x]==x)return x; 14 return fa[x]=find(fa[x]); 15 } 16 int main(){ 17 int T; 18 int i,j; 19 int x,y; 20 scanf("%d",&T); 21 while(T--){ 22 scanf("%d%d",&n,&m); 23 int nn=2*n; 24 for(i=1;i<=nn;i++){ 25 fa[i]=i; 26 } 27 for(i=1;i<=m;i++){ 28 scanf("%s%d%d",c,&x,&y); 29 if(c[0]==‘D‘){ 30 x=find(x);// 31 y=find(y);//AC以后试着优化,删了这两条,居然MLE了 32 //想来是这两步起了压缩并查集路径的作用? 33 int a=find(x+n); 34 int b=find(y+n); 35 fa[a]=y; 36 fa[b]=x; 37 38 } 39 else{ 40 if(find(x)==find(y)){ 41 printf("In the same gang.\n"); 42 continue; 43 } 44 if(find(x)==find(y+n) || find(y)==find(x+n)){ 45 printf("In different gangs.\n"); 46 continue; 47 } 48 printf("Not sure yet.\n"); 49 } 50 } 51 } 52 return 0; 53 }
标签:
原文地址:http://www.cnblogs.com/SilverNebula/p/5866771.html