您的位置:首页 > 其它

POJ1703——Find them, Catch them 并查集应用

2016-03-15 09:27 337 查看
Find them, Catch them

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 39239 Accepted: 12059
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次操作,一种告诉你某两个人属于不同的黑帮,一种要你查询两个人是不是在同一个黑帮。

分析:所以难点在哪里呢,因为告诉你的信息是某两个人属于不同的黑帮,所以你不能直接合并。

怎么分类呢?经典的方法,用x,x+n分别代表x属于A类和x属于B类。

怎么合并呢?比如告诉你x和y不在一个集合,那么就是两种可能,x-A,y-B或者x-B,y-A,我们把x,y+n合并,再把y,x+n合并。同一棵树上的时间同时发生或者同时不发生。也就是说,若x-A,则y一定属于B。

怎么查询呢?如果x和y在一棵树上,或者x+n和y+n在一棵树上,就是同一个黑帮的。如果x和y+n在同一棵树上,或者x+n和y在同一棵树上,就是不同的黑帮。否则不能判断。

代码如下

#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <queue>
#include <ctype.h>
#include <vector>
using namespace std;
#define INF 1000000000
typedef long long LL;
#define MAXN 200010

int par[MAXN];//父亲
int r[MAXN];//树的高度
//初始化n个元素
void init(int n)
{
for(int i=0;i<n;i++)
{
par[i]=i;
r[i]=0;
}
}
//查询数的根
int find(int x)
{
if(par[x]==x)
return x;
else
return par[x]=find(par[x]);
}
//合并x和y所属的集合
void unite(int x,int y){
x=find(x);
y=find(y);
if(x==y)
return;
if(r[x]<r[y])
par[x]=y;
else
par[y]=x;
if(r[x]==r[y])
r[x]++;
}
//判断x和y是否在同一个树上
bool same(int x,int y)
{
return find(x)==find(y);
}

int main()
{
int t;
cin>>t;
while(t--)
{
int n,m;
char op;
int a,b;
scanf("%d%d",&n,&m);
init(2*n);
getchar();
while(m--)
{
scanf("%c%d%d",&op,&a,&b);
a--,b--;
if(op=='D')
{
unite(a, b+n);
unite(a+n,b);
}
else if(op=='A')
{
if(same(a,b)||same(a+n,b+n))
printf("In the same gang.\n");
else if(same(a,b+n)||same(a+n,b))
printf("In different gangs.\n");
else
printf("Not sure yet.\n");
}
getchar();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: