标签:
题目地址:
http://poj.org/problem?id=2524
题目内容:
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 26119 | Accepted: 12859 |
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> int set[50001]; int n,m; int find_father(int son) { if (set[son] == 0) { return son; } return set[son] = find_father(set[son]); } void init() { for (int i = 0; i < n; i ++) { set[i] = 0; } } int main(void) { int count = 1; int man1,man2; while (scanf("%d%d", &n, &m)) { if (m == 0 && n == 0) break; int res = n; for (int i = 0; i < m; i ++) { scanf("%d%d", &man1, &man2); int fat1 = find_father(man1); int fat2 = find_father(man2); if (fat1 != fat2) { set[fat2] = fat1; res --; // 合并一次,减少一个集合 } } printf("Case %d: %d\n", count, res); init(); count ++; } return 0; }
【原创】poj ----- 2524 Ubiquitous Religions 解题报告
标签:
原文地址:http://www.cnblogs.com/shadowmydx/p/4348765.html