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;
}