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

为什么会TLE。。。。

Posted by solo_013 at 2012-08-09 17:03:01 on Problem 1611
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 30005
int n,m;//n 人数,m团体数
int father[N],sum[N],ans;
int find(int i){
	if(father[i]==i)
		return i;
	father[i]=find(father[i]);
	return father[i];
}
void un(int x,int y){
	int fx,fy;
	fx=find(x);
	fy=find(y);
	if(sum[fx]>=sum[fy]){
		father[fy]=fx;
		sum[fx]+=sum[fy];
	}
	else
	{
		father[fx]=fy;
		sum[fy]+=sum[fx];
	}
	return ;
}

int main(){
	int i,j,k,temp1,temp2;
	while(scanf("%d%d",&n,&m)==2,m+n){
		for(i=0;i<n;i++){
			sum[i]=1;
			father[i]=i;
		}
		while(m--){
			scanf("%d",&k);
			if(k==0||k==1)
				continue;
			scanf("%d",&temp1);
			for(i=1;i<k;i++){
				scanf("%d",&temp2);
				un(temp1,temp2);
			}
		}
		ans=sum[find(0)];
		printf("%d\n",ans);
	}
	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