码迷,mamicode.com
首页 > 其他好文 > 详细

【转】POJ-1703-Find them, Catch them:集合内的元素具有某种关系,集合间的关系不确定

时间:2015-07-31 09:00:01      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

和昨天晚上做的 A Bug‘s Life是一样思路的题···开始做的时候还是有些小问题,就在各种问题中进步吧。

#include<cstdio>
#define Size 100000
int N, M;
int Rel[Size+1], Pre[Size+1];

void Init()
{
        for( int i=1; i<=N; i++ )
        {
                Rel[i]=0; // 自己和自己同一个帮派
                Pre[i]=i;
        }
}

int GetPre(int a)
{
        if( Pre[a] == a )
            return a;
        int t = GetPre(Pre[a]);
        Rel[a] = Rel[a]^Rel[Pre[a]];
        Pre[a] = t;
        return Pre[a];
}

void Union(int a, int b)
{
        int Pa = GetPre(a);
        int Pb = GetPre(b);

        if( Pa == Pb )
            return;

        Pre[Pa] = Pb;
        Rel[Pa] = ~(Rel[a]^Rel[b]);
}

int main()
{
        int T;
        scanf("%d", &T);
        while(T--)
        {
                scanf("%d%d", &N, &M);
                Init();
                while(M--)
                {
                        char C;
                        int a, b;
                        getchar();//这个怎么可以忘呢···
                        scanf("%c%d%d", &C, &a, &b);
                        if( C==‘A‘ )
                        {
                                int Pa = GetPre(a);
                                int Pb = GetPre(b);
                                if( Pa==Pb )
                                    if( Rel[a] == Rel[b] )
                                        printf("In the same gang.\n");
                                    else
                                        printf("In different gangs.\n");
                                else
                                    printf("Not sure yet.\n");
                        }
                        else
                            Union(a, b);
                }
        }
        return 0;
}

  

【转】POJ-1703-Find them, Catch them:集合内的元素具有某种关系,集合间的关系不确定

标签:

原文地址:http://www.cnblogs.com/FightForCMU/p/4691145.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!