POJ 1703 Find them, Catch them

这道题与POJ 2492 几乎一模一样,就不多说了,直接上代码吧

 

 1 //#define LOCAL
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 using namespace std;
 6 
 7 const int maxn = 100000 + 10;
 8 int p[maxn], r[maxn];
 9 
10 int Find(int a)
11 {
12     if(p[a] == a)    return a;
13     int temp = p[a];
14     p[a] = Find(p[a]);
15     r[a] = (r[a] ^ r[temp]);
16     return p[a];
17 }
18 
19 int main(void)
20 {
21     #ifdef LOCAL
22         freopen("1703in.txt", "r", stdin);
23     #endif
24 
25     int T;
26     scanf("%d", &T);
27     while(T--)
28     {
29         int n, m, x, y;
30         char cmd[10];
31         scanf("%d %d", &n, &m);
32         for(int i = 0; i <= n; ++i)    p[i] = i;
33         memset(r, 0, sizeof(r));
34         for(int i = 0; i < m; ++i)
35         {
36             scanf("%s %d %d", cmd, &x, &y);
37             int px = Find(x);
38             int py = Find(y);
39             if(cmd[0] == 'A')
40             {
41                 if(px != py)
42                     printf("Not sure yet.\n");
43                 else
44                 {
45                     if((r[x] + r[y]) % 2)
46                         printf("In different gangs.\n");
47                     else
48                         printf("In the same gang.\n");
49                 }
50             }
51             else
52             {
53                 p[px] = py;
54                 r[px] = 1 - (r[x] ^ r[y]);
55             }
56         }
57     }
58     return 0;
59 }
代码君

 

转载于:https://www.cnblogs.com/AOQNRMGYXLMV/p/3959946.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值