标签:
题目地址:
http://poj.org/problem?id=1611
题目内容:
Time Limit: 1000MS | Memory Limit: 20000K | |
Total Submissions: 24253 | Accepted: 11868 |
Description
Input
Output
Sample Input
100 4 2 1 2 5 10 13 11 12 14 2 0 1 2 99 2 200 2 1 5 5 1 2 3 4 5 1 0 0 0
Sample Output
4 1 1
Source
#include <stdio.h> int man[30001]; int num[30001]; // 记录有几个子节点,但只有根记录的数据才算数 void init(int n) { for (int i = 0; i < n; i ++) { man[i] = -1; num[i] = 0; } } int find_root(int child) { if (man[child] == -1) return child; return man[child] = find_root(man[child]); } void union_set(int one, int two) { int fat1 = find_root(one); int fat2 = find_root(two); //printf("%d and %d has been union. ", fat1, fat2); if (fat1 == fat2) return; num[fat1] += num[fat2] + 1; man[fat2] = fat1; //printf("father is %d\n", fat1); } int main(void) { int m,n; while (scanf("%d%d", &n, &m)) { if (m == 0 && n == 0) break; init(n); for (int i = 0; i < m; i ++) { int count,pre,now; scanf("%d", &count); pre = -1; for (int j = 0; j < count; j ++) { scanf("%d", &now); if (pre != -1) { union_set(pre, now); } pre = now; } } int fa = find_root(0); printf("%d\n", num[fa] + 1); } return 0; }
【原创】poj ----- 1611 The Suspects 解题报告
标签:
原文地址:http://www.cnblogs.com/shadowmydx/p/4349163.html