| ||||||||||
| 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 | |||||||||
这段 344MSIn Reply To:两段并查集代码,谁能告诉我为什么第二段要慢些么…… Posted by:speedcell4 at 2011-08-01 16:46:43 #include<iostream>
#define SIZE 50001
using namespace std;
int n,m,x,y,times=1,result=0;
int a[SIZE]={0};
void Init(int n)
{
result=n;
for(int i=0;n-i>0;i++) a[i]=i;
}
int findPa(int n)
{
if(a[n]!=n) return a[n]=findPa(a[n]);
else return n;
}
void Union(int x,int y)
{
int xx=findPa(x);
int yy=findPa(y);
if(xx==yy) return;
else
{
a[xx]=yy;
result--;
return;
}
}
int main()
{
while(scanf("%d %d",&n,&m),n||m)
{
Init(n);
for(int i=0;m-i>0;i++)
{
scanf("%d %d",&x,&y);
Union(x,y);
}
printf("Case %d: %d\n",times++,result);
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator