标签:
1 #include<iostream> 2 #include<stdio.h> 3 #define MAXN 100002 4 using namespace std; 5 6 int pre[MAXN]; 7 int root[MAXN]; 8 int enemy[MAXN]; 9 10 void init(int n) 11 { 12 int i; 13 for(i = 1; i <= n; ++ i) 14 { 15 pre[i] = i; 16 root[i] = 0; 17 enemy[i] = -1; 18 } 19 } 20 21 int find_pre(int x) 22 { 23 if(pre[x] != x) 24 { 25 pre[x] = find_pre(pre[x]); 26 } 27 return pre[x]; 28 } 29 30 int comb(int x,int y) 31 { 32 if(x == -1) 33 return y; 34 if(y == -1) 35 return x; 36 //if(root[x] > root[y]) 37 //{ 38 pre[y] = x; 39 return x; 40 // } 41 //else 42 //{ 43 // pre[x] = y; 44 // if(root[x] == root[y]) 45 // ++ root[y]; 46 // return y; 47 //} 48 } 49 50 int main() 51 { 52 //freopen("acm.acm","r",stdin); 53 int test; 54 scanf("%d",&test); 55 int n; 56 int m; 57 int i; 58 char q; 59 int u; 60 int v; 61 int temp1; 62 int temp2; 63 while(test --) 64 { 65 scanf("%d%d",&n,&m); 66 init(n); 67 for(i = 0; i < m; ++ i) 68 { 69 cin>>q; 70 if(q == ‘D‘) 71 { 72 scanf("%d%d",&u,&v); 73 temp1 = find_pre(v); 74 temp2 = find_pre(u); 75 u = comb(temp1,enemy[temp2]); 76 v = comb(enemy[temp1],temp2); 77 enemy[u] = v; 78 enemy[v] = u; 79 } 80 else 81 { 82 scanf("%d%d",&u,&v); 83 temp1 = find_pre(u); 84 temp2 = find_pre(v); 85 if(temp1 == temp2) 86 cout<<"In the same gang."<<endl; 87 else if(temp1 == enemy[temp2]) 88 { 89 cout<<"In different gangs."<<endl; 90 } 91 else 92 { 93 cout<<"Not sure yet."<<endl; 94 } 95 } 96 } 97 } 98 }
标签:
原文地址:http://www.cnblogs.com/gavinsp/p/4566510.html