标签:des style blog http color os
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 23390 | Accepted: 11527 |
Description
Input
Output
Sample Input
10 9 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 10 4 2 3 4 5 4 8 5 8 0 0
Sample Output
Case 1: 1 Case 2: 7
Hint
Source
#include <stdio.h> #include <string.h> int pre[50050]; int find(int n) { int i,t; t = n; while(t!=pre[t]) t=pre[t]; while(t!=pre[n]) { i = pre[n]; pre[n]=t; n = i; } return t; } int main() { int n,m,k=1; while(scanf("%d%d",&n,&m),n||m) { int i,j,a,b,pa,pb,total=n; //memset(s,0,sizeof(s)); for(i=0;i<=n;i++) pre[i]=i; for(i=0;i<m;i++) { scanf("%d%d",&a,&b); pa=find(a); pb=find(b); if(pa!=pb) { pre[pa]=pb; total--; } } printf("Case %d: ",k++); printf("%d\n",total); } return 0; }
并差集的简单应用
poj_2524_Ubiquitous Religions_201407211506,布布扣,bubuko.com
poj_2524_Ubiquitous Religions_201407211506
标签:des style blog http color os
原文地址:http://www.cnblogs.com/xl1027515989/p/3858500.html