题意:求并查集中最大的集合的数据的个数 /* Author: ACb0y Date: 2010-10-13 ProblemId: hdu 1856 More is better Type: Union-Find set Result: AC */ #include <iostream> using namespace std; struct Node { int root; int num; }; struct Seg { int u; int v; }; int n; int ans; Seg seg[100010]; Node node[10000010]; //获取根节点 int get_root(int a) { if (node[a].root == a) { return a; } else { //之前写成了 return get_root(node[a].root); //结果一直超时,最后发现是要对node[a].root的 //值进行更新要不查询根节点时递归深度会很大 int temp = get_root(node[a].root); //更新根节点 node[a].root = temp; return node[a].root; } } //并查集合并 void union_set(int a, int b) { int root_a = get_root(a); int root_b = get_root(b); if (root_a != root_b) { node[root_a].root = root_b; node[root_b].num += node[root_a].num; //更新最大值 if (node[root_b].num > ans) { ans = node[root_b].num; } } } int main() { #ifndef ONLINE_JUDGE freopen("1856.txt", "r", stdin); #endif while (scanf("%d", &n) != EOF) { int i; ans = 1; for (i = 0; i < n; i++) { scanf("%d %d", &seg[i].u, &seg[i].v); node[seg[i].u].root = seg[i].u; node[seg[i].u].num = 1; node[seg[i].v].root = seg[i].v; node[seg[i].v].num = 1; } for (i = 0; i < n; i++) { union_set(seg[i].u, seg[i].v); } printf("%d/n", ans); } return 0; }