|
6 | 6 |
|
7 | 7 | <p>Given a<strong> directed acyclic graph</strong>, with <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, and an array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represents a directed edge from node <code>from<sub>i</sub></code> to node <code>to<sub>i</sub></code>.</p>
|
8 | 8 |
|
9 |
| - |
10 |
| - |
11 | 9 | <p>Find <em>the smallest set of vertices from which all nodes in the graph are reachable</em>. It's guaranteed that a unique solution exists.</p>
|
12 | 10 |
|
13 |
| - |
14 |
| - |
15 | 11 | <p>Notice that you can return the vertices in any order.</p>
|
16 | 12 |
|
17 |
| - |
18 |
| - |
19 | 13 | <p> </p>
|
20 | 14 |
|
21 | 15 | <p><strong>Example 1:</strong></p>
|
22 | 16 |
|
23 |
| - |
24 |
| - |
25 | 17 | <p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1500-1599/1557.Minimum%20Number%20of%20Vertices%20to%20Reach%20All%20Nodes/images/untitled22.png" style="width: 231px; height: 181px;" /></p>
|
26 | 18 |
|
27 |
| - |
28 |
| - |
29 | 19 | <pre>
|
30 | 20 |
|
31 | 21 | <strong>Input:</strong> n = 6, edges = [[0,1],[0,2],[2,5],[3,4],[4,2]]
|
|
34 | 24 |
|
35 | 25 | <b>Explanation: </b>It's not possible to reach all the nodes from a single vertex. From 0 we can reach [0,1,2,5]. From 3 we can reach [3,4,2,5]. So we output [0,3].</pre>
|
36 | 26 |
|
37 |
| - |
38 |
| - |
39 | 27 | <p><strong>Example 2:</strong></p>
|
40 | 28 |
|
41 |
| - |
42 |
| - |
43 | 29 | <p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1500-1599/1557.Minimum%20Number%20of%20Vertices%20to%20Reach%20All%20Nodes/images/untitled.png" style="width: 201px; height: 201px;" /></p>
|
44 | 30 |
|
45 |
| - |
46 |
| - |
47 | 31 | <pre>
|
48 | 32 |
|
49 | 33 | <strong>Input:</strong> n = 5, edges = [[0,1],[2,1],[3,1],[1,4],[2,4]]
|
|
54 | 38 |
|
55 | 39 | </pre>
|
56 | 40 |
|
57 |
| - |
58 |
| - |
59 | 41 | <p> </p>
|
60 | 42 |
|
61 | 43 | <p><strong>Constraints:</strong></p>
|
62 | 44 |
|
63 |
| - |
64 |
| - |
65 | 45 | <ul>
|
66 | 46 | <li><code>2 <= n <= 10^5</code></li>
|
67 | 47 | <li><code>1 <= edges.length <= min(10^5, n * (n - 1) / 2)</code></li>
|
|
77 | 57 | ### **Python3**
|
78 | 58 |
|
79 | 59 | ```python
|
80 |
| - |
| 60 | +class Solution: |
| 61 | + def findSmallestSetOfVertices(self, n: int, edges: List[List[int]]) -> List[int]: |
| 62 | + s = {to for _, to in edges} |
| 63 | + return [i for i in range(n) if i not in s] |
81 | 64 | ```
|
82 | 65 |
|
83 | 66 | ### **Java**
|
84 | 67 |
|
85 | 68 | ```java
|
| 69 | +class Solution { |
| 70 | + public List<Integer> findSmallestSetOfVertices(int n, List<List<Integer>> edges) { |
| 71 | + Set<Integer> s = new HashSet<>(); |
| 72 | + for (List<Integer> e : edges) { |
| 73 | + s.add(e.get(1)); |
| 74 | + } |
| 75 | + List<Integer> ans = new ArrayList<>(); |
| 76 | + for (int i = 0; i < n; ++i) { |
| 77 | + if (!s.contains(i)) { |
| 78 | + ans.add(i); |
| 79 | + } |
| 80 | + } |
| 81 | + return ans; |
| 82 | + } |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +### **C++** |
| 87 | + |
| 88 | +```cpp |
| 89 | +class Solution { |
| 90 | +public: |
| 91 | + vector<int> findSmallestSetOfVertices(int n, vector<vector<int>>& edges) { |
| 92 | + unordered_set<int> s; |
| 93 | + for (auto& e : edges) s.insert(e[1]); |
| 94 | + vector<int> ans; |
| 95 | + for (int i = 0; i < n; ++i) |
| 96 | + { |
| 97 | + if (!s.count(i)) ans.push_back(i); |
| 98 | + } |
| 99 | + return ans; |
| 100 | + } |
| 101 | +}; |
| 102 | +``` |
86 | 103 |
|
| 104 | +### **Go** |
| 105 | +
|
| 106 | +```go |
| 107 | +func findSmallestSetOfVertices(n int, edges [][]int) []int { |
| 108 | + s := make(map[int]bool) |
| 109 | + for _, e := range edges { |
| 110 | + s[e[1]] = true |
| 111 | + } |
| 112 | + var ans []int |
| 113 | + for i := 0; i < n; i++ { |
| 114 | + if !s[i] { |
| 115 | + ans = append(ans, i) |
| 116 | + } |
| 117 | + } |
| 118 | + return ans |
| 119 | +} |
87 | 120 | ```
|
88 | 121 |
|
89 | 122 | ### **...**
|
|
0 commit comments