diff --git a/problems/minimum-number-of-vertices-to-reach-all-nodes.rs b/problems/minimum-number-of-vertices-to-reach-all-nodes.rs new file mode 100644 index 0000000..f00c71a --- /dev/null +++ b/problems/minimum-number-of-vertices-to-reach-all-nodes.rs @@ -0,0 +1,19 @@ +impl Solution { + pub fn find_smallest_set_of_vertices(n: i32, edges: Vec>) -> Vec { + // It should be possible to use topological ordering + BFS, but… + + let mut is_reachable: Vec = vec![false; n as usize]; + for dst in edges.iter().map(|edge| edge[1] as usize) { + is_reachable[dst] = true; + } + + let mut result: Vec = vec![]; + for (i, &reachable) in is_reachable.iter().enumerate() { + if !reachable { + result.push(i as i32); + } + } + + result + } +}