Graph Valid Tree -- LeetCode
Given n
nodes labeled from 0
to n - 1
and a list of undirected edges (each edge is a pair of nodes), write a function to check whether these edges make up a valid tree.
For example:
Given n = 5
and edges = [[0, 1], [0, 2], [0, 3], [1, 4]]
, return true
.
Given n = 5
and edges = [[0, 1], [1, 2], [2, 3], [1, 3], [1, 4]]
, return false
.
思路:union find。
是一个树的条件有两个:无环,边数等于节点数减一。
检测无环可以用union find:枚举所有的边,检测并更新他们端点所属的集合。一开始所有的节点都属于各自的集合,然后merge每个边两端点所在的集合。因为树中的点都是连在一起的,最后肯定会在一个集合。注意的是,如果有一条边,一旦将它添加到树中后就会构成一个环,那么这条边的两个端点一定之前已经被添加进了树中(两个点属于同一个集合),否则,每条新加的边,至少有一个端点是之前不存在于这棵树中的(两个点属于不同的集合)。
class Solution {
public:
int find(vector<int> &parent, int x) {
if (parent[x] == x) return x;
int pa = find(parent, parent[x]);
parent[parent[x]] = pa;
return pa;
}
void merge(vector<int> &parent, int x, int y) {
int parentX = find(parent, x);
int parentY = find(parent, y);
if (parentX != parentY)
parent[parentY] = parentX;
}
bool validTree(int n, vector<pair<int, int>>& edges) {
vector<int> parent(n, );
for (int i = ; i < n; i++) parent[i] = i;
for (int i = ; i < edges.size(); i++) {
if (find(parent, edges[i].first) == find(parent, edges[i].second))
return false;
merge(parent, edges[i].first, edges[i].second);
}
return n - == edges.size();
}
};
Graph Valid Tree -- LeetCode的更多相关文章
- [Locked] Graph Valid Tree
Graph Valid Tree Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is ...
- [LeetCode] Graph Valid Tree 图验证树
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- [LeetCode] 261. Graph Valid Tree 图是否是树
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- LeetCode Graph Valid Tree
原题链接在这里:https://leetcode.com/problems/graph-valid-tree/ 题目: Given n nodes labeled from 0 to n - 1 an ...
- Leetcode: Graph Valid Tree && Summary: Detect cycle in undirected graph
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- [LeetCode#261] Graph Valid Tree
Problem: Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair o ...
- [LeetCode] 261. Graph Valid Tree _ Medium tag: BFS
Given n nodes labeled from 0 to n-1 and a list of undirected edges (each edge is a pair of nodes), w ...
- [Swift]LeetCode261.图验证树 $ Graph Valid Tree
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- 261. Graph Valid Tree
题目: Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nod ...
随机推荐
- ES6数组去重及ES5数组去重方法
ES6中新增了Set数据结构,类似于数组,但是 它的成员都是唯一的 ,其构造函数可以接受一个数组作为参数,如: let array = [1, 1, 1, 1, 2, 3, 4, 4, 5, 3]; ...
- vue_使用npm搭建vue2.0脚手架开发环境
前言: 在使用vue进行开发时需要搭建vue的运行环境,这里主要是使用淘宝镜像cnpm进行搭建vue的脚手架开发环境.主要是分为mac和window两个版本,两个环境的搭建都是大同小异. mac开发环 ...
- Tornado 安装及简单程序示例
1.安装步骤:tar xvzf tornado-3.2.tar.gz cd tornado-3.2 python setup.py build sudo python setup.py install ...
- 解决不走onActivityResult方法
最近在开发公司项目,在使用startActivityForResult关联俩个Activity中,发现A跳转到B,B设置setResult之后,A没有执行onActivityResult,查找一下,发 ...
- Python第三方库SnowNLP(Simplified Chinese Text Processing)快速入门与进阶
简介 github地址:https://github.com/isnowfy/snownlp SnowNLP是一个python写的类库,可以方便的处理中文文本内容,是受到了TextBlob的启发而写的 ...
- linux下进行base64编码解码
1.编码 2.解码
- 安全测试===sqlmap(叁)转载
十五.操作系统控制 1.执行任意操作系统命令 参数:--os-cmd和--os-shell 若数据库管理系统是MySQL.PostgreSQL或微软的SQL Server且当前用户有相关权限Sqlma ...
- VPS性能测试(1):CPU物理个数、内核、超线程、多核心
1.登录VPS界面,执行:cat /proc/cpuinfo,就会显示出VPS主机的CPU详细参数,如内核.频率.型号等等 2.主要参数physical_id表示物理CPU个数,cpu cores是内 ...
- gunicorn 启动无日志
gunicorn -c gunicorn_info.py info:app 接手整理老项目,发现有个服务迁移后启动不了,也没报错信息 修改gunicorn_info.py里的daemon = not ...
- 深度学习方法(六):神经网络weight参数怎么初始化
欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 技术交流QQ群:433250724,欢迎对算法.技术感兴趣的同学加入. 神经网络,或者深度学习算法的参数初始 ...