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 |
请教牛人:我的并查集有错么?怎么老是超时?急急急!!!#include "stdio.h" int stu[50001]; int main() { int num=0; int cusor(int a); int a,b,i,j,k,m,n,sum,ta,tb; while(scanf("%d %d",&m,&n)==2&&m!=0&&n!=0){ num++; for(k=1;k<=50000;k++)stu[k]=k; sum=0; for(i=0;i<n;i++){ scanf("%d %d",&a,&b); if(stu[b]==b){stu[b]=a;} else if(stu[a]==a){stu[a]=b;} else{ ta=cusor(a); tb=cusor(b); stu[tb]=ta; } } for(j=1;j<=m;j++) if(stu[j]==j)sum++; printf("Case %d: %d\n",num,sum); } return 0; } int cusor(int a){ while(stu[a]!=a)a=stu[a]; return a; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator