lintcode178. graph valid tree 图是否是树
来源:互联网 发布:淘宝客服要开通管理 编辑:程序博客网 时间:2024/06/07 06:59
【题目】
给出 n 个节点,标号分别从 0 到 n - 1 并且给出一个 无向 边的列表 (给出每条边的两个顶点), 写一个函数去判断这张`无向`图是否是一棵树
假设我们不会给出重复的边在边的列表当中. 无向边 [0, 1] 和 [1, 0] 是同一条边, 因此他们不会同时出现在我们给你的边的列表当中。
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.
【样例】
给出n = 5
并且 edges = [[0, 1], [0, 2], [0, 3], [1, 4]]
, 返回 true.
给出n = 5
并且 edges = [[0, 1], [1, 2], [2, 3], [1, 3], [1, 4]]
, 返回 false.
1. 如果图中边的个数不等于点的个数n-1,必然不为树
2. 如果有边的两个点属于同一个子树,则这个图不为树。
如何判断两个点是否属于同一个子树?
将有连边的两个点放在同一集合中(高大上的并查集)。可以使用长度为n的一维数组existed表示点之间的关系,existed初始化为-1。对于每条边连接的两个点,通过判断两个点在existed数组中的值判断点是否在图中出现过,点所属的集合,并进行处理。
1. 如果两个点处的值均为-1,表示两个点都没出现过,将两个点在existed中的值置为较小的点,表示两个点所在的集合;
2. 如果一个点在图中出现过,一个点未出现过,将未出现过的点在existed中对应的值置为已出现过的点对应的值,相当于更新了点的集合;
3. 如果两个点都出现过,合并两个点所在集合,将两个集合中点在existed中对应的值都换为值较小的集合。
4. 如果两个点属于同一个集合,图不为树。
AC代码如下(64ms)
class Solution {public: /** * @param n an integer * @param edges a list of undirected edges * @return true if it's a valid tree, or false */ bool validTree(int n, vector<vector<int>>& edges) { // Write your code here //使用n维向量保存某个点是否出现在图中,遍历edges,将有连边的点放入一个集合,如果两个点都已经在一个集合中,返回false,existed记录点是否已经存在在图中 //如果边的个数大于点的个数,返回false if (n - edges.size() != 1) return false; //属于同一个集合的点都用数值小的点标识集合 vector<int> existed(n, -1); for (int i = 0; i < edges.size(); ++i) { int node1 = edges[i][0]; int node2 = edges[i][1]; //两个点属于同一集合 if (existed[node1] == existed[node2]) { if (existed[node1] == -1) //都未出现过,更新existed existed[node1] = existed[node2] = min(node1, node2); else return false; } else { //两个点都出现过,合并点所在集合 if (existed[node1] != -1 && existed[node2] != -1) { int min = existed[node1]; int max = existed[node2]; if (existed[node1] > existed[node2]) swap(min, max); //将两个集合合并 for (int j = 0; j < n; ++j) { if (existed[j] == max) existed[j] = min; } } else { //只有一个点出现过 if (existed[node2] == -1) existed[node2] = existed[node1]; else existed[node1] = existed[node2]; } } } return true; }};对于一组数据,以上代码的执行过程如下表所示:
existed
0
1
2
3
4
edges
更新existed
-1
-1
-1
-1
-1
[0, 1]
existed[0] = 0
existed[1] = 0
0
0
-1
-1
-1
[1, 2]
existed[2]= 0
0
0
0
-1
-1
[3, 4]
existed[3]= 3
existed[4]= 3
0
0
0
3
3
[1, 3]
existed[3]= 0
existed[4]= 0
-1
0
0
0
0
[2, 4]
existed[2]==existed[4]
-1
0
0
0
0
存在问题:
合并两个集合时,需要多次遍历existed数组,将点所对应位置的值替换,产生了冗余。如[3, 4]加入时,3,4对应位置都变为3,再加入[1,3]边时,需要遍历数组,找到对应值为3的位置,将3换成0。
改进办法:
通过递归实现深度遍历,每次都在existed中寻找点的父节点,如果两个点具有相同的父节点,则图不为树。
AC代码(56ms):
class Solution {public: /** * @param n an integer * @param edges a list of undirected edges * @return true if it's a valid tree, or false */ int find(vector<int> &existed, int e){ //点未在图中出现过,返回该点;否则,找到该点的父节点 if (existed[e] == -1) return e; else return find(existed, existed[e]); } bool validTree(int n, vector<vector<int>>& edges) { // Write your code here //如果边的个数不等于点的个数减一,返回false if (n - edges.size() != 1) return false; vector<int> existed(n, -1); for (int i = 0; i < edges.size(); ++i) { int root1 = find(existed, edges[i][0]); int root2 = find(existed, edges[i][1]); if (root1 == root2) return false; //将两个点关联 existed[root2] = root1; } return true; }};对于相同一组数据,执行过程如下表所示:
existed
0
1
2
3
4
edges
调用find
更新existed
-1
-1
-1
-1
-1
[0, 1]
find(0) = 0
existed[1] = 0
-1
0
-1
-1
-1
find(1) = 1
[1, 2]
find(1) = find(0) = 0
existed[2] = 0
-1
0
0
-1
-1
find(2) = 2
[3, 4]
find(3) = 3
existed[4] = 3
-1
0
0
-1
3
find(4) = 4
[1, 3]
find(1) = find(0) = 0
existed[3] = 0
-1
0
0
0
3
find(3) = 3
[2, 4]
find(2) = find(0) = 0
find(2) == find(4)
false
-1
0
0
0
3
find(4) = find(3) = find(0) = 0
【小结】
1. 使用一维数组表示图的有向边的指向,简单直观,节省空间,只需要开辟大小为n的数组,并且规定每个点对应位置的值为父节点即可。方便通过深度遍历的方法迅速找到当前点的父节点;
2. 第一种方法在使用数组时,将属于同一根节点的点表示为一个集合,具体表现为这些点对应位置的值都替换为集合中当前最小的结点,每次更新数组时需要先比较两个节点对应位置的大小,并且合并两个集合时都需要遍历数组;
3. 改进后的方法,避免了比较大小的操作和对数组的反复遍历,保证每次更新操作只改变一个结点对应位置的值,将该点对应位置处的值改为当前能找到的最高父节点,但find函数递归实现深度遍历,需要不断压栈,弹栈,产生额外地时间,空间消耗,可以将递归改为循环,避免栈的开销。改进后的代码也更简洁,易读。
int find(vector<int> &existed, int e){ //点未在图中出现过,返回该点;否则,找到该点的父节点 while (existed[e] != -1) { e = existed[e]; } return e;}
- lintcode178. graph valid tree 图是否是树
- Lintcode178 Graph Valid Tree solution 题解
- lintcode graph-valid-tree 图是否是树
- *[Lintcode] Graph Valid Tree 图是否是树
- 178.Graph Valid Tree-图是否是树(中等题)
- LeetCode 261. Graph Valid Tree(判断图是否为树)
- [Leetcode]Graph Valid Tree
- Graph Valid Tree
- [LeetCode261] Graph Valid Tree
- 261. Graph Valid Tree
- lintcode:Graph Valid Tree
- Graph Valid Tree
- LintCode_178 Graph Valid Tree
- Graph Valid Tree
- leetcode Graph Valid Tree
- Leetcode Graph Valid Tree
- LeetCode Graph Valid Tree
- Graph Valid Tree
- swift 当需要改变cell的内容布局时
- 123452345678
- 小方法1——CountChar--统计一个字符串中每个字符出现的次数
- Hibernate关联映射之一对多,多对一以及双向一对多
- 量化分析师的Python日记
- lintcode178. graph valid tree 图是否是树
- POJ 1753 Flip Game 棋盘状态搜索
- JUnit4学习笔记
- jzoj 2548. 【NOIP2011模拟9.4】最大正方形
- mipi差分信号原理
- html+js的遮罩实现
- OPENCV入门教程二十:Canny边缘检测
- Android 如何通过menu id来得到menu item 控件
- Javaweb异常提示信息统一处理