POJ 1703 Find them, Catch them

题目链接http://poj.org/problem?id=1703


详细题解:种类并查集, 做完了食物链这道题果然是妥妥的 O(∩_∩)O~~~


详细做法请理解食物链那道题0.0……


#include<cstdio>
#include<cstring>
#define N 110000
#define MOD 2
int F[N];
int rel[N];
int fun(int x)
{
    if(F[x] != x)
    {
        int fa_x = fun(F[x]);
        rel[x] = (rel[x] + rel[F[x]]) % MOD;
        F[x] = fa_x;
    }
    return F[x];
}
int main ()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        int n, m;
        scanf("%d %d", &n, &m);
        for(int i = 0; i <= n; i++)
            F[i] = i, rel[i] = 0;


        while(m--)
        {
            getchar();
            char ch;
            int a, b;
            scanf("%c %d %d", &ch, &a, &b);
            int fa = fun(a);
            int fb = fun(b);
            if(ch == 'A')
            {
                if(fa != fb)
                    printf("Not sure yet.\n");
                else
                {
                    int tmp = (2-rel[a]+rel[b])%MOD;
                    if(tmp == 0)
                        printf("In the same gang.\n");
                    else
                        printf("In different gangs.\n");
                }
            }

            if(ch == 'D')
            {
                if(fa != fb)
                {
                    F[fb] = fa;
                    rel[fb] = (2-rel[b]+1+rel[a]) % MOD;
                }
            }
        }

    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值