LeetCode 261. Graph Valid Tree
来源:互联网 发布:今天淘宝怎么进不去了 编辑:程序博客网 时间:2024/05/18 06:20
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
.
Hint:
- Given
n = 5
andedges = [[0, 1], [1, 2], [3, 4]]
, what should your return? Is this case a valid tree? Show More Hint
Note: you can assume that no duplicate edges will appear in edges
. Since all edges are undirected, [0, 1]
is the same as [1, 0]
and thus will not appear together in edges
.
Method 1: Union-Find Algorithm
#include <vector>#include <iostream>using namespace std;// this is actually to use union-find.bool find(vector<int>& id, int p, int q) { return id[p] == id[q];}int root(vector<int>& id, int p) { while(p != id[p]) { p = id[p]; } return p;}void unite(vector<int>& id, int p, int q) { int root_p = root(id, p); int root_q = root(id, q); id[root_p] = root_q;}bool isValidTree(int n, vector< vector<int> >& edges) { if(n < 0 || (n - 1) != edges.size()) return false; vector<int> id(n); for(int i = 0; i < n; ++i) { id[i] = i; } for(int i = 0; i < edges.size(); ++i) { if(find(id, edges[i][0], edges[i][1])) { return false; } else { unite(id, edges[i][0], edges[i][1]); } } return true;}int main(void) { vector< vector<int> > edges{{0, 1}, {0, 2}, {0, 3}, {1, 4}}; vector< vector<int> > edges_1{{0, 1}, {1, 2}, {2, 3}, {1, 3}, {1, 4}}; cout << isValidTree(5, edges) << endl; cout << isValidTree(5, edges_1) << endl;}
If could't think up Union-Find method, it is also Ok to to use DFS to detect cycle. If cycle found, that is not a valid graph tree.
Step:
1: first build up hashmap hashmap[first].push_back(second), hashmap[second].push_back(first)
2: Make all the node unvisited.
3: Loop all the node, if found an unvisited node, using dfs to detect cycle. Note: here we need to pass the parent node in to dfs function
bool dfs(nodesMap, currNode, parentNode, visited) {
visited[curreNode] = true;
for(v : edges) {
if(unvisited) {if(dfs(nodesMap, v, currentNode, visited)) return true;}
else if( *v != parentNode) return ture // cycle detected
}
return false // there is no cycle.
}
if(dfs) return false // cycle in the undirected graph, that is not a valid tree.
Beside checking whether there is a cycle, we also need to check whether the given nodes forms a forest.
- Leetcode - 261. Graph Valid Tree
- LeetCode 261. Graph Valid Tree
- Leetcode 261. Graph Valid Tree
- LeetCode 261. Graph Valid Tree
- Leetcode 261. Graph Valid Tree
- [Leetcode]Graph Valid Tree
- leetcode Graph Valid Tree
- Leetcode Graph Valid Tree
- LeetCode Graph Valid Tree
- [leetcode] 261. Graph Valid Tree 解题报告
- [Leetcode] 261. Graph Valid Tree 解题报告
- 261. Graph Valid Tree
- 261. Graph Valid Tree
- 261. Graph Valid Tree
- LeetCode 题解(246) : Graph Valid Tree
- Graph Valid Tree
- [LeetCode261] Graph Valid Tree
- lintcode:Graph Valid Tree
- Java对象的4中引用类型
- 遍历DataTable内存数据的三种方法性能对比
- 享元模式
- HDU 2016 数据的交换输出
- 分形之美
- LeetCode 261. Graph Valid Tree
- su root 和su - root 的区别
- 第13周-阅读程序(2)
- 字体颜色对话框
- SICP chapter2 put get
- Database: Faces & Sketchs 人脸识别数据集
- Java的八种基本数据类型
- Android屏幕自适应研究
- Java的SPI(Service Provider Interface)规范