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

POJ 1173 Find them, Catch them

时间:2015-09-06 20:12:56      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

题意:有两个帮派,每个人只属于一个帮派,m次操作,一种操作告诉你两个人不是一个帮派的,另一种操作问两个人是不是在一个帮派。

 

解法:并查集+向量偏移。偏移量表示和根节点是不是同一帮派,是为0,不是为1。

 

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long

using namespace std;

int father[100005], delta[100005];
void init()
{
    memset(delta, 0, sizeof delta);
    for(int i = 0; i < 100005; i++)
        father[i] = i;
}
int Find(int a)
{
    if(father[a] != a)
    {
        int tmp = Find(father[a]);
        delta[a] = (delta[a] + delta[father[a]]) % 2;
        father[a] = tmp;
    }
    return father[a];
}
int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        init();
        int n, m;
        scanf("%d%d", &n, &m);
        while(m--)
        {
            char o[2];
            int x, y;
            scanf("%s%d%d", o, &x, &y);
            int c = Find(x), d = Find(y);
            if(o[0] == ‘A‘)
            {
                if(c != d) puts("Not sure yet.");
                else
                {
                    if(delta[x] == delta[y]) puts("In the same gang.");
                    else puts("In different gangs.");
                }
            }
            else
            {
                if(c == d) continue;
                father[c] = d;
                delta[c] = (delta[y] - delta[x] + 1) % 2;
            }
        }
    }
    return 0;
}

  

POJ 1173 Find them, Catch them

标签:

原文地址:http://www.cnblogs.com/Apro/p/4786915.html

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