本文主要是介绍POJ 1611 The Suspects(带权并查集),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
带权表示集合总和。
代码:
#include <stdio.h>
#include <string.h>const int N = 30005;int parent[N], sum[N], n, m;int find(int x) {if (x == parent[x])return x;return parent[x] = find(parent[x]);
}void init() {for (int i = 0; i < n; i++) {parent[i] = i;sum[i] = 1;}
}int main() {while (~scanf("%d%d", &n, &m) && n || m) {int num, a, b;init();while (m--) {scanf("%d", &num);if (num > 0) {scanf("%d", &a);num--;}while (num--) {scanf("%d", &b);int pa = find(a);int pb = find(b);if (pa != pb) {parent[pa] = pb;sum[pb] += sum[pa];}}}printf("%d\n", sum[find(0)]);}return 0;
}
这篇关于POJ 1611 The Suspects(带权并查集)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!