Poj 1703 finde them, catch them! (Query set)

Source: Internet
Author: User
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;

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.