1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<set> using namespace std; const int N = 500005; int pre[N], ran[N]; int m,n; void init() { for (int i = 1; i <= m;i++) { pre[i] = i; ran[i] = 0; } } int find(int x) { if(pre[x]==x) return x; int f = find(pre[x]); ran[x] = (ran[x] + ran[pre[x]]) % 2; return pre[x] = f; } void un(int a,int b) { int fx = find(a); int fy = find(b); if(fx!=fy) { ran[fx] = (ran[b] - ran[a] + 1) % 2; pre[fx] = fy; } return; } int main() { int t; scanf("%d",&t); while(t--) { scanf("%d%d",&m,&n); init(); for (int i = 1; i <= n;i++) { char a; cin >> a; if(a=='A') { int c, d; scanf("%d%d",&c,&d); int fx = find(c); int fy = find(d); if(fx!=fy) printf("Not sure yet.n"); else if(fx==fy&&ran[c]==ran[d]) printf("In the same gang.n"); else printf("In different gangs.n"); } else { int c, d; scanf("%d%d",&c,&d); un(c, d); } } } }
|
近期评论