2368. Reachable Nodes With Restrictions Medium
1/**
2 * [2368] Reachable Nodes With Restrictions
3 *
4 * There is an undirected tree with n nodes labeled from 0 to n - 1 and n - 1 edges.
5 * You are given a 2D integer array edges of length n - 1 where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree. You are also given an integer array restricted which represents restricted nodes.
6 * Return the maximum number of nodes you can reach from node 0 without visiting a restricted node.
7 * Note that node 0 will not be a restricted node.
8 *
9 * Example 1:
10 * <img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
11 * Input: n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
12 * Output: 4
13 * Explanation: The diagram above shows the tree.
14 * We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
15 *
16 * Example 2:
17 * <img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
18 * Input: n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
19 * Output: 3
20 * Explanation: The diagram above shows the tree.
21 * We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
22 *
23 *
24 * Constraints:
25 *
26 * 2 <= n <= 10^5
27 * edges.length == n - 1
28 * edges[i].length == 2
29 * 0 <= ai, bi < n
30 * ai != bi
31 * edges represents a valid tree.
32 * 1 <= restricted.length < n
33 * 1 <= restricted[i] < n
34 * All the values of restricted are unique.
35 *
36 */
37pub struct Solution {}
38
39// problem: https://leetcode.com/problems/reachable-nodes-with-restrictions/
40// discuss: https://leetcode.com/problems/reachable-nodes-with-restrictions/discuss/?currentPage=1&orderBy=most_votes&query=
41
42// submission codes start here
43
44impl Solution {
45 pub fn reachable_nodes(n: i32, edges: Vec<Vec<i32>>, restricted: Vec<i32>) -> i32 {
46 0
47 }
48}
49
50// submission codes end
51
52#[cfg(test)]
53mod tests {
54 use super::*;
55
56 #[test]
57 fn test_2368() {
58 }
59}
60
Back
© 2025 bowen.ge All Rights Reserved.