并查集代码总结
来源:互联网 发布:学完c语言 编辑:程序博客网 时间:2024/06/02 00:55
核心代码:
int find(int x){ while(x != root[x])/*循环寻找x对应根的值*/ { x = root[x]; } return x;}void unio(int a, int b)/*合并a所在的树的根,和b所在的树的根*/{ int x, y; x = find(a);/*求a的根*/ y = find(b);/*求b的根*/ if(x != y) { root[y] = x;/*让y的根等于x*/ }}
题目:小鑫的城堡http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Contest/contestproblem/cid/2014/pid/2798.html
#include<stdio.h>#include<stdlib.h>#include<string.h>int root[100555], flag;int house[100555];int find(int x){ while(x != root[x]) { x = root[x]; } return x;}void unio(int a, int b){ int x, y; x = find(a); y = find(b); if(x != y) { root[y] = x; } else flag = 0;//判断是否回路}int max1(int a, int b){ if(a < b) return b; else return a;}int main(){ int m, a, b, i, max, num; while(~scanf("%d", &m)) { max = 0; num = 0; memset(house, 0, sizeof(house)); flag = 1; for(i = 1; i <= 100005; i++)//初始化根 root[i] = i; for(i = 0; i < m; i++) { scanf("%d %d", &a, &b); if(max < max1(a, b))//找出最大房间号 max = max1(a, b); house[a]++; house[b]++; unio(a, b);//合并 } for(i = 1; i <= max; i++) { if(house[i]) num++;//统计房间数 } if(flag && num == m + 1) printf("Yes\n");//图满足房间数等于路径数加一 && 不允许有回路 else printf("No\n"); } return 0;}
0 0
- 并查集代码总结
- 并查集代码
- 并查集代码
- 并查集总结
- 并查集总结
- 并查集总结
- 并查集总结
- 并查集总结
- 并查集--总结
- 并查集总结
- 并查集总结
- 并查集总结
- 并查集 总结
- 并查集总结
- 并查集总结
- 并查集总结
- 并查集总结
- 并查集总结
- Docker1.13.1在 Ubuntu 16.04 上的安装和配置
- 我眼中的设计模式(2)
- Java-日志
- std::runtime_error 异常传递失败
- 简单工厂模式(静态工厂方法(Static Factory Method)模式)
- 并查集代码总结
- 伙伴系统的概述
- LeetCode 3. Longest Substring Without Repeating Characters
- VMWare 安装 Eclipse
- 【event】tx:allocate ITL entry
- JSP内置对象response常见用法
- 传统定时器Timer
- 聊天机器人相关中文资料
- Node.js中mkdir方法的使用说明