标签:poj1703
1 5 5 A 1 2 D 1 2 A 1 2 D 2 4 A 1 4
Not sure yet. In different gangs. In the same gang.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long LL;
#define maxn 100005
int pre[maxn << 1];
int ufind(int k) {
int a = k, b;
while(pre[k]) k = pre[k];
while(a != k) {
b = pre[a];
pre[a] = k;
a = b;
}
return k;
}
bool same(int a, int b) {
return ufind(a) == ufind(b);
}
bool unite(int a, int b) {
a = ufind(a);
b = ufind(b);
if(a == b) return false;
pre[a] = b;
return true;
}
int main() {
// freopen("stdin.txt", "r", stdin);
int T, N, M, i, a, b;
char ch[2];
scanf("%d", &T);
while(T--) {
scanf("%d%d", &N, &M);
memset(pre, 0, sizeof(int) * (2 * N + 1));
while(M--) {
scanf("%s%d%d", ch, &a, &b);
if(*ch == 'D') {
unite(a, b + N);
unite(a + N, b);
} else {
if(same(a, b + N))
printf("In different gangs.\n");
else if(same(a, b))
printf("In the same gang.\n");
else printf("Not sure yet.\n");
}
}
}
return 0;
}POJ1703 Find them, Catch them 【并查集】
标签:poj1703
原文地址:http://blog.csdn.net/chang_mu/article/details/41675475