标签:des style blog io color ar os sp for
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 20668 | Accepted: 10153 |
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
思路:没什么好说的 就是并查集.
代码如下:
#include <stdio.h> int s; int p[50050]; int find(int x)//找老大. { if(x!=p[x]) p[x]=find(p[x]); return p[x]; } void Union (int x,int y)//合并成同一派的老大唯一.不是老大就是老二! { int fx=find(x),fy=find(y); if(fx!=fy) { s--;//合并一次就减一个帮派. p[y]=p[x];//原来的老大变成现在的老二,小弟都变成老二 } else return; } int main() { int i,j; int s1,s2; int x,y,n,m; int t = 1; while(~scanf("%d %d",&n,&m)) { if(n==0&&m==0) break; s=n;//开始我们有n个帮派. for(i = 1; i <= n; i++) p[i] = i; for(i = 1; i <= m; i++) { scanf("%d %d",&x,&y); s1 = find(x); s2 = find(y); Union(s1,s2); } printf("Case %d: ",t); printf("%d\n",s); t++; } return 0; }
POJ 2524 (Ubiquitous Religions)
标签:des style blog io color ar os sp for
原文地址:http://www.cnblogs.com/ikids/p/4087964.html