Class Solution
- java.lang.Object
-
- g2301_2400.s2368_reachable_nodes_with_restrictions.Solution
-
public class Solution extends Object
2368 - Reachable Nodes With Restrictions.Medium
There is an undirected tree with
nnodes labeled from0ton - 1andn - 1edges.You are given a 2D integer array
edgesof lengthn - 1whereedges[i] = [ai, bi]indicates that there is an edge between nodesaiandbiin the tree. You are also given an integer arrayrestrictedwhich represents restricted nodes.Return the maximum number of nodes you can reach from node
0without visiting a restricted node.Note that node
0will not be a restricted node.Example 1:

Input: n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
Output: 4
Explanation: The diagram above shows the tree. We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
Example 2:

Input: n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
Output: 3
Explanation: The diagram above shows the tree. We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
Constraints:
2 <= n <= 105edges.length == n - 1edges[i].length == 20 <= ai, bi < nai != biedgesrepresents a valid tree.1 <= restricted.length < n1 <= restricted[i] < n- All the values of
restrictedare unique.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intreachableNodes(int n, int[][] edges, int[] restricted)
-