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

POJ 1703 Find them, Catch them (种类并查集)

时间:2020-03-23 00:14:47      阅读:66      评论:0      收藏:0      [点我收藏+]

标签:class   tac   个人   ++   name   first   ble   can   实现   

Find them, Catch them

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 61806   Accepted: 18734

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.

Input

The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. Each test case begins with a line with two integers N and M, followed by M lines each containing one message as described above.

Output

For each message "A [a] [b]" in each case, your program should give the judgment based on the information got before. The answers might be one of "In the same gang.", "In different gangs." and "Not sure yet."

Sample Input

1
5 5
A 1 2
D 1 2
A 1 2
D 2 4
A 1 4

Sample Output

Not sure yet.
In different gangs.
In the same gang.

题目大意与分析

n个人,分成两个帮派,给出m次操作,A a b 代表查询ab是否是同一个派别,D a b 代表ab不是同一个派别

通过这个题学习了种类并查集

普通的并查集是将相同的合并为一个祖先,但是本题给的是元素之间的不同关系,可以通过开两倍的数组来实现

合并时 将a+n与b合并 a与b+n合并,代表两者敌对,查询时如果a与b的祖先相同则代表是同一帮派,如果a+n与b的祖先(或a与b+n的祖先)相同则属于对立帮派

本题输入输出cin cout会超时

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <math.h>
#include <cstdio>
using namespace std;

int s[200005],t,n,m,i;

int findf(int x)
{
    return x==s[x]?x:s[x]=findf(s[x]);
}

void hebing (int x,int y)
{
    int fx=findf(x);
    int fy=findf(y);
    if(fx!=fy)
    {
        s[fx]=fy;
    }
}

int pd(int x,int y)
{
    if(findf(x)==findf(y))
    {
        return 1; 
    }
    else
    {
        return 0;
    }
}

int main()
{
    ios::sync_with_stdio(false);
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&m); 
        for(i=1;i<=2*n;i++)
        {
            s[i]=i;
         } 
        while(m--)
        {
            int a,b;
            char S[5];
            scanf("%s%d%d",S,&a,&b);
            if(S[0]==D)
            {
                hebing(a+n,b);
                hebing(a,b+n);
            }
            else
            {
                if(pd(a,b)==1)
                {
                    cout << "In the same gang." << endl;
                }
                else if(pd(a,b+n)==1)
                {
                    cout << "In different gangs." << endl;
                }
                else
                {
                    cout << "Not sure yet." << endl;
                }
            }
        }
    }
}

 




POJ 1703 Find them, Catch them (种类并查集)

标签:class   tac   个人   ++   name   first   ble   can   实现   

原文地址:https://www.cnblogs.com/dyhaohaoxuexi/p/12549289.html

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