首页 > 其他 > 详细

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

时间:2015-07-31 08:59:53      阅读:264      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!