[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 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 and edges = [[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.
分析:
首先,由于类似[0, 1]和[1, 0]这样的对不会同时出现,故树的必要条件是edges.size() == n -1,不满足这个条件的直接false;当满足这个条件时,再判断图中是否有环,或者两个及以上的独立图,两种方法都行,我采用的是后者。
代码:
void dfs(int i, unordered_multimap<int, int> &hash, vector<bool> &visited) {
visited[i] = true;
auto pospair = hash.equal_range(i);
auto pos = pospair.first;
while(pos != pospair.second) {
if(!visited[pos->second]) {
visited[pos->second] = true;
dfs(pos->second, hash, visited);
}
pos++;
}
return;
}
bool validTree(int n, vector<vector<int> > &edges) {
if(edges.size() != n - )
return false;
vector<bool> visited(n, false);
unordered_multimap<int, int> hash;
//映射到hashmap中,便于访问
for(auto e : edges) {
hash.insert(make_pair(e[], e[]));
hash.insert(make_pair(e[], e[]));
}
//从任意一个点扩展,看是否全连通。全连通则为真,不全连通则为假
dfs(, hash, visited);
for(bool vd : visited)
if(!vd)
return false;
return true;
}
[Locked] Graph Valid Tree的更多相关文章
- [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: 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 ...
- [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), ...
- [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), ...
- 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 ...
- 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 ...
- [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 ...
随机推荐
- 把Excel数据导入到数据库
引入命名空间 using System.IO; using System.Data; using System.Data.OleDb; 引入命名空间 首先要把Excel上传到服务器 //上传Excel ...
- ASP.NET 3.5路由总结篇
URL Routing是非常重要的一块技术体系,笔者将URL Routing的知识进行梳理后得出本文,旨在同大家分享,希望能够起到抛砖引玉的作用. 1. 什么是URL Routing? 所谓UR ...
- 解读oracle执行计划-待续
Cost(%CPU): 优化器估算出完成当前操作的代价(包含子操作的代价),它是IO代价和CPU 代价总和.其中IO代价是最基本的代价.而对于CPU代价,在默认情况下,优化器会将CPU代价计算在内,而 ...
- iOS、mac开源项目及库汇总
原文地址:http://blog.csdn.net/qq_26359763/article/details/51076499 iOS每日一记------------之 中级完美大整理 iOS.m ...
- inno setup 多语言安装
之前的安装程序默认语言为英文,现在我们需要将它变成中文,由于InnoSetup安装包中默认没有带中文语言文件,我们需要下载一个先: 到http://www.400gb.com/u/758954/123 ...
- Long型整数,缄默溢出
/** 长整数问题 @author husky */ public class LongDemo { public static void main(String[] args) { /** * 问题 ...
- 一种实现C++反射功能的想法(三)
如何实现类型名跟类型的对应, 我们很容易想到map, 没错, 就是使用map实现的. std::map<std::string, .....>, 等下, 第二部分该填什么类型, 一个函数指 ...
- 给表格设置border还可以这样玩
<table width="100%" border="0" cellpadding="0" cellspacing="1& ...
- jquery动画效果中,避免持续反应用户的连续点击
一.某些动画效果中,避免持续连续反应用户的连续点击(这标题真不好描述) 意思就是指用户点击速度很快,完成一次效果的时间不能很快结束的话,就会出现用户不点击了,效果还在持续.看下面例子就明白了,手风琴效 ...
- asp.net在应用母版的页面下采用了ModalPopupExtender弹出窗中应用autocomplete
autocomplete是jqueryUI的一个插件,可以实现自动填充的功能. 要点:1.应用了母版页,所以取页面上控件的ID时与一般方法不同 2.由于用了ajax的updatepanel,所以会出现 ...