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. 关于Android4.x系统默认显示方向各种修改

    1.设置属性值 在device.mk文件中加入PRODUCT_PROPERTY_OVERRIDES += \ ro.sf.hwrotation=180 2.设置屏幕默认显示方向 在frameworks ...

  2. Shell case正则匹配法

    Shell case正则匹配法   case $BOOLEAN in [yY][eE][sS]) echo 'Thanks' $BOOLEAN ;; [yY]|[nN]) echo 'Thanks' ...

  3. iOS 集成支付宝遇到的问题(续)

    调起支付宝进行支付时提示private key is null ,碰到这个问题有两种解决方案 第一种. 将私钥转成PKCS8替换一下原私钥即可 1.生成私钥pem,  执行命令openssl genr ...

  4. JavaScript_变量的自动转换和语句

    JS自动类型转换 var a = 1; var b = true; "==" 表示 可以自动类型转换,比较的是数值 "===" 表示可以自动类型转换,先比较数值 ...

  5. c++ undefined reference to mysqlinit

    Solved g++ $(mysql_config --cflags) file.cpp -o filename $(mysql_config --libs)

  6. Bootstrap_表单_表单提示信息

    平常在制作表单验证时,要提供不同的提示信息.在Bootstrap框架中也提供了这样的效果.使用了一个"help-block"样式,将提示信息以块状显示,并且显示在控件底部. < ...

  7. 如何去掉textarea右下角的灰色角标?

    在css中定义: resize: none; ,这个样式同时禁用textarea调整大小

  8. 如何将eclipse里的项目发布到github

    首先,给eclipse安装上EGit 在“Help > Install new software”中添加 http://download.eclipse.org/egit/updates 两个都 ...

  9. 史上最强NDK入门项目实战

    目标: 利用NDK生成SO库,使用SO库进行JNI调用,在Android sdcard创建文件并写入数据. 工具: NDK1.5 R1, android SDK1.5 R1, SDCARD, Ecli ...

  10. 张江在线APP演示

    app下载地址:https://itunes.apple.com/cn/app/zhang-jiang-zai-xian/id722630317?mt=8