标签:des style blog color strong os
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 22980 | Accepted: 11336 |
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
又是一道并查集的基础题吧。就是给你n个人,每个人都有一个宗教信仰,告诉你谁和谁有相同的宗教信仰,要你求有几种不同的宗教信仰。
#include <stdio.h> int father[500005], num[500005]; void MakeSet(int n) { for(int i = 1; i<=n; i++) { father[i] = i; num[i] = 0; } } int FindSet(int x) { if(x != father[x]) { father[x] = FindSet(father[x]); } return father[x]; } void UnionSet(int a, int b) { int x = FindSet(a); int y = FindSet(b); if(x == y) return ; if(num[x] >= num[y]) { father[y] = x; num[x] += 1; } else { father[x] = y; num[y] += 1; } } int main() { int n, m, k = 1; int a, b, sum; while(scanf("%d%d", &n, &m)!=EOF && n && m) { MakeSet(n); while(m--) { scanf("%d%d", &a, &b); UnionSet(a, b); } sum = 0; for(int i = 1; i<=n; i++) sum += num[i]; printf("Case %d: %d\n", k++, n-sum); } }
poj 2524 Ubiquitous Religions,布布扣,bubuko.com
标签:des style blog color strong os
原文地址:http://www.cnblogs.com/fengxmx/p/3833259.html