Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

为什么我换了种统计的方式就对了?

Posted by ACM06_ghosthy at 2007-10-07 22:57:27 on Problem 2524
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator