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

100题~贴个代码纪念一下

Posted by qitaishui at 2012-01-26 13:41:54 on Problem 2524
#include<stdio.h>
int pre[50005];
int find(int x){
	int r,t,z;
	r=t=x;
	while(r!=pre[r])
		r=pre[r];
	while(t!=r){// 压缩路径
		z=pre[t];
		pre[t]=r;
		t=z;
	}
	return r;
}
int main()
{
	int n,m,i,j,a,b,total,fa,fb,num;
	num=0;
	while(scanf("%d%d",&n,&m)&&n){
		num++;
		total=n;
		for(i=1;i<=n;i++)
			pre[i]=i;
		for(i=1;i<=m;i++){
			scanf("%d%d",&a,&b);
			fa=find(a),fb=find(b);
			if(fa!=fb){
				pre[fa]=fb;
				total--;
			}
		}
		printf("Case %d: %d\n",num,total);
	}
}

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