并查集。
这题错了不少次才过的。
分析见代码。
http://poj.org/problem?id=1703
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1e5 + ;
const char str1[] = "Not sure yet.";
const char str2[] = "In different gangs.";
const char str3[] = "In the same gang.";
int fa[maxn], belong[maxn], nex[maxn];
int n, m, u, v, k;
char op; int father(int u){
//WHAT WOULD CAUSE AN INFINITE LOOP
if(fa[u] == -) return u;
int tem = father(fa[u]);
belong[u] = belong[tem];
fa[u] = tem;
return tem;
} void solve(){
if(op == 'D'){
if(belong[u] == - && belong[v] == -){
//this is the simplest case
belong[u] = k++;
belong[v] = k++;
nex[u] = v;
nex[v] = u;
return;
}
if(belong[u] != - && belong[v] == -){
//notice that v is never mentioned, but u is already processed
//since is u is vistied, u got its partner
int fu = father(u), fu1 = father(nex[u]);
//draw a line from v to u1
fa[v] = fu1;
nex[v] = fu;
belong[v] = belong[fu1];
return;
//match a virtue partner for node u
}
if(belong[u] == - && belong[v] != -){
int fv = father(v), fv1 = father(nex[v]);
fa[u] = fv1;
nex[u] = fv;
belong[u] = belong[fv1];
return;
}
if(belong[u] != - && belong[v] != -){
//notice that both u and v is already visited
int fu = father(u), fu1 = father(nex[u]);
int fv = father(v), fv1 = father(nex[v]);
int bu = belong[fu] >> ;
int bv = belong[fv] >> ;
if(bu > bv){
// v is relatively primitive
fa[fu] = fv1;
fa[fu1] = fv;
return;
}
if(bu < bv){
fa[fv] = fu1;
fa[fv1] = fu;
return;
}
}
}
if(op == 'A'){
int fu = father(u);
int fv = father(v);
if(belong[fu] == - || belong[fv] == -) puts(str1);
else if(belong[fu] == belong[fv]) puts(str3);
else if(belong[fu] == ( ^ belong[fv])) puts(str2);
else puts(str1);
}
} int main(){
freopen("in.txt", "r", stdin);
int T;
scanf("%d", &T);
while(T--){
scanf("%d%d", &n, &m);
memset(belong, -, sizeof belong);
memset(fa, -, sizeof fa);
k = ;
for(int i = ; i < m; i++){
scanf(" %c%d%d", &op, &u, &v);
solve();
}
}
}