码迷,mamicode.com
首页 > 其他好文 > 详细

261. Graph Valid Tree

时间:2017-05-14 13:50:49      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:add   pre   integer   false   rect   nod   else   air   dtree   

题目:

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:

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

链接: http://leetcode.com/problems/graph-valid-tree/

5/13/2017

算法班,抄答案

被hashCode跑偏了,没有想起来怎么做这道题。

最后用顶点做hashset比用边要好。因为用边不容易检测到环。

http://www.jiuzhang.com/solutions/graph-valid-tree/

 1 public class Solution {
 2 
 3     public boolean validTree(int n, int[][] edges) {
 4         // make sure E == V - 1
 5         if (edges == null || edges.length != n - 1) {
 6             return false;
 7         }
 8         Map<Integer, Set<Integer>> graph = initializeGraph(n, edges);
 9         Queue<Integer> queue = new LinkedList<Integer>();
10         Set<Integer> set = new HashSet<Integer>();
11         queue.offer(0);
12         set.add(0);
13         
14         while (!queue.isEmpty()) {
15             Integer v = queue.poll();
16             for (Integer neighbor: graph.get(v)) {
17                 if (!set.contains(neighbor)) {
18                     queue.offer(neighbor);
19                     set.add(neighbor);
20                 }
21             }
22         }
23         return set.size() == n;
24     }
25     private Map<Integer, Set<Integer>> initializeGraph(int n, int[][] edges) {
26         Map<Integer, Set<Integer>> map = new HashMap<>();
27         
28         for (int i = 0; i < n; i++) {
29             map.put(i, new HashSet<Integer>());
30         }
31         for (int i = 0; i < edges.length; i++) {
32             int e = edges[i][0];
33             int o = edges[i][1];
34             map.get(e).add(o);
35             map.get(o).add(e);
36         }
37         return map;
38     }
39 }

顺便实现了一次hashCode

 1     class Edge {
 2         final int either, other;
 3         Edge(int[] edge) {
 4             if (edge[0] < edge[1]) {
 5                 this.either = edge[0];
 6                 this.other = edge[1];                
 7             } else {
 8                 this.other = edge[0];
 9                 this.either = edge[1];
10             }
11         }
12         @Override
13         public boolean equals(Object obj) {
14             if (obj == null) return false;
15             if (!(obj instanceof Edge)) return false;
16             Edge o = (Edge) obj;
17             if (o.either == this.either && o.other == this.other) {
18                 // because we already ordered the two V, no need to check other direction
19                 return true;
20             }
21             return false;
22         }
23         @Override
24         public int hashCode() {
25             int hash = 7;
26             hash = 71 * hash + this.either;
27             hash = 71 * hash + this.other;
28             return hash;        
29         }
30     }

还有union-find的方法。

261. Graph Valid Tree

标签:add   pre   integer   false   rect   nod   else   air   dtree   

原文地址:http://www.cnblogs.com/panini/p/6852084.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!