poj1611 The Suspects 并查集_小优化
来源:互联网 发布:old town coffee淘宝 编辑:程序博客网 时间:2024/05/02 01:21
poj1611 The Suspects 并查集_小优化
标签:并查集
题目链接
原文出处
并查集基础
(1)
/* 可以通过压缩路径来优化,在查找到祖先节点之后, 将路径上所有它的子孙节点都直接连接到它上面,这样树的高度就大大降低了。*/#include <stdio.h>int num[30005]; //结点所在集合元素的个数int father[30005];void make_set(int x){ ////初始化集合 father[x] = -1; num[x] = 1;}int find_set(int x){ ////查找x元素所在集合 int r = x, temp; while(father[r] != -1) r = father[r]; //找到根节点 while(x != r){ //压缩路径,将路径上r所有的子孙都直接连接到r上 temp = father[x]; father[x] = r; x = temp; } return x;}void union_(int x, int y){ //合并x, y所在的集合 x = find_set(x), y = find_set(y); if(x == y) return ; //同一个集合则不需要合并 if(num[x] < num[y]) { //小集合合并到大集合 father[x] = y; num[y] += num[x]; } else{ father[y] = x; num[x] += num[y]; }}int main(){ int n, m, t, x, y, i, j; while(scanf("%d %d", &n, &m) && (n || m)){ for(i = 0; i < n; i++) make_set(i); for(i = 0; i < m; i++){ scanf("%d", &t); scanf("%d", &x); for(j = 1; j < t; j++){ scanf("%d", &y); union_(x, y); } } printf("%d\n", num[find_set(0)]); } return 0;}
(2)
/* 对于根节点来说,father[]本身就可以记录节点数目,只不过值是负的。*/#include <stdio.h>int father[30005];void make_set(int x){ //初始化集合 father[x] = -1;}int find_set(int x){ //查找x元素所在集合 int r = x, temp; while(father[r] > 0) r = father[r]; ////找到根节点 while(x != r){ //压缩路径,将路径上r所有的子孙都直接连接到r上 temp = father[x]; father[x] = r; x = temp; } return x;}void union_(int x, int y){ //合并x, y所在的集合 x = find_set(x), y = find_set(y); if(x == y) return ; //同一个集合则不需要合并 if(father[x] < father[y]) { ////小集合合并到大集合(负数比较), < father[x] += father[y]; father[y] = x; } else{ father[y] += father[x]; father[x] = y; }}int main(){ int n, m, t, x, y, i, j; while(scanf("%d %d", &n, &m) && (n || m)){ for(i = 0; i < n; i++) make_set(i); for(i = 0; i < m; i++){ scanf("%d", &t); scanf("%d", &x); for(j = 1; j < t; j++){ scanf("%d", &y); union_(x, y); } } printf("%d\n", -father[find_set(0)]); //结点数目为负值 } return 0;}
阅读全文
0 0
- poj1611 The Suspects 并查集_小优化
- POJ1611 The Suspects 并查集求解
- POJ1611 The Suspects 并查集
- poj1611 The Suspects (并查集)
- poj1611-The Suspects--并查集
- poj1611 The Suspects(并查集)
- POJ1611 The Suspects (并查集)
- 并查集 poj1611 The Suspects
- POJ1611 The Suspects 并查集
- poj1611 The Suspects(基础并查集)
- poj1611 The Suspects 并查集
- POJ1611:The Suspects【并查集】
- poj1611 The suspects 并查集
- 【并查集】:poj1611,The Suspects
- POJ1611 The Suspects(并查集)
- poj1611 the-suspects 并查集
- POj1611:The Suspects(并查集)
- POJ1611 The Suspects 种类并查集
- JAVA 传不定长度的参数
- poj1611 The Suspects 并查集
- JavaScript OOP(二):this关键字以及call、apply、bind
- 带你理解CycleGAN,并用TensorFlow轻松实现
- HDU
- poj1611 The Suspects 并查集_小优化
- 使用RancherServer:v1.6.12部署K8S-v1.8.3
- LDAP入门
- 第十五周训练总结(一)
- 扩展欧几里德算法
- Circular Buffer of Raw Binary Data in C++
- 如何处理僵死进程?
- Linux虚拟机:Centos详细安装及配置步骤
- P1008 ——1:2:3数字组合