(中等) POJ 1703 Find them, Catch them,带权并查集。

Description

The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, the police first needs to identify which gang a criminal belongs to. The present question is, given two criminals; do they belong to a same clan? You must give your judgment based on incomplete information. (Since the gangsters are always acting secretly.) 

Assume N (N <= 10^5) criminals are currently in Tadu City, numbered from 1 to N. And of course, at least one of them belongs to Gang Dragon, and the same for Gang Snake. You will be given M (M <= 10^5) messages in sequence, which are in the following two kinds: 

1. D [a] [b] 
where [a] and [b] are the numbers of two criminals, and they belong to different gangs. 

2. A [a] [b] 
where [a] and [b] are the numbers of two criminals. This requires you to decide whether a and b belong to a same gang. 

 

  题目就是给了一些点不在一个集合内,问某两个点是否在一个集合内。

  带权并查集的经典题,要注意两个集合合并是判断是否是一个,然后就是权相加的时候要注意。

  不过感觉这个题目有些bug,题目说每个集合至少一个,这样的话就应该判断N=2的情况,但是没判断也过了。。。

 

代码如下:

// ━━━━━━神兽出没━━━━━━
//      ┏┓       ┏┓
//     ┏┛┻━━━━━━━┛┻┓
//     ┃           ┃
//     ┃     ━     ┃
//     ████━████   ┃
//     ┃           ┃
//     ┃    ┻      ┃
//     ┃           ┃
//     ┗━┓       ┏━┛
//       ┃       ┃
//       ┃       ┃
//       ┃       ┗━━━┓
//       ┃           ┣┓
//       ┃           ┏┛
//       ┗┓┓┏━━━━━┳┓┏┛
//        ┃┫┫     ┃┫┫
//        ┗┻┛     ┗┻┛
//
// ━━━━━━感觉萌萌哒━━━━━━

// Author        : WhyWhy
// Created Time  : 2015年07月17日 星期五 20时24分40秒
// File Name     : 1703_1.cpp

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

const int MaxN=100005;

int N;
int fa[MaxN],val[MaxN];

int find(int x,int &num)
{
    if(fa[x]==-1)
        return x;

    int tn=0;
    int t=find(fa[x],tn);

    fa[x]=t;
    num=tn+val[x];
    val[x]=num;

    return fa[x];
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    
    int T,Q;
    int a,b;
    int ba,bb;
    int x,y;
    char s[10];

    scanf("%d",&T);

    while(T--)
    {
        scanf("%d %d",&N,&Q);

        memset(fa,-1,sizeof(fa));
        
        while(Q--)
        {
            scanf("%s %d %d",s,&a,&b);

            if(s[0]=='D')
            {
                x=y=0;
                ba=find(a,x);
                bb=find(b,y);
    
                if(ba!=bb)
                {
                    fa[ba]=bb;
                    val[ba]=x+y+1;
                }
            }
            else
            {
                x=y=0;
                ba=find(a,x);
                bb=find(b,y);

                if(ba!=bb)
                    puts("Not sure yet.");
                else if((y-x)%2)
                    puts("In different gangs.");
                else
                    puts("In the same gang.");
            }
        }
    }
    
    return 0;
}
View Code

 

posted @ 2015-07-17 20:43  WhyWhy。  阅读(165)  评论(0编辑  收藏  举报