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

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

Posted by 3013216027 at 2014-08-09 10:06:33 on Problem 2524
In Reply To:贴个282ms的代码,求2位数代码写法 Posted by:Belldandy at 2013-10-23 13:01:18
#include <stdio.h>

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

inline int read(void) {
	int x = 0;
	static char ch;
	while ((ch = getchar()) < '0' || (ch > '9'));
	x += ch - '0';
	while ((ch = getchar()) >= '0' && (ch <= '9')) {
		x *= 10;
		x += ch - '0';
	}
	return x;
}

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)
		{
			x = read();
                        y = read(); //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