POJ1703Find them, Catch them[种类并查集]

Find them, Catch them
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 42416   Accepted: 13045

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.

Source


题意:给出不在同一集合关系
罪犯只可能属于两个团伙中的一个,现在给出M个条件(D a b表示a和b不在同一团伙),
对于每一个询问(A a b)确定a,b是不是属于同一团伙或者不能确定。

 
种类并查集类似于带权并查集v[i]=0表示i与父相同,v[i]=1表示不同
路径压缩和合并时推导一下
&1相当于%2
//
// main.cpp
// poj1703
//
// Created by Candy on 30/10/2016.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
const int N=1e5+;
typedef long long ll;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int T,n,m,a,b;
char c[];
int fa[N],v[N];
inline int find(int x){
if(x==fa[x]) return x;
int root=find(fa[x]);
v[x]=(v[x]+v[fa[x]])&;
return fa[x]=root;
}
inline void unn(int x,int y){
int f1=find(x),f2=find(y);
if(f1!=f2){
fa[f1]=f2;
v[f1]=(v[x]+v[y]+)&;
}
}
int main(int argc, const char * argv[]) {
T=read();
while(T--){
n=read();m=read();
for(int i=;i<=n;i++) fa[i]=i,v[i]=;
for(int i=;i<=m;i++){
scanf("%s",c);a=read();b=read();
if(c[]=='D') unn(a,b);
else{
int f1=find(a),f2=find(b);
if(f1!=f2) puts("Not sure yet.");
else if(v[a]==v[b]) puts("In the same gang.");
else puts("In different gangs.");
}
}
} return ;
}
上一篇:使用Fiddler的X5S插件查找XSS漏洞


下一篇:使程序能够引入.json文件, 为网站添加 MIME 映射