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 |
这段 329MSIn 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; int a[SIZE]={0}; bool b[SIZE]={false}; void Init(int n) { for(int i=0;n-i>0;i++) a[i]=i,b[i]=false; } int findPa(int n) { if(a[n]!=n) return a[n]=findPa(a[n]); else return n; } void Union(int x,int y) { a[findPa(x)]=findPa(y); } int Count(int n) { int count=0; for(int i=0;n-i>0;i++) { b[findPa(i)]=true; } for(int i=0;n-i>0;i++) { if(b[i]) count++; } return count; } void printInfo(int a[],int n) { for(int i=0;n-i>0;i++) cout<<a[i]<<" "; cout<<endl; } 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); } // printInfo(a,n); printf("Case %d: %d\n",times++,Count(n)); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator