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;
}


你可能感兴趣的:(POJ 1703 Find them, Catch them)