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 |
100题~贴个代码纪念一下#include<stdio.h> int pre[50005]; int find(int x){ int r,t,z; r=t=x; while(r!=pre[r]) r=pre[r]; while(t!=r){// 压缩路径 z=pre[t]; pre[t]=r; t=z; } return r; } int main() { int n,m,i,j,a,b,total,fa,fb,num; num=0; while(scanf("%d%d",&n,&m)&&n){ num++; total=n; for(i=1;i<=n;i++) pre[i]=i; for(i=1;i<=m;i++){ scanf("%d%d",&a,&b); fa=find(a),fb=find(b); if(fa!=fb){ pre[fa]=fb; total--; } } printf("Case %d: %d\n",num,total); } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator