|
| 1 | +/** |
| 2 | + * [1557] Minimum Number of Vertices to Reach All Nodes |
| 3 | + * |
| 4 | + * Given a directed acyclic graph, with n vertices numbered from 0 to n-1, and an array edges where edges[i] = [fromi, toi] represents a directed edge from node fromi to node toi. |
| 5 | + * Find the smallest set of vertices from which all nodes in the graph are reachable. It's guaranteed that a unique solution exists. |
| 6 | + * Notice that you can return the vertices in any order. |
| 7 | + * |
| 8 | + * Example 1: |
| 9 | + * <img alt="" src="https://assets.leetcode.com/uploads/2020/07/07/untitled22.png" style="width: 231px; height: 181px;" /> |
| 10 | + * |
| 11 | + * Input: n = 6, edges = [[0,1],[0,2],[2,5],[3,4],[4,2]] |
| 12 | + * Output: [0,3] |
| 13 | + * Explanation: 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]. |
| 14 | + * Example 2: |
| 15 | + * <img alt="" src="https://assets.leetcode.com/uploads/2020/07/07/untitled.png" style="width: 201px; height: 201px;" /> |
| 16 | + * |
| 17 | + * Input: n = 5, edges = [[0,1],[2,1],[3,1],[1,4],[2,4]] |
| 18 | + * Output: [0,2,3] |
| 19 | + * Explanation: Notice that vertices 0, 3 and 2 are not reachable from any other node, so we must include them. Also any of these vertices can reach nodes 1 and 4. |
| 20 | + * |
| 21 | + * |
| 22 | + * Constraints: |
| 23 | + * |
| 24 | + * 2 <= n <= 10^5 |
| 25 | + * 1 <= edges.length <= min(10^5, n * (n - 1) / 2) |
| 26 | + * edges[i].length == 2 |
| 27 | + * 0 <= fromi, toi < n |
| 28 | + * All pairs (fromi, toi) are distinct. |
| 29 | + * |
| 30 | + */ |
| 31 | +pub struct Solution {} |
| 32 | + |
| 33 | +// problem: https://leetcode.com/problems/minimum-number-of-vertices-to-reach-all-nodes/ |
| 34 | +// discuss: https://leetcode.com/problems/minimum-number-of-vertices-to-reach-all-nodes/discuss/?currentPage=1&orderBy=most_votes&query= |
| 35 | + |
| 36 | +// submission codes start here |
| 37 | + |
| 38 | +impl Solution { |
| 39 | + pub fn find_smallest_set_of_vertices(n: i32, edges: Vec<Vec<i32>>) -> Vec<i32> { |
| 40 | + use std::iter::FromIterator; |
| 41 | + |
| 42 | + let mut set: std::collections::HashSet<i32> = |
| 43 | + std::collections::HashSet::from_iter((0..n).into_iter()); |
| 44 | + |
| 45 | + for edge in edges { |
| 46 | + set.remove(&edge[1]); |
| 47 | + } |
| 48 | + |
| 49 | + set.into_iter().collect::<Vec<i32>>() |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// submission codes end |
| 54 | + |
| 55 | +#[cfg(test)] |
| 56 | +mod tests { |
| 57 | + use super::*; |
| 58 | + |
| 59 | + #[test] |
| 60 | + fn test_1557_example_1() { |
| 61 | + let n = 6; |
| 62 | + let edges = vec![vec![0, 1], vec![0, 2], vec![2, 5], vec![3, 4], vec![4, 2]]; |
| 63 | + |
| 64 | + let result = vec![0, 3]; |
| 65 | + |
| 66 | + assert_eq_sorted!(Solution::find_smallest_set_of_vertices(n, edges), result) |
| 67 | + } |
| 68 | + |
| 69 | + #[test] |
| 70 | + fn test_1557_example_2() { |
| 71 | + let n = 5; |
| 72 | + let edges = vec![vec![0, 1], vec![2, 1], vec![3, 1], vec![1, 4], vec![2, 4]]; |
| 73 | + |
| 74 | + let result = vec![0, 2, 3]; |
| 75 | + |
| 76 | + assert_eq_sorted!(Solution::find_smallest_set_of_vertices(n, edges), result) |
| 77 | + } |
| 78 | +} |
0 commit comments