并查集 - 模板
2017-03-23 18:23
387 查看
class DisjointSet { private: int *father, *rank; public: DisjointSet(int size) { father = new int[size]; rank = new int[size]; for (int i = 0; i < size; ++i) { father[i] = i; rank[i] = 0; } } ~DisjointSet() { delete[] father; delete[] rank; } int find_set(int node) { if (father[node] != node) { father[node] = find_set(father[node]); } return father[node]; } bool merge(int node1, int node2) { int ancestor1 = find_set(node1); int ancestor2 = find_set(node2); if (ancestor1 != ancestor2) { if (rank[ancestor1] > rank[ancestor2]) { swap(ancestor1, ancestor2); } father[ancestor1] = ancestor2; rank[ancestor2] = max(rank[ancestor2], rank[ancestor1] + 1); return true; } return false; } int find_father(int node) { return father[node]; } };
2017 - 3 - 23
NOIP Day 232
相关文章推荐
- 并查集 disjoint set模板
- hdu1232 畅通工程(并查集模板)
- 【并查集模板】 【洛谷P2978】 【USACO10JAN】下午茶时间
- HDU 1232 并查集模板题
- 并查集模板
- P3367 【模板】并查集
- POJ 2524 宗教信仰 并查集 基础模板
- 并查集的模板
- 并查集之找祖先的模板
- 并查集模板
- HDOJ 题目3635Dragon Balls(带权值的并查集,模板)
- 【模板】并查集 压缩路径
- Hdu 1232 畅通工程(并查集模板题)
- C++-并查集模板
- 2012-2013 ACM-ICPC, Asia Tokyo Regional Contest F.Never Wait for Weights(带权并查集模板)
- 并查集总结【模板】 例题:①简单POJ - 1611 The Suspects ②一般HDU - 1272 小希的迷宫
- 并查集简单模板
- 【模板】并查集 两种路径压缩写法
- 并查集模板
- Disjointset 并查集(按秩合并,与路径压缩)的模板