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的更多相关文章

  1. [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), ...

  2. 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), ...

  3. [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 ...

  4. [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), ...

  5. [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), ...

  6. 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), ...

  7. LeetCode Graph Valid Tree

    原题链接在这里:https://leetcode.com/problems/graph-valid-tree/ 题目: Given n nodes labeled from 0 to n - 1 an ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. LTP 分词算法实践

    参考链接: https://github.com/HIT-SCIR/ltp/blob/master/doc/install.rst http://www.xfyun.cn/index.php/serv ...

  2. HTML5 WebAudioAPI简介(一)

    一.常用对象 1.AudioContext对象 AudioContext是一个专门用于音频处理的接口,并且原理是讲AudioContext创建出来的各种节点(AudioNode)相互连接,音频数据流经 ...

  3. (转)smarty实现多级分类的方法

    --http://www.aspku.com/kaifa/php/44679.html 这篇文章主要介绍了smarty实现多级分类的方法,涉及循环读取的技巧,非常具有实用价值,需要的朋友可以参考下   ...

  4. Chrome浏览器允许跨域请求配置

    最近有个做数据标注的任务,但是标注平台是别人公司的,他们又不愿意对平台进行升级改造: 其实要改的地方也很简单,就是对页面做一些处理,做一些脚本控制. 没办法,做了个 iframe 给她嵌入到我们自己的 ...

  5. xceed wpf datagrid

    <!--*********************************************************************************** Extended ...

  6. 掌握JS

    1.原生的js,好比全真教的武功,一步步从基础开始(先练气再御剑),很长一段时间内和jquery有很大差距,掌握以后发现jquery只不过是另外一种武功,看一遍既会.且当学原生到一定程度之后,可以自创 ...

  7. 将fastjson元素转化为String[]

    在fastjson中如果JSONObject中添加了 String[] 类型的元素 例如 JSONObject jo = new JSONObject(); String[] array = {&qu ...

  8. Linux内存点滴:用户进程内存空间

    原文出处:PerfGeeks 经常使用top命令了解进程信息,其中包括内存方面的信息.命令top帮助文档是这么解释各个字段的.VIRT , Virtual Image (kb)RES, Residen ...

  9. overflow第一次觉得你有点可恶

    今天用css做下拉菜单,因为不需要做手机自适应,再手机里看起来工整一点就行,可是列表中最后一个li的宽度撑开了父div,导致看起来很糟糕,所以给父元素加overflow:hidden:但是下拉列表也被 ...

  10. HTML5 <Canvas>文字粒子化

    文字粒子化,额或者叫小圆圈化... 1 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> ...