Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
为什么我换了种统计的方式就对了?#include <iostream> using namespace std; int n,m ,ans ; int root[50001]; int rank[50001]; bool P[50001]; int find( int t ) { int r = t , i ; while ( root[r] != r ) r = root[r]; while ( r != t ) { i = root [ t ] ; root[ t ] = r ; t = i ; } return r; } void Union ( int x , int y ) { x = find(x); y = find(y); if (x!=y) { ans -- ; if (rank[x] > rank[y] ) { root[y] = x ; rank[x]+=rank[y];} else { root[x] = y ; rank[y] += rank[x]; } } return ; } int main() { int test = 0 ; while (cin >> n >> m ) { ans = n ; test++; if ( n == 0 ) break; int h; memset(P,0,sizeof(P)); for ( h = 1 ; h <= n ; h ++ ) { root[h] = h ; rank[h] = 1 ; } for ( h = 1 ; h <= m ; h ++ ) { int x , y ; cin >> x >> y ; Union(x,y); } cout << "Case "<<test << ": " << ans <<endl; } return 0; } ................................. Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator