M = = 0 has trick ... N < 2 also has trick ...
1#include"iostream"2#include"Cstdio"3#include"CString"4#include"Map"5 using namespacestd;6 intN, M;7 intrt[100010], w[100010];8 intsize[100010], friend[100010];9 Ten intN,m,l,r; One Charstr[Ten]; A - voidPacointa) - { the if(rt[a]==a) { - return ; - } - Paco (Rt[a]); +W[a] ^=W[rt[a]]; -Rt[a] =Rt[rt[a]]; + } A at BOOLUnio (intAintb) - { - Paco (a); - Paco (b); - intRt_a = Rt[a],rt_b =Rt[b]; - if(rt_a==rt_b) { in returnW[A]! =W[b]; - } to Else { +RT[RT_A] =Rt_b; -W[RT_A] = (w[a] = =w[b]); the if(W[rt_a] = =0) { *Friend[rt_b] + =Friend[rt_a]; $ }Panax Notoginseng Else { -Friend[rt_b] + = (size[rt_a)-friend[rt_a]); the } +Size[rt_b] + =Size[rt_a]; A } the return true; + } - $ intMain () $ { - intI, T; -scanf"%d", &T); the while(t--) { -Memset (W,0,sizeof(w));Wuyiscanf"%d%d", &n, &M); the if(N <2) { -printf"Poor wyh\n"); Wu Continue; - } About for(i =0; I <= N; ++i) { $Rt[i] =i; -Size[i] =1; -Friend[i] =1; - } A inti; + intu, v; the BOOLOK =1; - for(i =1; I <= M; ++i) { $scanf"%d%d", &u, &v); the //if (w[u] = =-1) { the //W[u] = 0; the // } the //if (w[v] = =-1) { - //W[v] = 0; in // } the if(!Unio (U, v)) { theOK =0; About } the //Int J; the //For (j = 1; j <= N; ++j) { the //printf ("w[%d] = =%d rt[%d] = =%d\n", J, W[j], J, Rt[j]); + // } - //printf ("\ n"); the }Bayi if(OK) { the intres =0; the for(i =1; I <= N; ++i) { - if(Rt[i] = =i) { -Res + = min (Friend[i], Size[i]-friend[i]); the } the } the intMax = N-Res; the intMin =Res; - if(Max <Min) { the swap (Max, Min); the } the if(Max = =N) {94--Max; the++Min; the } theprintf"%d%d\n", Max, Min);98 } About Else { -printf"Poor wyh\n");101 }102 }103}
HDU 5285 bestcoder Round #48 ($) 1002 species and search collection