Http://poj.org/problem? Id = 1703
It is basically the same as 2492, not to mention.
Code:
# Include <cstdio>
Using namespace STD;
Int f [100005];
Int R [100005];
Int n, m;
Int find_set (int x ){
Int temp;
If (x = f [x]) {
Return X;
}
Temp = f [x];
F [x] = find_set (temp );
R [x] = (R [x] + R [temp]) % 2; // maintain the stability of R [x] relative to the root node
Return f [x];
}
Void Union (int x, int y, int FX, int FY ){
F [FY] = FX;
R [FY] = (R [x] + R [y] + 1) % 2;
}
Int main (){
Int T, I, j, X, Y, FX, FY;
Char C;
Scanf ("% d", & T );
While (t --){
Scanf ("% d", & N, & M );
For (I = 0; I <= N; I ++ ){
F [I] = I;
R [I] = 0;
}
Getchar ();
While (M --){
Scanf ("% C % d", & C, & X, & Y );
Getchar ();
FX = find_set (X );
FY = find_set (y );
If (C = 'D '){
Union (X, Y, FX, FY );
}
If (C = 'A '){
If (FX = FY ){
If (R [x] = R [y])
Printf ("in the same gang. \ n ");
Else
Printf ("in different gangs. \ n ");
}
Else
Printf ("Not sure yet. \ n ");
}
}
}
Return 0;
}