Poj 2524 ubinquitous religions (query set)

Source: Internet
Author: User
Http://poj.org/problem? Id = 2524

Still simple and check the set, take 1611 to change the fire speed to submit, decisive pe... put the last space in front of % d, it is really worth it if it doesn't run.

But there is a question: why is the merge by rank faster than the merge directly ??

Code:

# Include <cstdio>
Int P [50005], R [50005];
Void make_set (int n ){
For (INT I = 0; I <n; I ++ ){
P [I] = I;
R [I] = 0;
}
}
Int find_set (int x ){
If (X! = P [x])
P [x] = find_set (P [x]);
Return P [x];
}
Void union_set (int x, int y ){
X = find_set (X );
Y = find_set (y );
If (X! = Y ){
If (R [x]> r [y])
P [y] = X;
Else {
P [x] = y;
If (R [x] = R [y])
R [x] ++;
}

}
}
Int main (){
Int n, m, I, X, Y, sum, T = 0;
While (~ Scanf ("% d", & N, & M) & (n + M )){
T ++;
Make_set (N );
While (M --){
Scanf ("% d", & X );
Scanf ("% d", & Y );
Union_set (x, y );
}
Sum = 0;
For (I = 0; I <n; I ++)
If (I = find_set (I ))
Sum ++;
Printf ("case % d: % d \ n", T, sum );
}
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.