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 497597816 at 2007-06-02 07:28:05 on Problem 2524
#include "stdio.h"
int stu[50001];
int main()
{
	int num=0;
	int cusor(int a);
	int a,b,i,j,k,m,n,sum,ta,tb;
	while(scanf("%d %d",&m,&n)==2&&m!=0&&n!=0){
		num++;
		for(k=1;k<=50000;k++)stu[k]=k;
		sum=0;
		for(i=0;i<n;i++){
		scanf("%d %d",&a,&b);
		if(stu[b]==b){stu[b]=a;}
		else if(stu[a]==a){stu[a]=b;}
		else{
		ta=cusor(a);
		tb=cusor(b);
		stu[tb]=ta;
		}
		}
		for(j=1;j<=m;j++)
			if(stu[j]==j)sum++;
		printf("Case %d: %d\n",num,sum);
	}
	return 0;
}
int cusor(int a){
	while(stu[a]!=a)a=stu[a];
	return a;
}

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