Find them, Catch them
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 36913 Accepted: 11349
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:
D [a] [b]
where [a] and [b] are the numbers of two criminals, and they belong to different gangs.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
POJ Monthly–2004.07.18
法一:
所有元素个数为2 * N,如果i表示属于帮派A,那么i + N表示属于帮派B,每次输入两个家伙不在同一帮派的时候,就合并他们分属的两个帮派的元素。我认为这是最简单最好懂的算法,那些利用复杂节点带权重接着依靠大量if-else维护并查集的做法都不够美。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 100000 + 5;
int par[maxn*2];
int union_find(int x) {
return x == par[x] ? x : par[x] = union_find(par[x]);
}
bool union_same(int a, int b) {
return union_find(a) == union_find(b);
}
void union_unite(int a, int b) {
par[union_find(a)] = union_find(b);
}
void union_init(int n) {
for(int i = 0; i <= n; i++) par[i] = i;
}
int main()
{
int T;
scanf("%d", &T);
while(T--) {
int n, m;
scanf("%d%d", &n, &m);
getchar();
union_init(2*n);
for(int i = 0; i < m; i++) {
char op;
int a, b;
scanf("%c %d %d", &op, &a, &b);
getchar();
if(op == 'A') {
if(union_same(a, b)) {
cout << "In the same gang." << endl;
} else if(union_same(a, b + n)) {
cout << "In different gangs." << endl;
} else {
cout << "Not sure yet." << endl;
}
} else {
union_unite(a, b + n);//合并a和b的反派(跟a同派)
union_unite(a + n, b);//合并b和a的反派(跟b同派)
}
}
}
return 0;
}
法二:方法跟第一个一样,不同的是用OppoGang[X]来表示X的反派,而不是用X+N。这样更自然,更直接。
#include <iostream>
#include<cstdio>
#define MAX_N 100005
using namespace std;
int parent[MAX_N];
int height[MAX_N];/*height数组记录树的高度,合并时矮的树合并到高的树上,避免出现退化。而实际上在GetFather的时候有压缩路径,所以不用担//心退化问题。因此,可以去掉该数组和相应的操作,就像法一。*/
int OppoGang[MAX_N];
void makeSet(const int& n){
for (int i = 1; i <= n; ++i){
parent[i] = i;
height[i] = 0;
OppoGang[i]=0;
}
}
int GetFather(const int& x){
if (parent[x] == x){
return x;
}
else{
return parent[x] = GetFather(parent[x]);
}
}
void unite(int x, int y){
x = GetFather(x); y = GetFather(y);
if (x == y) return;
if (height[x] < height[y]){
parent[x] = y;
}
else{
parent[y] = x;
if (height[x] == height[y])
++height[x];
}
}
bool same(const int& x, const int& y)
{
return GetFather(x) == GetFather(y);
}
int main(){
int T;
cin >> T;
while (T--){
int N, M;
cin >> N >> M;
makeSet(N );
char message;
int x, y;
getchar();
while (M--){
scanf("%c%d%d", &message, &x, &y);
getchar();
if (message == 'A'){
if (same(x,y)){
cout << "In the same gang." << endl;
}
else if (same(x,OppoGang[y])){
cout << "In different gangs." << endl;
}
else{
cout << "Not sure yet." << endl;
}
}
else{
if(OppoGang[x]==0) OppoGang[x]=y;
if(OppoGang[y]==0) OppoGang[y]=x;
unite(x, OppoGang[y]);
unite(OppoGang[x], y);
}
}
}
return 0;
}