java.lang.Object
g2301_2400.s2368_reachable_nodes_with_restrictions.Solution

public class Solution extends Object
2368 - Reachable Nodes With Restrictions.<p>Medium</p> <p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>You are given a 2D integer array <code>edges</code> of length <code>n - 1</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p> <p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node</em> <code>0</code> <em>without visiting a restricted node.</em></p> <p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" alt="" /></p> <p><strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong> 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.</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" alt="" /></p> <p><strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> 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.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 <= n <= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> <li><code>1 <= restricted.length < n</code></li> <li><code>1 <= restricted[i] < n</code></li> <li>All the values of <code>restricted</code> are <strong>unique</strong>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • reachableNodes

      public int reachableNodes(int n, int[][] edges, int[] restricted)