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 ...
随机推荐
- Java多线程学习(五)线程间通信知识点补充
系列文章传送门: Java多线程学习(二)synchronized关键字(1) Java多线程学习(二)synchronized关键字(2) Java多线程学习(三)volatile关键字 Java多 ...
- HDU 6119 小小粉丝度度熊 双指针
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6119 题意:中文题面. 解法:先处理可能交叉的区间,然后容易发现满足双指针的特性. //HDU 611 ...
- lsb_release查看当前系统的发行版信息
Linux除了用uname -r查看系统版本信息外,还可以用lsb_release. 安装: yum install -y redhat-lsb-core 使用: lsb_release -a
- Python-生成器/你不知道的点
1.什么是生成器 通过列表生成式,我们可以直接创建一个列表.但是,受到内存限制,列表容量肯定是有限的.而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要访问前面几个元素, ...
- 初识ES6
1.ECMAScript的官网地址:http://www.ecma-international.org/cma-262/6.0/,其是JS语言的下一代标准,已经在2015年6月正式发布,目标是让JS可 ...
- ASPxTreeList的右键按钮事件
ASPxTreeList应该是比较长用的控件了~现在就来说说它的右键按钮事件 这里实现的是右键里有折合和展开所有节点的功能 code: <dx:ASPxTreeList ID="ASP ...
- java - 线程1打印1-10,当线程打印到5后,线程2打印“hello”,然后线程1继续打印
public class T { private static int a =1;//1代表线程1 2线程2 public static void main(String[] args) { fina ...
- CF1064 E - Dwarves, Hats and Extrasensory Abilities
题意 交互题, 本来应该是在平面上进行的. 实际上换成一条直线就可以, 其实换成在平面上更复杂一些. Solution 假设\(l\)点是黑点, \(r\)处是白点, 那么就把下一个点的位置放置在\( ...
- springmvc3 拦截器,过滤ajax请求,判断用户登录,拦截规则设置
web.xml设置:(/拦截所有请求) <servlet> <servlet-name>dispatcher</servlet-name> <servlet- ...
- HDU-2389
Rain on your Parade Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 655350/165535 K (Java/Ot ...