| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
为什么会TLE。。。。#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator