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

贴个282ms的代码,求2位数代码写法

Posted by Belldandy at 2013-10-23 13:01:18 on Problem 2524
#include <stdio.h>

int student[50001],n=0;
int count;

inline int find(int x)
{
	while(student[x]!=x)
	{
		x=student[x];
	}
	return x;
}

void change(int x,int y)
{
	int i=0;
	int px=find(x),py=find(y);
	if(px!=py)
	{
		--count;
		student[px]=py;
	}
	return;
}

int main(void)
{
	int k=0,m=0;
	int i=0,x=0,y=0;

	while(scanf("%d%d",&n,&m)&&n+m)
	{
		for(i=1;i<=n;++i)
		{
			student[i]=i;
		}
		count=n;
		for(i=0;i<m;++i)
		{
			scanf("%d%d",&x,&y);
			change(x,y);
		}	
		printf("Case %d: %d\n",++k,count);
	}
}

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