id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 0 | {
"code": "class Solution {\npublic:\n /**\n * qs: is it a forest of size 1? ie one tree?\n * will 0 ever be a restricted node?\n * does the count include or exclude the restricted ndoes themselves?\n * is restricted composed of distinct values?\n * approach: BFS from 0 with stopping condition of restricted node\n * maintain and then return a counter of visited nodes\n */\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n int nVisited = 0;\n vector<vector<int>> adj(n);\n vector<bool> visited(n, false);\n for (auto& e : edges) {\n adj[e[0]].push_back(e[1]);\n adj[e[1]].push_back(e[0]);\n }\n for (auto& r : restricted) {\n visited[r] = true;\n }\n queue<int> q;\n q.push(0);\n while (!q.empty()) {\n auto node = q.front();\n q.pop();\n cout << \"n=\" << node << \"\\n\";\n nVisited++;\n visited[node] = true;\n for (auto& neighbor : adj[node]) {\n if (!visited[neighbor]) {\n q.push(neighbor);\n }\n\n }\n }\n return nVisited;\n }\n};",
"memory": "151980"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 0 | {
"code": "class Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<vector<int>> G(n);\n for (auto& e : edges) {\n G[e[0]].push_back(e[1]);\n G[e[1]].push_back(e[0]);\n }\n vector<bool> R(n);\n for (int r : restricted) {\n R[r] = true;\n }\n vector<bool> V(n);\n V[0] = true;\n queue<int> Q;\n Q.push(0);\n int res = 0;\n while (!Q.empty()) {\n int v = Q.front();\n Q.pop();\n res++;\n for (int c : G[v]) {\n if (!V[c] && !R[c]) {\n Q.push(c);\n V[c] = true;\n }\n }\n }\n return res;\n }\n};",
"memory": "151980"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 0 | {
"code": "class Solution {\npublic:\n int count = 0;\n int reachableNodes(int n, vector<vector<int>>& edges,\n vector<int>& restricted) {\n vector<int> graph[n];\n int restrictedArr[100000];\n for (int i = 0; i < 100000; i++){\n restrictedArr[i] = 1;\n }\n for(int res : restricted){\n restrictedArr[res] = 0;\n }\n for (int i = 0; i < edges.size(); i++) {\n graph[edges[i][0]].push_back(edges[i][1]);\n graph[edges[i][1]].push_back(edges[i][0]);\n }\n dfs(0, 0, graph, restrictedArr);\n return count + 1;\n }\n\n void dfs(int v,int p ,vector<int>* graph,\n int* restrictedArr) {\n for (int to : graph[v]) {\n if (to != p && restrictedArr[to]) {\n dfs(to, v, graph, restrictedArr);\n count++;\n }\n }\n }\n};",
"memory": "153300"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 0 | {
"code": "class Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<vector<int>> adj(n);\n for(int i=0;i<n-1;i++){\n adj[edges[i][0]].push_back(edges[i][1]);\n adj[edges[i][1]].push_back(edges[i][0]);\n\n }\n vector<int> visit(n,0);\n int m = restricted.size();\n for(int i=0;i<m;i++){\n visit[restricted[i]]=1;\n }\n queue<int> q;\n q.push(0);\n while(!q.empty()){\n int a = q.front();\n visit[a]=1;\n for(int i=0;i<adj[a].size();i++){\n if(visit[adj[a][i]]==0){\n q.push(adj[a][i]);\n }\n }\n q.pop();\n }\n int x=0;\n for(int i=0;i<n;i++){\n if(visit[i]==1){\n x++;\n }\n }\n return x-m;\n\n \n }\n};",
"memory": "153300"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 0 | {
"code": "class Solution {\npublic:\n\n void dfs(int src, vector<int> &vis, vector<int> adj[]){\n vis[src]=1;\n for(auto it:adj[src]){\n if(!vis[it]){\n dfs(it,vis,adj);\n }\n }\n }\n\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n\n vector<int> adj[n];\n for(int i=0;i<edges.size();i++){\n int u=edges[i][0];\n int v=edges[i][1];\n\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n \n vector<int> vis(n,0);\n for(int i=0;i<restricted.size();i++){\n vis[restricted[i]]=1;\n }\n\n dfs(0,vis,adj);\n\n int cnt=0;\n for(int i=0;i<n;i++){\n if(vis[i]==1) cnt++;\n }\n\n return cnt-restricted.size();\n }\n};",
"memory": "154620"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 0 | {
"code": "class Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<int> res(n,0);\n for(int i=0;i<restricted.size();i++){\n res[restricted[i]]=1;\n }\n\n vector<vector<int>> adj(n);\n for(int i=0;i<edges.size();i++){\n adj[edges[i][0]].push_back(edges[i][1]);\n adj[edges[i][1]].push_back(edges[i][0]);\n }\n\n queue<int> q;\n q.push(0);\n vector<int> vis(n,0);\n vis[0]=1;\n while(!q.empty()){\n int node=q.front();\n q.pop();\n\n for(auto it : adj[node]){\n if(!vis[it] && !res[it]){\n vis[it]=1;\n q.push(it);\n }\n }\n }\n\n int ct=0;\n for(int i=0;i<n;i++){\n if(vis[i])ct++;\n }\n\n return ct;\n }\n};",
"memory": "154620"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 0 | {
"code": "class Solution {\npublic:\n\n void dfs(int i, vector<int>adj[], vector<int>&res, vector<int>&vis)\n {\n vis[i]=1;\n for(auto it : adj[i])\n {\n if(!vis[it] && !res[it])\n {\n dfs(it,adj,res,vis);\n }\n }\n }\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n\n vector<int>adj[n];\n for(auto & it : edges)\n {\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n vector<int>vis(n,0);\n vector<int>res(n,0);\n for(auto & it : restricted)\n {\n res[it]=1;\n }\n dfs(0,adj,res,vis);\n int ans=0;\n for(int i=0;i<n;i++)\n {\n if(vis[i]) ans++;\n }\n return ans;\n }\n};",
"memory": "155940"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 0 | {
"code": "class Solution {\npublic:\n int dfsNumNodes(int u, int par, vector<vector<int>> &adj){\n int ans = 1;\n\n for(auto v: adj[u]){\n if(v==par){\n continue;\n }\n ans += dfsNumNodes(v, u, adj);\n }\n return ans;\n }\n\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<vector<int>> adj(n);\n unordered_set<int> restrictedSet;\n\n for(auto u: restricted){\n restrictedSet.insert(u);\n }\n\n int m = edges.size();\n for(int i=0;i<m;i++){\n int u = edges[i][0], v = edges[i][1];\n if(restrictedSet.find(u) != restrictedSet.end() || restrictedSet.find(v) != restrictedSet.end()){\n continue; \n }\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n return dfsNumNodes(0, -1, adj);\n }\n};",
"memory": "155940"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 0 | {
"code": "class Solution {\npublic:\n\n void dfs(int v, int par, vector<vector<int>> &adj, int &res){\n res++;\n for(int &u: adj[v]){\n if(u==par){\n continue;\n }\n dfs(u,v,adj,res);\n }\n }\n\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n set<int> st(restricted.begin(),restricted.end());\n vector<vector<int>> adj(n);\n for(auto &edge: edges){\n if(st.find(edge[0])!=st.end() || st.find(edge[1])!=st.end()){\n continue;\n }\n adj[edge[0]].push_back(edge[1]);\n adj[edge[1]].push_back(edge[0]);\n }\n int res=0;\n dfs(0,-1,adj,res);\n return res;\n }\n};",
"memory": "157260"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 0 | {
"code": "class Solution {\npublic:\n int ans = 0;\n bool used[100000 + 1];\n\n void dfs(int node, vector<vector<int>>& graph) {\n used[node] = true;\n ans += 1;\n for (const auto& elem : graph[node]) {\n if (!used[elem]) {\n dfs(elem, graph);\n }\n }\n }\n\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n \n vector<vector<int>> graph(n);\n for (size_t ind = 0; ind < edges.size(); ++ind) {\n graph[edges[ind][0]].push_back(edges[ind][1]);\n graph[edges[ind][1]].push_back(edges[ind][0]);\n }\n\n for (size_t ind = 0; ind != restricted.size(); ++ind) {\n used[restricted[ind]] = true;\n }\n\n dfs(0, graph);\n\n return ans;\n }\n};",
"memory": "157260"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 0 | {
"code": "class Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<vector<int>> adj(n);\n unordered_set<int> noVisit(restricted.begin(), restricted.end());\n for(auto &vec: edges)\n {\n adj[vec[0]].push_back(vec[1]);\n adj[vec[1]].push_back(vec[0]);\n }\n\n vector<bool> visited(n, false);\n int count = 1;\n queue<int> que;\n visited[0] = true;\n que.push(0);\n\n while(!que.empty())\n {\n int node = que.front();\n que.pop();\n\n for(int neigh: adj[node])\n {\n if(noVisit.find(neigh) == noVisit.end() && !visited[neigh])\n {\n que.push(neigh);\n visited[neigh] = true;\n count++;\n }\n }\n }\n\n return count;\n }\n};",
"memory": "163860"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 0 | {
"code": "class Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<vector<int>>adj(n);\n unordered_set<int>st;\n\n for(auto it:restricted)\n {\n st.insert(it);\n }\n\n for(auto &it:edges)\n {\n int u=it[0];\n int v=it[1];\n\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n\n vector<bool>vis(n,false);\n queue<int>q;\n q.push(0);\n vis[0]=true;\n int result=0;\n\n while(!q.empty())\n {\n int node=q.front();\n q.pop();\n\n for(auto it:adj[node])\n {\n if(!vis[it] && st.find(it)==st.end())\n {\n vis[node]=true;\n result++;\n q.push(it);\n }\n }\n }\n\n return result+1;\n }\n};",
"memory": "163860"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 1 | {
"code": "class Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<int> vis(n,0);\n for(int i=0;i<restricted.size();i++){\n vis[restricted[i]]=1;\n }\n unordered_set<int> s;\n for(auto it: restricted)\n s.insert(it);\n\n vector<vector<int>> adj(edges.size()+1);\n for(auto it: edges)\n {\n if(s.find(it[0])==s.end() && s.find(it[1])==s.end())\n {\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n }\n queue<int> q;\n q.push(0);\n vis[0]=2;\n int p=0;\n while(!q.empty()){\n int node=q.front();\n q.pop();\n \n \n for(auto it:adj[node]){\n if(vis[it]==0){\n vis[it]=2;\n q.push(it);\n }\n\n }\n }\n int cnt=0;\n for(int i=0;i<n;i++){\n cout<<vis[i]<<\" \";\n if(vis[i]==2){\n cnt++;\n }\n }\n return cnt;\n\n\n \n }\n};",
"memory": "165180"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 1 | {
"code": "#include <vector>\n#include <unordered_set>\n#include <queue>\n\nusing namespace std;\n\nclass Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) \n {\n // Use an adjacency list to represent the graph\n vector<vector<int>> adj(n);\n vector<int> vis(n, 0);\n \n // Use unordered_set for faster lookups\n unordered_set<int> restrictedSet(restricted.begin(), restricted.end());\n \n // Construct the graph\n for (auto& edge : edges) {\n int u = edge[0];\n int v = edge[1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n \n int count = 0;\n queue<int> q;\n q.push(0);\n vis[0] = 1;\n \n // BFS traversal\n while (!q.empty()) {\n int node = q.front();\n q.pop();\n count++;\n \n for (int neighbor : adj[node]) {\n // If the neighbor is not visited and not restricted\n if (!vis[neighbor] && restrictedSet.find(neighbor) == restrictedSet.end()) {\n vis[neighbor] = 1; // Mark as visited\n q.push(neighbor);\n }\n }\n }\n \n return count;\n }\n};\n",
"memory": "165180"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 1 | {
"code": "class Solution {\npublic:\n void dfs(vector<int> adj[],vector<int> &vis,int u,int &count){\n vis[u]=1;\n for(auto v: adj[u]){\n if(!vis[v]){\n count++; \n dfs(adj,vis,v,count);\n }\n }\n }\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<int> adj[n];\n set<int> s(restricted.begin(),restricted.end());\n for(auto edge: edges){\n if((s.count(edge[0])==0) && (s.count(edge[1])==0)){\n cout<<edge[0]<<\" \"<<edge[1]<<endl;\n adj[edge[0]].push_back(edge[1]);\n adj[edge[1]].push_back(edge[0]);\n }\n }\n int count=0;\n vector<int> vis(n,0);\n dfs(adj,vis,0,count);\n return count+1;\n }\n};",
"memory": "166500"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 1 | {
"code": "class Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n int ans=0;\n vector<vector<int>>adj(n+1);\n for(auto it:edges){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n vector<int>vis(n+1,-1);\n queue<int>q;\n \n for(auto it:restricted){\n if(it==0){\n return 0;\n }\n vis[it]=1;\n }\n vis[0]=1;\n q.push(0);\n \n while(!q.empty()){\n int node=q.front();\n q.pop();\n ans++;\n for(auto it:adj[node]){\n if(vis[it]==-1){\n vis[it]=1;\n q.push(it);\n }\n }\n }\n return ans;\n \n }\n};",
"memory": "166500"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 1 | {
"code": "class Solution {\npublic:\n void dfs(int node,int &ans,vector<int> adj[],vector<int>& vis){ \n vis[node]=1;\n for(auto it:adj[node]){\n if(!vis[it]){\n ans=ans+1;\n dfs(it,ans,adj,vis);\n }\n }\n }\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<int> adj[n];\n for(auto it:edges){\n adj[it[1]].push_back(it[0]);\n adj[it[0]].push_back(it[1]);\n }\n int ans=1;\n vector<int> vis(n,0);\n for(int i=0;i<restricted.size();i++){\n vis[restricted[i]]=1;\n }\n \n dfs(0,ans,adj,vis);\n return ans;\n }\n};",
"memory": "167820"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 1 | {
"code": "\nclass Solution {\npublic:\n\tint dfs(int node,vector<int>&vis,vector<int>adj[]){\n\t\tif(vis[node]) return 0;\n\t\tvis[node] = 1;\n\t\tint res = 0;\n\t\tfor(auto edge:adj[node]) if(!vis[edge]) res+=dfs(edge,vis,adj);\n\t\t\treturn res+1;\n\t}\n\n\tint reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n\t\tvector<int>vis(n,0);\n\t\tfor(auto x:restricted) vis[x] = 1;\n\t\tvector<int>adj[n];\n\t\tfor(auto node:edges) adj[node[0]].push_back(node[1]),adj[node[1]].push_back(node[0]);\n\t\treturn dfs(0,vis,adj);\n\t}\n};",
"memory": "167820"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 1 | {
"code": "class Solution {\nprivate: \n int dfs(int src, vector<int> adj[], vector<int>& vis, set<int>& notAllowed) {\n vis[src] = 1;\n int reachable = 1;\n\n for(auto &it: adj[src]) {\n if(!vis[it] && !notAllowed.count(it)) {\n reachable += dfs(it, adj, vis, notAllowed);\n }\n }\n\n return reachable;\n }\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n set<int> notAllowed(restricted.begin(), restricted.end());\n\n vector<int> adj[n];\n\n for(auto &it: edges) {\n int u = it[0], v = it[1];\n\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n\n vector<int> vis(n, 0);\n return dfs(0, adj, vis, notAllowed);\n }\n};",
"memory": "169140"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 1 | {
"code": "class Solution {\npublic:\n vector<int>count;\n int ans = 0;\n vector<bool>restri;\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<int> adj[n];\n restri.resize(n,false);\n for(auto i:restricted) restri[i] = true;\n for(int i = 0; i < edges.size() ; i++){\n adj[edges[i][0]].push_back(edges[i][1]);\n adj[edges[i][1]].push_back(edges[i][0]);\n }\n solve(0,adj,-1);\n return ans;\n } \n void solve(int src , vector<int>*adj, int p)\n {\n ans++;\n for(auto nbr:adj[src]){\n if(nbr != p and !restri[nbr]){\n solve(nbr , adj , src);\n }\n }\n }\n\n};",
"memory": "169140"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 1 | {
"code": "class Solution {\npublic:\n void fun(long long node,vector<long long>adj[],vector<long long>&vis){\n vis[node]=1;\n for(auto it:adj[node]){\n if(!vis[it] && vis[it]!=-1){\n fun(it,adj,vis);\n }\n }\n }\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<long long>adj[n];\n for(auto it:edges){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n vector<long long>vis(n,0);\n for(auto it:restricted){\n vis[it]=-1;\n }\n fun(0,adj,vis);\n long long cnt=0;\n for(auto it:vis){\n if(it==1)cnt+=1;\n }\n return cnt;\n }\n};",
"memory": "170460"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 1 | {
"code": "class Solution {\npublic:\n void solve(int node,int p,int n,vector<int> adj[],vector<int>& r,vector<int> &vis){\n if(vis[node]) return;\n vis[node]=1;\n for(auto it:adj[node]){\n if(it==p||vis[it]) continue;\n solve(it,node,n,adj,r,vis);\n }\n }\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<int> adj[n];\n for(auto it:edges){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n vector<int> vis(n);\n for(auto it:restricted){\n vis[it]=-1;\n }\n \n solve(0,-1,n,adj,restricted,vis);\n int cnt=0;\n for(auto it:vis){\n cnt+=it==1;\n }\n return cnt;\n }\n};",
"memory": "170460"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 1 | {
"code": "class Solution {\npublic:\n void dfs(int i, vector<vector<int>>&adj, vector<int> &vis,int &c)\n {\n vis[i]=1;\n for(auto it:adj[i])\n {\n if(!vis[it])\n {\n c++;\n dfs(it,adj,vis,c);\n }\n }\n }\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n unordered_set<int> s;\n for(auto it: restricted)\n s.insert(it);\n\n vector<vector<int>> adj(edges.size()+1);\n for(auto it: edges)\n {\n if(s.find(it[0])==s.end() && s.find(it[1])==s.end())\n {\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n }\n vector<int> vis(edges.size()+1);\n int c=1;\n dfs(0,adj,vis,c);\n return c;\n }\n};",
"memory": "171780"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 1 | {
"code": "class Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& r) {\n unordered_set<int>st(r.begin(),r.end());\n queue<int>q;\n q.push(0);\n vector<int>adj[n];\n for(vector<int>c : edges) {\n adj[c[0]].push_back(c[1]);\n adj[c[1]].push_back(c[0]);\n }\n int c=1;\n int vis[n]; memset(vis,0,sizeof(vis));\n vis[0]=1;\n while(!q.empty()) {\n int k=q.front(); q.pop();\n for(int i : adj[k]) {\n if(st.find(i)==st.end() && vis[i]==0) {\n c++;\n q.push(i);\n vis[i]=1;\n }\n }\n }\n return c;\n }\n};",
"memory": "171780"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 1 | {
"code": "class Solution {\npublic:\n int solve(int n,vector<int> adj[],unordered_set<int> &check){\n queue<int> q;\n q.push(0);\n vector<int> vis(n,0);\n vis[0] = 1;\n int ans = 0;\n while(!q.empty()){\n int node = q.front();\n q.pop();\n ans++;\n for(auto i:adj[node]){\n if(vis[i] == 0 && check.find(i) == check.end()){\n vis[i]=1;\n q.push(i);\n }\n }\n\n }\n return ans;\n }\n\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<int> adj[n];\n for(auto i:edges){\n int u = i[0];\n int v = i[1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n\n unordered_set<int> check;\n for(auto i:restricted){\n check.insert(i);\n }\n\n int ans = solve(n,adj,check);\n return ans;\n }\n};",
"memory": "173100"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 1 | {
"code": "class Solution {\npublic:\n int solve(int n,vector<int> adj[],unordered_set<int> &check){\n queue<int> q;\n q.push(0);\n vector<int> vis(n,0);\n vis[0] = 1;\n int ans = 0;\n while(!q.empty()){\n int node = q.front();\n q.pop();\n ans++;\n for(auto i:adj[node]){\n if(vis[i] == 0 && check.find(i) == check.end()){\n vis[i]=1;\n q.push(i);\n }\n }\n\n }\n return ans;\n }\n\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<int> adj[n];\n for(auto i:edges){\n int u = i[0];\n int v = i[1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n\n unordered_set<int> check;\n for(auto i:restricted){\n check.insert(i);\n }\n\n int ans = solve(n,adj,check);\n return ans;\n }\n};",
"memory": "173100"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 1 | {
"code": "class Solution {\npublic:\n void DFS(vector<vector<int>>&adj,unordered_set<int>&restricted_nodes,vector<int>&vis,int node,int &count)\n { \n for(int i=0;i<adj[node].size();i++)\n {\n if(!vis[adj[node][i]]&&restricted_nodes.find(adj[node][i])==restricted_nodes.end())\n {\n vis[adj[node][i]]=1;\n count++;\n DFS(adj,restricted_nodes,vis,adj[node][i],count);\n }\n\n }\n\n }\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<int>vis(n,0);\n vector<vector<int>>adj(n);\n unordered_set<int>restricted_nodes;\n for(int i=0;i<n-1;i++)\n {\n adj[edges[i][0]].push_back(edges[i][1]);\n adj[edges[i][1]].push_back(edges[i][0]);\n }\n for(int i=0;i<restricted.size();i++)\n {\n restricted_nodes.insert(restricted[i]);\n }\n int count=1;\n vis[0]=1;\n DFS(adj,restricted_nodes,vis,0,count);\n return count;\n }\n};",
"memory": "174420"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 1 | {
"code": "class Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<vector<int>> graph(n);\n for (const auto& edge : edges) {\n graph[edge[0]].push_back(edge[1]);\n graph[edge[1]].push_back(edge[0]);\n }\n \n unordered_set<int> restrictedSet(restricted.begin(), restricted.end());\n \n queue<int> q;\n q.push(0);\n unordered_set<int> visited;\n visited.insert(0);\n \n int reachableCount = 0;\n \n while (!q.empty()) {\n int node = q.front();\n q.pop();\n reachableCount++;\n \n for (const int& neighbor : graph[node]) {\n if (visited.count(neighbor) == 0 && restrictedSet.count(neighbor) == 0) {\n q.push(neighbor);\n visited.insert(neighbor);\n }\n }\n }\n \n return reachableCount;\n }\n};",
"memory": "174420"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 1 | {
"code": "class Solution {\nprivate:\n int dfs(int node, vector<bool>& seen, vector<vector<int>>& graph) {\n if (seen[node]) {\n return 0;\n }\n int ans = 1;\n seen[node] = true;\n for (int neighbor: graph[node]) {\n ans += dfs(neighbor, seen, graph);\n }\n return ans;\n }\n\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<vector<int>> graph(n);\n vector<bool> seen(n, false);\n for(vector<int>& edge: edges) {\n int x = edge[0];\n int y = edge[1];\n graph[x].push_back(y);\n graph[y].push_back(x);\n }\n for(int node: restricted) {\n seen[node] = true;\n }\n return dfs(0, seen, graph); \n }\n};",
"memory": "175740"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 1 | {
"code": "class Solution {\n public:\n int reachableNodes(int n, vector<vector<int>>& edges,\n vector<int>& restricted) {\n vector<vector<int>> tree(n);\n vector<bool> seen(n);\n\n for (const vector<int>& edge : edges) {\n const int u = edge[0];\n const int v = edge[1];\n tree[u].push_back(v);\n tree[v].push_back(u);\n }\n\n for (const int r : restricted)\n seen[r] = true;\n\n return dfs(tree, 0, seen);\n }\n\n private:\n int dfs(const vector<vector<int>>& tree, int u, vector<bool>& seen) {\n if (seen[u])\n return 0;\n\n seen[u] = true;\n int ans = 1;\n\n for (const int v : tree[u])\n ans += dfs(tree, v, seen);\n\n return ans;\n }\n};",
"memory": "175740"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 1 | {
"code": "#pragma GCC optimize(\"O3\")\n#pragma GCC target(\"avx2, bmi, bmi2, lzcnt, popcnt\")\n\nstatic const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n std::cin.tie(nullptr);\n return true;\n}();\n\nclass Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<vector<int>> graph(n);\n for(auto edge : edges){\n graph[edge[0]].push_back(edge[1]);\n graph[edge[1]].push_back(edge[0]);\n }\n unordered_set<int> s (restricted.begin(),restricted.end());\n\n queue<int> q;\n q.push(0);\n vector<bool> visited(n,false);\n visited[0] = true;\n int res = 1;\n while(!q.empty()){\n int node = q.front();\n q.pop();\n for(int i = 0; i < graph[node].size(); i++){\n if(!visited[graph[node][i]] && s.find(graph[node][i])==s.end()){\n visited[graph[node][i]] = true;\n q.push(graph[node][i]);\n res++;\n }\n }\n }\n\n return res;\n }\n};",
"memory": "177060"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 1 | {
"code": "\nstatic const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n std::cin.tie(nullptr);\n return true;\n}();\n\nclass Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<vector<int>> graph(n);\n for(auto edge : edges){\n graph[edge[0]].push_back(edge[1]);\n graph[edge[1]].push_back(edge[0]);\n }\n unordered_set<int> s (restricted.begin(),restricted.end());\n\n queue<int> q;\n q.push(0);\n vector<bool> visited(n,false);\n visited[0] = true;\n int res = 1;\n while(!q.empty()){\n int node = q.front();\n q.pop();\n for(int i = 0; i < graph[node].size(); i++){\n if(!visited[graph[node][i]] && s.find(graph[node][i])==s.end()){\n visited[graph[node][i]] = true;\n q.push(graph[node][i]);\n res++;\n }\n }\n }\n\n return res;\n }\n};",
"memory": "177060"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 1 | {
"code": "class Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<bool> visited(n, false);\n vector<vector<int>> neighbours(n);\n //Mark restricted nodes as already visited\n for (int i=0; i<restricted.size(); i++) {\n visited[restricted[i]] = true;\n }\n for (int i=0; i<edges.size(); i++) {\n neighbours[edges[i][0]].push_back(edges[i][1]);\n neighbours[edges[i][1]].push_back(edges[i][0]);\n }\n\n return nodesVisitable(neighbours, visited, 0);\n }\n\n int nodesVisitable(vector<vector<int>>& neighbours, vector<bool>& visited, int curr) {\n visited[curr] = true;\n int numNodes = 1;\n for (int i=0; i<neighbours[curr].size(); i++) {\n if (!visited[neighbours[curr][i]]) {\n numNodes += nodesVisitable(neighbours, visited, neighbours[curr][i]);\n }\n }\n return numNodes;\n }\n};",
"memory": "178380"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 1 | {
"code": "class Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<bool> visited(n, false);\n vector<vector<int>> neighbours(n);\n //Mark restricted nodes as already visited\n for (int i=0; i<restricted.size(); i++) {\n visited[restricted[i]] = true;\n }\n for (int i=0; i<edges.size(); i++) {\n neighbours[edges[i][0]].push_back(edges[i][1]);\n neighbours[edges[i][1]].push_back(edges[i][0]);\n }\n\n return nodesVisitable(neighbours, visited, 0);\n }\n\n int nodesVisitable(vector<vector<int>>& neighbours, vector<bool>& visited, int curr) {\n visited[curr] = true;\n int numNodes = 1;\n for (int i=0; i<neighbours[curr].size(); i++) {\n if (!visited[neighbours[curr][i]]) {\n numNodes += nodesVisitable(neighbours, visited, neighbours[curr][i]);\n }\n }\n return numNodes;\n }\n};",
"memory": "178380"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 2 | {
"code": "class Solution {\npublic:\n\n\n void dfs(int node, vector<int> &vis, int &count, vector<int> adj[]){\n vis[node]=1;\n for(auto it:adj[node]){\n if(!vis[it]){\n count++;\n dfs(it,vis,count,adj);\n }\n }\n }\n\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<int> adj[n];\n for(auto it:edges){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n unordered_set<int> st;\n vector<int> vis(n,0);\n for(auto it:restricted){\n st.insert(it);\n vis[it]=1;\n }\n int count=1;\n dfs(0,vis,count,adj);\n return count;\n \n \n }\n};",
"memory": "179700"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 2 | {
"code": "\n\nclass Solution {\npublic:\n int count = 1;\n\n void dfs(int node, vector<int> adj[], vector<int>& vis, unordered_set<int>& st) {\n vis[node] = 1;\n for (int neighbor : adj[node]) {\n if (!vis[neighbor] && st.find(neighbor) == st.end()) {\n count++;\n dfs(neighbor, adj, vis, st);\n }\n }\n }\n \n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<int> adj[n];\n unordered_set<int> st;\n \n for (int node : restricted) {\n st.insert(node);\n }\n \n for (const auto& edge : edges) {\n int u = edge[0];\n int v = edge[1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n \n vector<int> vis(n, 0);\n if (st.find(0) == st.end()) {\n dfs(0, adj, vis, st);\n }\n return count;\n }\n};",
"memory": "179700"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 2 | {
"code": "class Solution {\npublic:\n\n vector<vector<int>> g;\n vector<bool> r, vis;\n void dfs(int node) {\n if(r[node]) return;\n vis[node] = true;\n for(auto nbr: g[node]) {\n if(!vis[nbr]) {\n dfs(nbr);\n }\n }\n }\n\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n g.resize(n);\n for(int i = 0; i+1 < n; ++i) {\n g[edges[i][0]].push_back(edges[i][1]);\n g[edges[i][1]].push_back(edges[i][0]);\n }\n r.assign(n, 0);\n for(auto i: restricted) r[i] = true;\n vis.assign(n, 0);\n dfs(0);\n int ans = 0;\n for(int i = 0; i < n; ++i) {\n if(vis[i]) ++ans;\n }\n return ans;\n }\n};",
"memory": "181020"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 2 | {
"code": "class Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<vector<int>> adj(n);\n for (const auto& e : edges) {\n adj[e[0]].push_back(e[1]);\n adj[e[1]].push_back(e[0]);\n }\n\n vector<bool> visited(n, false);\n for (int r : restricted) visited[r] = true;\n \n function<int(int)> dfs = [&](int root)\n {\n visited[root] = true;\n int count = 1;\n for (int nbr : adj[root]) {\n if (!visited[nbr]) count += dfs(nbr);\n }\n return count;\n };\n\n return dfs(0);\n }\n};",
"memory": "181020"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 2 | {
"code": "class Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n unordered_map<int,vector<int>> graph;\n unordered_set<int> nogo(restricted.begin(), restricted.end());\n unordered_map<int,bool> seen;\n\n for (int i = 0; i < edges.size(); i++) {\n vector<int> edge = edges[i];\n int u = edges[i][0];\n int v = edges[i][1];\n\n /* do not add an edge that originates or ends at a restricted node */\n if (nogo.find(u) != nogo.end() || nogo.find(v) != nogo.end())\n continue;\n\n graph[u].push_back(v);\n graph[v].push_back(u);\n }\n\n stack<int> s;\n int reach = 0;\n s.push(0);\n\n while (!s.empty()) {\n int node = s.top();\n s.pop();\n seen[node] = true;\n reach++;\n vector<int> &neighbors = graph[node];\n for (int i = 0; i < neighbors.size(); i++) {\n if (!seen[neighbors[i]])\n s.push(neighbors[i]);\n }\n }\n \n return reach;\n }\n};",
"memory": "182340"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 2 | {
"code": "class Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<int> adj[n];\n for(auto it:edges)\n {\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n unordered_set<int> st;\n for(auto it:restricted)\n st.insert(it);\n int ans=0;\n queue<int> q;\n q.push(0);\n st.insert(0);\n while(!q.empty())\n {\n int x=q.size();\n ans+=x;\n while(x--)\n {\n int node=q.front();\n q.pop();\n for(auto it:adj[node])\n {\n if(st.find(it)==st.end())\n {\n st.insert(it);\n q.push(it);\n }\n }\n }\n }\n return ans;\n }\n};",
"memory": "182340"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 2 | {
"code": "\n\nclass Solution {\n\nvector<vector<int>> adj;\nunordered_set<int> res;\n\n\nint dfs(int u, int pa) {\n\tif (res.count(u) > 0) {\n\t\treturn 0;\n\t}\n\tint cnt = 1;\n\tfor (int v: adj[u]) {\n\t\tif (v != pa) {\n\t\t\tcnt += dfs(v, u);\n\t\t}\n\t}\n\treturn cnt;\n}\n\n\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n\t\tadj.resize(n);\n\t\tfor (vector<int> &e: edges) {\n\t\t\tint u = e[0], v = e[1];\n\t\t\tadj[u].push_back(v);\t\n\t\t\tadj[v].push_back(u);\n\t\t}\t \n\t\tfor (int u: restricted) {\n\t\t\tres.insert(u);\n\t\t}\n\t\treturn dfs(0, -1);\n }\n};\n\n",
"memory": "183660"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 2 | {
"code": "class Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<vector<int>> adj(n);\n for (int i = 0; i < edges.size(); i++) {\n int u = edges[i][0];\n int v = edges[i][1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n\n unordered_set<int> set;\n for (int i = 0; i < restricted.size(); i++) {\n set.insert(restricted[i]);\n }\n\n if (set.find(0) != set.end()) return 0;\n return dfs(0, -1, set, adj);\n }\n\n int dfs(int u, int p, unordered_set<int>& set, vector<vector<int>>& adj) {\n\n int count = 1;\n for (int v: adj[u]) {\n if (v == p) continue;\n if (set.find(v) == set.end()) {\n count += dfs(v, u, set, adj);\n }\n }\n return count;\n }\n};",
"memory": "183660"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 2 | {
"code": "class Solution {\npublic:\n void dfs(int node,vector<vector<int>>&adj,vector<int>&vis,unordered_set<int>&st,int &ans){\n vis[node]=1;\n if(st.find(node)==st.end()){\n ans++;\n }\n else{\n return;\n }\n for(auto it:adj[node]){\n if(vis[it]==0){\n dfs(it,adj,vis,st,ans);\n }\n }\n return;\n }\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& arr) {\n unordered_set<int>st;\n for(int i=0; i<arr.size(); i++){\n st.insert(arr[i]);\n }\n vector<vector<int>>adj(n);\n for(int i=0; i<edges.size(); i++){\n adj[edges[i][0]].push_back(edges[i][1]);\n adj[edges[i][1]].push_back(edges[i][0]);\n }\n vector<int>vis(n,0);\n int ans=0;\n dfs(0,adj,vis,st,ans);\n return ans;\n }\n};",
"memory": "184980"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 2 | {
"code": "class Solution {\npublic:\n void dfs(int node,vector<vector<int>>&adj,vector<int>&vis,unordered_set<int>&st,int &ans){\n vis[node]=1;\n if(st.find(node)==st.end()){\n ans++;\n }\n else{\n return;\n }\n for(auto it:adj[node]){\n if(vis[it]==0){\n dfs(it,adj,vis,st,ans);\n }\n }\n return;\n }\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& arr) {\n unordered_set<int>st;\n for(int i=0; i<arr.size(); i++){\n st.insert(arr[i]);\n }\n vector<vector<int>>adj(n);\n for(int i=0; i<edges.size(); i++){\n adj[edges[i][0]].push_back(edges[i][1]);\n adj[edges[i][1]].push_back(edges[i][0]);\n }\n vector<int>vis(n,0);\n int ans=0;\n dfs(0,adj,vis,st,ans);\n return ans;\n }\n};",
"memory": "184980"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 2 | {
"code": "class Solution {\npublic:\n int c=0;\n map<int,int> mp;\n int result(int n,vector<vector<int>>&edges,vector<int> &restricted){\n vector<vector<int>> adjacency(n);\n for(int i=0;i<edges.size();i++){\n int u=edges[i][0];\n int v=edges[i][1];\n adjacency[u].push_back(v);\n adjacency[v].push_back(u);\n }\n for(int i=0;i<restricted.size();i++){\n mp[restricted[i]]=1;\n }\n vector<int> visited(n,0);\n dfs(visited,adjacency,0);\n return c;\n }\n void dfs(vector<int> &visited,vector<vector<int>> &adjacency,int node){\n if(visited[node]==0 && mp.find(node)==mp.end()){\n visited[node]=1;\n c+=1;\n for(int i=0;i<adjacency[node].size();i++){\n dfs(visited,adjacency,adjacency[node][i]);\n }\n }\n }\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n return result(n,edges,restricted);\n }\n};",
"memory": "186300"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 2 | {
"code": "class Solution {\npublic:\n\n void dfs(int i , vector<int>adj[] , int &count , vector<bool>&vis){\n vis[i] = true;\n count++;\n for(auto it : adj[i]){\n if(vis[it]==false){\n dfs(it , adj , count , vis);\n }\n }\n }\n\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<int>adj[n];\n for(auto it : edges){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n\n }\n vector<bool>vis(n , false);\n for(auto it : restricted){\n vis[it] = true;\n }\n int count = 0;\n dfs(0 , adj , count , vis);\n return count;\n }\n};",
"memory": "186300"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 2 | {
"code": "class Solution {\n int dfs(int n, unordered_map<int, vector<int>>& edges, vector<bool>& visited) {\n visited[n] = true;\n int accum = 1;\n for (auto neighbor : edges[n]) {\n if (!visited[neighbor]) \n accum += dfs(neighbor, edges, visited);\n }\n return accum;\n }\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n unordered_map<int, vector<int>> adjList;\n unordered_set<int> forbiddenSet(restricted.begin(), restricted.end());\n for (auto &edge : edges) {\n if (forbiddenSet.count(edge[0]) || forbiddenSet.count(edge[1])) continue;\n adjList[edge[0]].push_back(edge[1]);\n adjList[edge[1]].push_back(edge[0]);\n }\n //if (adjList.empty()) return 1;\n vector<bool> visited(n, false);\n return dfs(0, adjList, visited);\n }\n};",
"memory": "187620"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 2 | {
"code": "class Solution {\nprivate:\n void dfs(int node, vector<vector<int>>& adj, vector<int>& visited, unordered_set<int>& restrictedSet, int& ans) {\n visited[node] = 1;\n ans++;\n\n for (int neighbor : adj[node]) {\n if (!visited[neighbor] && !restrictedSet.count(neighbor)) {\n dfs(neighbor, adj, visited, restrictedSet, ans);\n }\n }\n }\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<vector<int>> adj(n);\n for (const auto& edge : edges) {\n int u = edge[0];\n int v = edge[1];\n\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n\n unordered_set<int> restrictedSet(restricted.begin(), restricted.end());\n\n vector<int> visited(n, 0);\n int ans = 0;\n dfs(0, adj, visited, restrictedSet, ans);\n\n return ans;\n }\n};",
"memory": "187620"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 2 | {
"code": "class Solution {\npublic:\n int count=1;\n int dfs(int node,vector<vector<int>>&adj,vector<int>&vis,set<int>&st){\n vis[node]=1;\n for(auto it:adj[node]){\n if(!vis[it] and st.find(it)==st.end()){\n count=1+dfs(it,adj,vis,st);\n }\n }\n return count;\n }\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<vector<int>>adj(n);\n vector<int>vis(n,0);\n set<int>st;\n for(auto it:restricted){\n st.insert(it);\n }\n for(int i=0;i<edges.size();i++){\n adj[edges[i][0]].push_back(edges[i][1]);\n adj[edges[i][1]].push_back(edges[i][0]);\n }\n\n return dfs(0,adj,vis,st);\n }\n};",
"memory": "188940"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 2 | {
"code": "class Solution {\npublic:\n void dfs(vector<vector<int>> &adj,int node,vector<int> &vis,set<int> &restr)\n {\n vis[node] = 1;\n for(auto it: adj[node])\n {\n if(!vis[it] && restr.find(it)==restr.end())\n {\n vis[it] = 1;\n dfs(adj,it,vis,restr);\n }\n }\n }\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<vector<int>> adj(n);\n for(int i=0;i<edges.size();i++)\n {\n adj[edges[i][0]].push_back(edges[i][1]);\n adj[edges[i][1]].push_back(edges[i][0]);\n }\n set<int> restr;\n for(int num: restricted)\n {\n restr.insert(num);\n }\n int cnt = 0;\n vector<int> vis(n,0);\n dfs(adj,0,vis,restr);\n for(int i=0;i<n;i++)\n {\n if(vis[i]==1)\n {\n cnt++;\n }\n }\n return cnt;\n }\n};",
"memory": "188940"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 2 | {
"code": "class Solution {\npublic:\n int count;\n void dfs(int source, vector<vector<int>>& adj, vector<bool>& visited){\n visited[source] = true;\n count++;\n for(int neighbor: adj[source]){\n if(!visited[neighbor]) dfs(neighbor, adj, visited);\n }\n }\n\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<vector<int>> adj(n);\n unordered_set<int> restrictedSet(restricted.begin(), restricted.end());\n for(auto e: edges){\n if(restrictedSet.count(e[0]) || restrictedSet.count(e[1])) continue;\n adj[e[0]].push_back(e[1]);\n adj[e[1]].push_back(e[0]);\n }\n count = 0;\n vector<bool> visited(n, false);\n dfs(0, adj, visited);\n return count;\n }\n};",
"memory": "190260"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 2 | {
"code": "class Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n unordered_map<int, vector<int>> connected;\n for (auto edge:edges) {\n connected[edge[0]].push_back(edge[1]);\n connected[edge[1]].push_back(edge[0]);\n }\n vector<bool> seen(n, false);\n for (auto node: restricted) seen[node]=true;\n // dfs part;\n int ans=0;\n stack<int> depthnodes;\n depthnodes.push(0);\n seen[0]=true;\n while (!depthnodes.empty()) {\n int node=depthnodes.top();\n depthnodes.pop();\n ans++;\n for (auto con:connected[node]) {\n if (!seen[con]) {\n seen[con]=true;\n depthnodes.push(con);\n }\n }\n }\n return ans;\n }\n};",
"memory": "190260"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 2 | {
"code": "class Solution {\npublic:\n void dfs(vector<int>* graph, unordered_set<int> &res, int u, int p, int &cnt){\n if(res.find(u) != res.end()) return;\n cnt++;\n for(auto v : graph[u]){\n if(v == p ) continue;\n dfs(graph,res,v,u,cnt);\n }\n\n return;\n\n }\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n unordered_set<int> res;\n for(auto x : restricted){\n res.insert(x);\n }\n\n vector<int> graph[n];\n\n for(auto e : edges){\n graph[e[0]].push_back(e[1]);\n graph[e[1]].push_back(e[0]);\n }\n\n int cnt = 0;\n\n dfs(graph, res, 0, -1, cnt);\n\n return cnt;\n }\n};",
"memory": "191580"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 2 | {
"code": "class Solution {\nprivate:\n unordered_map<int, int> mp;\n int dfs(int node, int par, vector<int> adj[]) {\n int cnt = 1;\n for(int child: adj[node]) {\n if(child == par || mp.find(child) != mp.end())\n continue;\n cnt += dfs(child, node, adj);\n }\n return cnt;\n }\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<int> adj[n];\n for(auto it: edges) {\n int u = it[0], v = it[1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n for(int it: restricted)\n mp[it]++;\n\n return dfs(0, -1, adj);\n }\n};",
"memory": "191580"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 2 | {
"code": "class Solution {\npublic:\n int cnt=0;\n unordered_map <int,int> mp;\n void bfs(int node, vector <int> adj[],vector <int> &vis){\n vis[node]=1;\n cnt++;\n for(auto it: adj[node]){\n if(vis[it]!=1 && mp.find(it)==mp.end()){\n bfs(it,adj,vis);\n }\n }\n }\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector <int> adj[n];\n for(auto it: edges){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n for(auto it: restricted){\n mp[it]++;\n }\n vector <int> vis(n,0);\n bfs(0,adj,vis);\n return cnt;\n }\n};",
"memory": "192900"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 2 | {
"code": "class Solution {\npublic:\nint ans=0;\nvoid dfs(vector<int> adj[],int node,vector<int>& vis,unordered_set<int>& st){\n if(st.find(node)==st.end()){\n ans++;\n }\n vis[node]=1;\n for(auto it:adj[node]){\n if(vis[it]==0 && st.find(it)==st.end()){\n dfs(adj,it,vis,st);\n }\n }\n}\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n unordered_set<int> st;\n for(auto it:restricted){\n st.insert(it);\n }\n vector<int> adj[n];\n for(auto it:edges){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n vector<int> vis(n);\n dfs(adj,0,vis,st);\n // int ans=0;\n // for(int i=0;i<n;i++){\n // if(vis[i]==0){\n // ans++;\n // }\n // }\n return ans;\n }\n};",
"memory": "192900"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 2 | {
"code": "/*\r\n * @lc app=leetcode id=2368 lang=cpp\r\n *\r\n * [2368] Reachable Nodes With Restrictions\r\n */\r\n\r\n\r\nclass Solution {\r\npublic:\r\n vector<int> ans;\r\n \r\n void dfs(int src, const vector<vector<int>>& adj, const unordered_set<int>& res, vector<bool>& vis) {\r\n vis[src] = true;\r\n ans.push_back(src);\r\n\r\n for (auto i : adj[src]) {\r\n if (!vis[i] && res.find(i) == res.end()) {\r\n dfs(i, adj, res, vis);\r\n }\r\n }\r\n }\r\n\r\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\r\n unordered_set<int> res(restricted.begin(), restricted.end());\r\n \r\n vector<vector<int>> adj(n);\r\n for (const auto& edge : edges) {\r\n adj[edge[0]].push_back(edge[1]);\r\n adj[edge[1]].push_back(edge[0]); // Add edge in both directions\r\n }\r\n \r\n vector<bool> vis(n, false);\r\n\r\n dfs(0, adj, res, vis);\r\n\r\n return ans.size();\r\n }\r\n};\r\n// @lc code=end\r\n",
"memory": "194220"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 2 | {
"code": "class Solution {\npublic:\nint fun(int src,int p,vector<int>adj[],unordered_map<int,int>&m )\n{\n int cnt=1;\n for(auto it:adj[src])\n {\n if(m.find(it)==m.end()&&it!=p)\n {\n cnt+=fun(it,src,adj,m);\n }\n }\n return cnt;\n}\n int reachableNodes(int n, vector<vector<int>>& e, vector<int>& r) {\n vector<int>adj[n];\n for(auto it:e)\n {\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n unordered_map<int,int>m;\n for(auto it:r)\n m[it]++;\n return fun(0,-1,adj,m);\n\n \n }\n};",
"memory": "194220"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 2 | {
"code": "class Solution {\npublic:\n\n int dfs(vector<int>adj[],unordered_map<int,int>&mp,int node,vector<int>&vis){\n vis[node]=1;\n if(mp.find(node)!=mp.end()){\n return 0;\n }\n\n int ans = 0 ;\n\n for(int it : adj[node]){\n if(!vis[it]&&mp.find(it)==mp.end()){\n ans = ans + 1 + dfs(adj,mp,it,vis);\n\n }\n }\n\n return ans ;\n }\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n\n vector<int>adj[n];\n vector<int>vis(n,0);\n unordered_map<int,int>mp;\n\n for(int it:restricted){\n mp[it]=1;\n }\n\n for(auto it:edges){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n\n return dfs(adj,mp,0,vis)+1;\n \n }\n};",
"memory": "195540"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 2 | {
"code": "class Solution {\npublic:\n void dfs(vector<int> adj[], int node, vector<int>& vis, unordered_set<int>& restricted, int& cnt) {\n vis[node] = 1;\n cnt++;\n\n for (auto neighbor : adj[node]) {\n if (!vis[neighbor] && restricted.find(neighbor) == restricted.end()) {\n dfs(adj, neighbor, vis, restricted, cnt);\n }\n }\n }\n\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<int> adj[n];\n for (auto i : edges) {\n adj[i[0]].push_back(i[1]);\n adj[i[1]].push_back(i[0]);\n }\n vector<int> vis(n, 0);\n unordered_set<int> restrictedSet(restricted.begin(), restricted.end());\n\n int cnt = 0;\n dfs(adj, 0, vis, restrictedSet, cnt);\n\n return cnt;\n }\n};\n",
"memory": "195540"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 3 | {
"code": "// simple dfs and bfs\n\nclass Solution {\npublic:\n int cnt=0;\n void dfs(int node,vector<int>adj[],vector<int>&vis,set<int>&st){\n vis[node]=1;\n cnt++;\n for(auto it:adj[node]){\n if(!vis[it] && st.find(it)==st.end())dfs(it,adj,vis,st);\n }\n }\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n set<int>st(restricted.begin(),restricted.end());\n vector<int>adj[n];\n for(auto it:edges){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n vector<int>vis(n,0);\n dfs(0,adj,vis,st);\n return accumulate(vis.begin(),vis.end(),0);\n return cnt;\n }\n};\n\nclass Solution1 {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<vector<int>> adj(n); \n for(auto& edge : edges) {\n adj[edge[0]].push_back(edge[1]);\n adj[edge[1]].push_back(edge[0]);\n }\n unordered_set<int> restrictedSet(restricted.begin(), restricted.end());\n vector<bool> visited(n, false);\n queue<int> que;\n int source = 0;\n que.push(source); \n int count = 0;\n while(que.size()) {\n int node = que.front(); que.pop();\n visited[node] = true; \n if(restrictedSet.find(node) == restrictedSet.end()) {\n count++;\n for(auto& adjacentNode : adj[node]) {\n if(!visited[adjacentNode] && restrictedSet.find(adjacentNode) == restrictedSet.end()) {\n que.push(adjacentNode);\n }\n }\n }\n }\n \n return count;\n }\n};",
"memory": "196860"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 3 | {
"code": "class Solution {\npublic:\n void dfs(int node, vector<int> &vis, vector<int> adj[], set<int>& res){\n vis[node] = 1;\n for(auto it:adj[node]){\n if(!vis[it] and !res.count(it)){\n dfs(it, vis, adj, res);\n }\n }\n }\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<int> vis(n, 0);\n vector<int> adj[n];\n for(auto it:edges){\n int u = it[0];\n int v = it[1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n set<int> res;\n for(auto it:restricted)\n res.insert(it);\n dfs(0, vis, adj, res);\n int ans = 0;\n for(auto it:vis){\n ans+=it;\n }\n return ans;\n }\n};",
"memory": "196860"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 3 | {
"code": "class Solution {\npublic:\n set<int> s;\n vector<vector<int>> g;\n int dfs(int nn,int pp){\n if(s.find(nn)!=s.end()) return 0;\n int ans=1;\n for(auto v:g[nn]){\n if(v==pp) continue;\n ans+=dfs(v,nn);\n }\n\n return ans;\n }\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n\n for(auto nn:restricted){\n s.insert(nn);\n }\n g.resize(n);\n\n for(auto e:edges){\n g[e[0]].push_back(e[1]);\n g[e[1]].push_back(e[0]);\n }\n\n return dfs(0,-1);\n \n }\n};",
"memory": "198180"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 3 | {
"code": "class Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n mNodes = n;\n mVisited = std::vector<bool>(mNodes, false);\n std::unordered_set<int> restrictedNodes(restricted.begin(), restricted.end());\n for(const auto connection : edges){\n auto node1 = connection[0];\n auto node2 = connection[1];\n\n if(!restrictedNodes.contains(node1) && !restrictedNodes.contains(node2)){\n mGraph[node1].push_back(node2);\n mGraph[node2].push_back(node1);\n }\n }\n return dfs(0);\n }\n\n int dfs(const int node){\n mVisited[node] = true;\n int visitedNum = 1;\n for(const auto neighbor : mGraph[node]){\n if(!mVisited[neighbor]){\n visitedNum += dfs(neighbor);\n }\n }\n return visitedNum;\n }\n \n int mNodes = -1;\n std::vector<bool> mVisited;\n std::unordered_map<int, std::vector<int>> mGraph;\n};",
"memory": "198180"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 3 | {
"code": "class Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<int>adj[100001];\n queue<int>q;\n unordered_map<int,bool>mp;\n for(auto i : restricted) mp[i] = true;\n q.push(0);\n for(auto i : edges){\n adj[i[0]].push_back(i[1]);\n adj[i[1]].push_back(i[0]);\n }\n vector<int>vis(100001, 0);\n vis[0] = 1;\n int ans = 0;\n while(!q.empty()){\n int node = q.front();\n q.pop();\n ans++;\n for(auto nei : adj[node]){\n if(!vis[nei] && mp.find(nei)==mp.end()){\n q.push(nei);\n vis[nei] = 1;\n }\n }\n }\n return ans;\n }\n};",
"memory": "199500"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 3 | {
"code": "class Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n \n //create adjList\n unordered_map< int, list<int>> adjList;\n for(auto &edge :edges)\n {\n int u = edge[0];\n int v = edge[1];\n\n adjList[u].push_back(v);\n adjList[v].push_back(u);\n }\n\n vector<bool> visited(n, false);\n\n //remove restricted nodes\n //get restrictes nodes in hashtable\n unordered_map<int, int> restrictedNodes;\n for(int i = 0; i < restricted.size(); ++i)\n {\n restrictedNodes[restricted[i]]++;\n }\n\n queue<int> nodes;\n nodes.push(0);\n visited[0] = true;\n int count = 0;\n while(!nodes.empty())\n {\n int currNode = nodes.front();\n nodes.pop();\n\n count++;\n\n for(int &neighbour : adjList[currNode])\n {\n cout << neighbour;\n if(!visited[neighbour] && restrictedNodes.find(neighbour) == restrictedNodes.end())\n {\n visited[neighbour] = true;\n nodes.push(neighbour);\n }\n }\n }\n return count;\n }\n};",
"memory": "199500"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 3 | {
"code": "class Solution {\n\npublic:\n int nodes;\n\npublic:\n void dfs(int src, int par, set<int>& restrictedNodes, vector<vector<int> >&adj) {\n nodes++;\n for(int child : adj[src]) {\n if(child == par) continue;\n if(restrictedNodes.find(child) == restrictedNodes.end()) {\n dfs(child, src, restrictedNodes, adj);\n }\n }\n }\n\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<vector<int> > adj(n);\n set<int> restrictedNodes;\n for(int ele : restricted) restrictedNodes.insert(ele);\n for(auto edge : edges) {\n adj[edge[0]].push_back(edge[1]);\n adj[edge[1]].push_back(edge[0]);\n }\n nodes = 0;\n dfs(0, -1, restrictedNodes, adj);\n return nodes;\n }\n};",
"memory": "200820"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 3 | {
"code": "class Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<vector<int>> alist(n, vector<int>());\n for (auto e: edges) {\n alist[e[0]].push_back(e[1]);\n alist[e[1]].push_back(e[0]);\n }\n\n int ans = 0;\n set<int> rset = set<int>(restricted.begin(), restricted.end());\n dfs(0, -1, ans, alist, rset);\n return ans;\n }\n\n void dfs(int node, int par, int& ct, vector<vector<int>>& alist, set<int>& restricted) {\n ct++;\n for (auto nei: alist[node]) {\n if (nei == par) continue;\n if (restricted.find(nei) != restricted.end()) continue;\n dfs(nei, node, ct, alist, restricted);\n }\n }\n};",
"memory": "200820"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 3 | {
"code": "class Solution {\npublic:\n int dfs(int node,vector<vector<int>>&adj,vector<int>&vis,map<int,int>&m){\n int ct=1;\n vis[node]=1;\n for(auto it:adj[node]){\n if(m.find(it)==m.end() && vis[it]==0){\n \n ct+=dfs(it,adj,vis,m);\n }\n }\n return ct;\n\n }\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<vector<int>>adj(n);\n vector<int>vis(n,0);\n map<int,int>m;\n for(int i=0;i<restricted.size();i++){\n m[restricted[i]]++;\n }\n for(auto it:edges){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n int ct=dfs(0,adj,vis,m);\n return ct;\n }\n};",
"memory": "202140"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 3 | {
"code": "class Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n unordered_map<int, vector<int>>graph;\n for(vector<int>edge :edges){\n graph[edge[0]].push_back(edge[1]);\n graph[edge[1]].push_back(edge[0]);\n }\n int count = 1;\n stack<int> pile;\n set<int> nope;\n for(int res: restricted){\n nope.insert(res);\n }\n vector<bool> seen(n,false);\n pile.push(0);\n seen[0] = true;\n while(!pile.empty()){\n int node = pile.top();\n pile.pop();\n for(int neighbor :graph[node]){\n if(seen[neighbor]==false && nope.find(neighbor)==nope.end()){\n count++;\n pile.push(neighbor);\n seen[neighbor] = true;\n }\n }\n }\n return count;}\n};",
"memory": "202140"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 3 | {
"code": "class Solution {\npublic:\n void dfs(vector <vector <int>> &adj,map <int,int> &r,int node,int parent,int &count){\n\n if(r[node])\n return ;\n\n count++;\n for(auto it: adj[node]){\n if(it!=parent){\n dfs(adj,r,it,node,count);\n }\n }\n }\n\n\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n \n\n vector <vector <int>> adj(n);\n\n for(auto it: edges){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n\n int count=0;\n map <int,int> r;\n for(auto it: restricted){\n r[it]++;\n }\n\n dfs(adj,r,0,-1,count);\n return count;\n\n\n }\n};",
"memory": "210060"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 3 | {
"code": "class Solution {\npublic:\n\n void dfs(int x,vector<int> &visited,unordered_set<int> &st,unordered_map<int,vector<int>> &graph)\n {\n visited[x]=1;\n for(auto child : graph[x])\n {\n if(visited[child]==0 && st.find(child)==st.end())\n {\n dfs(child,visited,st,graph);\n }\n }\n }\n\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n unordered_map<int,vector<int>> graph;\n for(int i=0;i<edges.size();i++)\n {\n int x=edges[i][0],y=edges[i][1];\n graph[x].push_back(y);\n graph[y].push_back(x);\n }\n vector<int> visited(n,0);\n unordered_set<int> st;\n for(int i=0;i<restricted.size();i++)\n {\n st.insert(restricted[i]);\n }\n dfs(0,visited,st,graph);\n int ans=0;\n for(int i=0;i<n;i++)\n {\n if(visited[i]==1) ans++;\n }\n return ans;\n }\n};",
"memory": "210060"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 3 | {
"code": "class Solution {\npublic:\n\n void dfs(int node,vector<int> adj[],vector<bool> &visited,unordered_map<int,bool> &mp){\n visited[node]=true;\n for(auto it:adj[node]){\n if(!visited[it] && !mp[it]){\n dfs(it,adj,visited,mp);\n }\n }\n }\n\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n unordered_map<int,bool> mp;\n for(auto it:restricted){\n mp[it]=true;\n }\n if(mp[0]) return 0;\n vector<int> adj[n];\n for(auto it:edges){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n vector<bool> visited(n,false);\n dfs(0,adj,visited,mp);\n int ans=0;\n for(auto it:visited){\n if(it) ans++;\n }\n return ans;\n }\n};",
"memory": "211380"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 3 | {
"code": "class Solution {\n unordered_map<int, vector<int>> graph;\n vector<bool> seen;\n unordered_set<int> restricted_set;\n \npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n graph.reserve(n);\n seen.resize(n, false);\n restricted_set = unordered_set<int>{ restricted.begin(), restricted.end() };\n \n for (const auto& vertices : edges) {\n int left{ vertices[0] };\n int right{ vertices[1] };\n graph[left].push_back(right);\n graph[right].push_back(left);\n }\n \n seen[0] = true;\n \n return dfs(0);\n }\n \n int dfs(int node) {\n int count{1};\n for (int vertice : graph[node]) {\n if (!seen[vertice] && !restricted_set.contains(vertice)) {\n seen[vertice] = true;\n count += dfs(vertice);\n }\n }\n return count;\n }\n};",
"memory": "211380"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 3 | {
"code": "class Solution {\npublic:\n unordered_map<int,vector<int>>m;\nunordered_map<int,int>mp;\n int c=0;\n /* void b(int i){\n if(mp[i]>0)\n return;\n mp[i]++;\n c++;\n for(auto j:m[i]){\n if(mp[j]==0)\n b(j);}}*/\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& r) {\n // unordered\n for(auto i:edges){\n m[i[0]].push_back(i[1]);\nm[i[1]].push_back(i[0]);\n }\n for(auto i:r)\n mp[i]++;\n // b(0);\n queue<int>q;\n q.push(0);\n while(!q.empty()){\n int i=q.front();\n c++;\n q.pop();\n mp[i]++;\n for(auto j:m[i]){\n if(mp[j]==0)\n q.push(j);}}\n return c;\n }\n};",
"memory": "212700"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 3 | {
"code": "class Solution {\npublic:\n void dfs(unordered_map<int,vector<int> > &adj,int node,vector<bool> &vis,vector<bool> &res,int &count){\n vis[node]=true;\n count++;\n for(auto nbr:adj[node]){\n if(vis[nbr]==false && res[nbr]==false){\n dfs(adj,nbr,vis,res,count);\n }\n }\n }\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<bool> res(n,false);\n vector<bool> vis(n,false);\n for(int i=0;i<restricted.size();i++){\n res[restricted[i]]=true;\n }\n unordered_map<int,vector<int> > adj;\n for(int i=0;i<edges.size();i++){\n adj[edges[i][0]].push_back(edges[i][1]);\n adj[edges[i][1]].push_back(edges[i][0]);\n }\n int count=0;\n dfs(adj,0,vis,res,count);\n return count;\n }\n};",
"memory": "212700"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 3 | {
"code": "class Solution {\npublic:\n void dfs(int node, unordered_map<int, vector<int>>& adj, vector<int>& vis, set<int>& res) {\n vis[node] = 1;\n\n for (auto nbr : adj[node]) {\n if ( vis[nbr] == 1 || res.find(nbr) != res.end()) continue;\n dfs(nbr, adj, vis, res);\n }\n }\n\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& rest) {\n unordered_map<int, vector<int>> adj;\n for (auto& e : edges) {\n adj[e[0]].push_back(e[1]);\n adj[e[1]].push_back(e[0]);\n }\n\n vector<int> vis(n, 0);\n set<int> res;\n for(auto i: rest){\n res.insert(i);\n }\n dfs(0, adj, vis, res); // Start DFS from node 0\n\n return accumulate(vis.begin(), vis.end(), 0);\n }\n};\n",
"memory": "214020"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 3 | {
"code": "class Solution {\npublic:\n void rec(int s,map<int,vector<int>>&a,vector<int>&vis,int &res){\n if(vis[s])return;\n vis[s]=1;\n res++;\n for(auto & c: a[s]){\n if(!vis[c]){\n rec(c,a,vis,res);\n }\n }\n }\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n map<int,vector<int>>a;\n for(auto c:edges){\n a[c[0]].push_back(c[1]);\n a[c[1]].push_back(c[0]);\n }\n int res=0;\n vector<int>vis(n+1,0);\n for(auto c:restricted)vis[c]=1;\n rec(0,a,vis,res);\n return res;\n }\n};",
"memory": "214020"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 3 | {
"code": "class Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n unordered_map<int,vector<int>>adj;\n for(auto it:edges){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n\n vector<int>vis(n,0);\n int ans = 1;\n unordered_map<int,bool>res;\n for(auto it:restricted){\n res[it] = 1;\n }\n queue<int>q;\n q.push(0);\n vis[0] = 1;\n while(!q.empty()){\n int val = q.front();\n q.pop();\n for(auto it:adj[val]){\n if(vis[it] == 0 && res[it] == 0){\n q.push(it);\n vis[it] = 1;\n ans++;\n }\n }\n }\n return ans;\n }\n};",
"memory": "215340"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 3 | {
"code": "class Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n map<int, vector<int>> adj;\n map<int, bool>rest;\n for(int i=0; i<restricted.size(); i++) {\n rest[restricted[i]] = true;\n }\n for(int i=0; i<edges.size(); i++) {\n int u = edges[i][0];\n int v = edges[i][1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n queue<int> q;\n q.push(0);\n if(rest[0]) {\n return 0;\n }\n int res = 0;\n map<int, bool>visited;\n visited[0] = true;\n while(!q.empty()) {\n int front = q.front();\n q.pop();\n res++;\n for(int i=0; i<adj[front].size(); i++) {\n int v = adj[front][i];\n if(!rest[v] && !visited[v]) {\n q.push(v);\n visited[v] = true;\n }\n }\n }\n return res;\n }\n};",
"memory": "215340"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 3 | {
"code": "class Solution {\npublic:\n void dfs(int from,vector<int> &vis,set<int> &st,unordered_map<int, vector<int>> &adj,int n,int &count){\n vis[from]=1;\n count++;\n for(auto to:adj[from]){\n //if not already visited and not restricted\n if(!vis[to] && st.find(to)==st.end()){\n dfs(to,vis,st,adj,n,count);\n }\n }\n }\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n //This problem can be solved using the dfs approach. We can first make the adjacency list and then make a visited\n //array to keep tack of the visited.\n vector<int> visited(n,0);\n unordered_map<int, vector<int>> adj;\n for (auto& it : edges) {\n int u = it[0];\n int v = it[1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n set<int> st(restricted.begin(),restricted.end());\n int count=0;\n dfs(0,visited,st,adj,n,count);\n //now iterate the visted vector and count the number of zeros.\n \n \n return count;\n }\n};",
"memory": "216660"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 3 | {
"code": "class Solution {\npublic:\n vector<bool> seen;\n unordered_map<int, vector<int>> graph;\n int answer = 0; \n \n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n \n seen = vector(n, false);\n for (int r : restricted)\n {\n seen[r] = true;\n }\n \n seen[0] = true;\n answer++;\n \n for (vector<int> edge: edges) \n {\n int x = edge[0];\n int y = edge[1];\n \n graph[x].push_back(y);\n graph[y].push_back(x); \n \n }\n \n dfs(0);\n \n \n return answer;\n }\n \n void dfs(int node)\n {\n \n for (int neighbor: graph[node])\n {\n if (!seen[neighbor])\n {\n answer++;\n seen[neighbor] = true;\n dfs(neighbor);\n }\n }\n }\n \n \n};",
"memory": "216660"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 3 | {
"code": "class Solution {\npublic:\n unordered_map<int,vector<int>> tree;\n unordered_set<int> valSet;\n vector<bool> seen;\n int reachable = 0;\n \n void dfs(int node) {\n for(auto neighbour: tree[node]) {\n if(valSet.find(neighbour) == valSet.end() && !seen[neighbour]) {\n seen[neighbour] = true;\n reachable++;\n dfs(neighbour);\n }\n }\n }\n \n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n\n for(int i =0;i<edges.size();i++) {\n tree[edges[i][0]].push_back(edges[i][1]);\n tree[edges[i][1]].push_back(edges[i][0]);\n }\n \n for(int i =0;i<restricted.size();i++) {\n valSet.insert(restricted[i]);\n }\n \n seen = vector(n,false);\n \n //Start from node 0\n seen[0] = true;\n reachable++;\n \n //dfs from 0\n dfs(0);\n \n return reachable;\n }\n};",
"memory": "217980"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 3 | {
"code": "class Solution {\npublic:\n std::unordered_map<int, std::vector<int>> graph;\n std::unordered_set<int> restrictedSet;\n vector<bool> seen;\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n //create the graph\n restrictedSet = std::unordered_set<int>(restricted.begin(), restricted.end());\n seen.resize(n, false);\n for(vector<int>& edge : edges){\n graph[edge[0]].push_back(edge[1]);\n graph[edge[1]].push_back(edge[0]);\n }\n \n seen[0] = true;\n return dfs(0);\n }\n \n int dfs(int node){\n int ans = 1;\n for(int neighboor : graph[node]){\n if(!seen[neighboor] && !restrictedSet.count(neighboor)){\n seen[neighboor] = true;\n ans += dfs(neighboor);\n }\n }\n return ans;\n }\n};",
"memory": "217980"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 3 | {
"code": "class Solution \n{\n // Graph hashmap to store the graph from array of edges\n unordered_map<int, vector<int>> graph;\n // Seen vector to keep track of all the visited nodes\n vector<bool> seen;\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) \n {\n // Create a hash,ap graph from the array of edges representation\n for(vector<int> edge : edges)\n {\n int x = edge[0];\n int y = edge[1];\n graph[x].push_back(y);\n graph[y].push_back(x);\n }\n\n // Initialize the seen vector\n seen = vector<bool>(n, false);\n // Make restricted nodes true\n for(int node : restricted)\n {\n seen[node] = true;\n }\n\n // Do DFS from node 0 to count the number of nodes reachable from 0\n seen[0] = true;\n return dfs(0);\n }\n\nprivate:\n // Helper method for DFS\n int dfs(int node)\n {\n int count = 1;\n for(int neighbor : graph[node])\n {\n if(!seen[neighbor])\n {\n seen[neighbor] = true;\n count += dfs(neighbor);\n }\n }\n return count;\n }\n};",
"memory": "219300"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 3 | {
"code": "class Solution {\n /*\n Do a simple bfs or dfs\n Mark the restricted nodes as visited instead of using extra memory\n */\n int count=0;\nprivate:\n void dfs(unordered_map<int,vector<int>>& ump,vector<bool>& vis,int node){\n vis[node] = true;\n count++;\n for(auto e: ump[node]){ \n if(!vis[e]) \n dfs(ump,vis,e);\n }\n return;\n }\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n unordered_map<int,vector<int>> ump;\n vector<bool> vis(n,false);\n for(auto edge: edges){\n ump[edge[0]].push_back(edge[1]);\n ump[edge[1]].push_back(edge[0]);\n }\n\n for(int node: restricted){\n vis[node]=true;\n }\n\n dfs(ump,vis,0);\n return count;\n }\n};",
"memory": "219300"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 3 | {
"code": "class Solution {\npublic:\n void dfs(int v, unordered_map<int, vector<int>>&adj, unordered_set<int>&vis, int &count){\n vis.insert(v);\n count++;\n for(auto &child : adj[v]){\n if(vis.count(child)){\n continue;\n }\n dfs(child, adj, vis, count);\n }\n }\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n \n unordered_map<int, vector<int>>adj;\n\n for(auto &edge : edges){\n int u = edge[0];\n int v = edge[1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n \n \n unordered_set<int>vis(begin(restricted), end(restricted));\n if(vis.count(0)){\n return 1;\n }\n int count = 0;\n dfs(0, adj, vis, count);\n return count;\n }\n};",
"memory": "220620"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 3 | {
"code": "class Solution {\npublic:\n void dfs(int node,map<int,vector<int>>&mp,vector<bool>&vis){\n vis[node]=1;\n for(auto i:mp[node]){\n if(!vis[i]){\n dfs(i,mp,vis);\n }\n }\n }\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<bool>vis(n,0);\n map<int,vector<int>>mp;\n for(auto i:edges){\n mp[i[0]].push_back(i[1]);\n mp[i[1]].push_back(i[0]);\n }\n for(auto i:restricted){\n vis[i]=1;\n }\n dfs(0,mp,vis);\n int ans=0;\n for(auto i:vis){\n if(i==1)ans++;\n }\n return ans-restricted.size();\n\n }\n};",
"memory": "220620"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 3 | {
"code": "class Solution {\npublic:\n\n unordered_set<int> blocked;\n unordered_map<int, vector<int>> adj;\n\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n for (int i : restricted) blocked.insert(i);\n for (auto nodePair : edges) {\n adj[nodePair[0]].push_back(nodePair[1]);\n adj[nodePair[1]].push_back(nodePair[0]);\n }\n return dfs(0, -1);\n }\n\n int dfs(int node, int parentNode) {\n if (blocked.find(node) != blocked.end()) return 0;\n \n int canVisit = 1; // 1 for the current node\n for (int neighbor : adj[node]) {\n if (neighbor == parentNode) continue;\n canVisit += dfs(neighbor, node);\n }\n return canVisit;\n }\n};",
"memory": "221940"
} |
2,445 | <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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<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.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<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.
</pre>
<p> </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>
| 3 | {
"code": "class Solution {\npublic:\nvoid helper(int currentNode, vector<vector<int>>& edges, unordered_set<int>& restricted, int& reachableNodes) {\n\trestricted.insert(currentNode);\n\treachableNodes++;\n\n\tfor (int value : edges[currentNode]) {\n\t\tif (restricted.find(value) == restricted.end()) {\n\t\t\thelper(value, edges, restricted, reachableNodes);\n\t\t}\n\t}\n}\n\nint reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n\tunordered_set<int> restrictedSet;\n\n\tfor (int value : restricted) {\n\t\trestrictedSet.insert(value);\n\t}\n\n\tvector<vector<int>> edgesVec;\n\n\tfor (int i = 0; i < n; i++) {\n\t\tedgesVec.push_back(vector<int> {});\n\t}\n\n\tfor (vector<int> vec : edges) {\n\t\tedgesVec[vec[0]].push_back(vec[1]);\n\t\tedgesVec[vec[1]].push_back(vec[0]);\n\t}\n\n\tint reachableNodes = 0;\n\n\thelper(0, edgesVec, restrictedSet, reachableNodes);\n\n\treturn reachableNodes;\n}\n};",
"memory": "221940"
} |
2,443 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p>
<p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p>
<ol>
<li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li>
<li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li>
<li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li>
</ol>
<p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,4,5,6]
<strong>Output:</strong> true
<strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6].
This partition is valid, so we return true.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no valid partition for this array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
</ul>
| 0 | {
"code": "auto init = []()\n{ \n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 0;\n}();\nclass Solution {\npublic:\n bool validPartition(vector<int>& nums) {\n if(nums.size() == 2) return nums[0] == nums[1];\n \n bool dp_i_3 = true;\n bool dp_i_2 = false;\n bool dp_i_1 = nums[0] == nums[1];\n for(int i = 3 ; i <= nums.size() ; i++)\n {\n // case 1 : if the last 2 elements are a good subarray and previous i - 2 are valid\n bool dp_i = (nums[i-1] == nums[i-2]) && dp_i_2;\n\n // case 2 : last 3 are equal\n if(!dp_i) dp_i = i > 2 && (nums[i-1] == nums[i-2]) && nums[i-2] == nums[i-3] && dp_i_3;\n\n // case 3 : last 3 are consecutive\n if(!dp_i) dp_i = i > 2 && (nums[i-1] == nums[i-2] + 1) && (nums[i-2] == nums[i-3] + 1) && dp_i_3;\n\n dp_i_3 = dp_i_2;\n dp_i_2 = dp_i_1;\n dp_i_1 = dp_i;\n if(!(dp_i_3 || dp_i_2 || dp_i_1)) return false;\n }\n return dp_i_1;\n }\n};",
"memory": "86200"
} |
2,443 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p>
<p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p>
<ol>
<li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li>
<li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li>
<li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li>
</ol>
<p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,4,5,6]
<strong>Output:</strong> true
<strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6].
This partition is valid, so we return true.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no valid partition for this array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
</ul>
| 0 | {
"code": "auto init = []()\n{ \n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 0;\n}();\nclass Solution {\npublic:\n bool validPartition(vector<int>& nums) {\n if(nums.size() == 2) return nums[0] == nums[1];\n \n bool dp_i_3 = true;\n bool dp_i_2 = false;\n bool dp_i_1 = nums[0] == nums[1];\n for(int i = 3 ; i <= nums.size() ; i++)\n {\n // case 1 : if the last 2 elements are a good subarray and previous i - 2 are valid\n bool dp_i = (nums[i-1] == nums[i-2]) && dp_i_2;\n\n // case 2 : last 3 are equal\n if(!dp_i) dp_i = i > 2 && (nums[i-1] == nums[i-2]) && nums[i-2] == nums[i-3] && dp_i_3;\n\n // case 3 : last 3 are consecutive\n if(!dp_i) dp_i = i > 2 && (nums[i-1] == nums[i-2] + 1) && (nums[i-2] == nums[i-3] + 1) && dp_i_3;\n\n dp_i_3 = dp_i_2;\n dp_i_2 = dp_i_1;\n dp_i_1 = dp_i;\n if(!(dp_i_3 || dp_i_2 || dp_i_1)) return false;\n }\n return dp_i_1;\n }\n};",
"memory": "86300"
} |
2,443 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p>
<p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p>
<ol>
<li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li>
<li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li>
<li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li>
</ol>
<p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,4,5,6]
<strong>Output:</strong> true
<strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6].
This partition is valid, so we return true.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no valid partition for this array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool validPartition(vector<int>& nums) {\n int n = nums.size();\n if (n == 1) return false;\n\n bool prev3 = true; // corresponds to dp[i-3]\n bool prev2 = false; // corresponds to dp[i-2]\n bool curr = n > 1 && nums[0] == nums[1]; // corresponds to dp[i-1]\n\n for (int i = 2; i < n; i++) {\n bool next = false;\n\n if (nums[i] == nums[i - 1] && prev2) {\n next = true;\n } else if (nums[i] == nums[i - 1] && nums[i] == nums[i - 2] && prev3) {\n next = true;\n } else if (nums[i] - nums[i - 1] == 1 && nums[i - 1] - nums[i - 2] == 1 && prev3) {\n next = true;\n }\n\n // Update states\n prev3 = prev2;\n prev2 = curr;\n curr = next;\n }\n\n return curr;\n }\n};\n",
"memory": "86400"
} |
2,443 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p>
<p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p>
<ol>
<li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li>
<li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li>
<li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li>
</ol>
<p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,4,5,6]
<strong>Output:</strong> true
<strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6].
This partition is valid, so we return true.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no valid partition for this array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool validPartition(vector<int>& nums) {\n int const n = nums.size();\n bool p[n+1];\n\n fill(p,p+n+1,0);\n p[0] = 1;\n\n\n for (int i = 1; i<n; i++){\n \n\n if (nums[i] == nums[i-1]){\n if (p[i-1]){\n p[i+1] = 1;\n }\n if (i>1 && nums[i-2] == nums[i] && (i == 2 || p[i-2])){\n p[i+1] = 1;\n }\n } else if (nums[i] == nums[i-1]+1){\n if (i<n-1 && nums[i+1] == nums[i]+1 && p[i-1]){\n p[i+2] = 1;\n }\n\n }\n // cout<<p[i]<<endl;\n }\n\n \n\n return p[n];\n }\n};",
"memory": "86500"
} |
2,443 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p>
<p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p>
<ol>
<li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li>
<li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li>
<li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li>
</ol>
<p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,4,5,6]
<strong>Output:</strong> true
<strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6].
This partition is valid, so we return true.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no valid partition for this array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool validPartition(vector<int>& nums) {\n\n int n = nums.size();\n vector<bool> dp(n, false); // valid arr: is nums arr valid from start to curr i\n\n dp[0]=false; // single num must be invalid\n\n for(int i=1; i<n; i++){\n\n //case1: \n if(nums[i]==nums[i-1]){\n if(i-2<0){\n dp[i]=true;\n }\n else{\n if(dp[i-2]){\n dp[i]=true;\n }\n }\n }\n\n //case2:\n if(!dp[i] && i-2>=0 && nums[i]==nums[i-1] && nums[i-1]==nums[i-2]){\n if(i-3<0){\n dp[i]=true;\n }\n else{\n if(dp[i-3]){\n dp[i]=true;\n }\n }\n }\n\n //case3:\n if(!dp[i] && i-2>=0 && nums[i]==nums[i-1]+1 && nums[i-1]==nums[i-2]+1){\n if(i-3<0){\n dp[i]=true;\n }\n else{\n if(dp[i-3]){\n dp[i]=true;\n }\n }\n }\n\n }\n\n return dp.back();\n\n }\n};",
"memory": "86600"
} |
2,443 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p>
<p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p>
<ol>
<li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li>
<li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li>
<li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li>
</ol>
<p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,4,5,6]
<strong>Output:</strong> true
<strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6].
This partition is valid, so we return true.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no valid partition for this array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool validPartition(vector<int>& nums) {\n int n = nums.size();\n vector<bool>t(n+1 , false);\n t[n] = true;\n t[n-1] = false;\n for(int i=n-2 ; i>=0 ; --i){\n t[i] = (nums[i] == nums[i+1]) && t[i+2];\n if(i <= n-3){\n t[i] = t[i] || ((nums[i] == nums[i+1] && nums[i] == nums[i+2]) && t[i+3]);\n t[i] = t[i] || ((nums[i]+1 == nums[i+1] && nums[i+1]+1 == nums[i+2]) && t[i+3]);\n }\n }\n return t[0];\n }\n};",
"memory": "86700"
} |
2,443 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p>
<p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p>
<ol>
<li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li>
<li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li>
<li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li>
</ol>
<p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,4,5,6]
<strong>Output:</strong> true
<strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6].
This partition is valid, so we return true.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no valid partition for this array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool validPartition(vector<int>& nums) {\n int n = nums.size();\n vector<bool>t(n+1 , false);\n t[n] = true;\n t[n-1] = false;\n for(int i=n-2 ; i>=0 ; --i){\n t[i] = (nums[i] == nums[i+1]) && t[i+2];\n if(t[i]) continue;\n if(i <= n-3){\n t[i] = t[i] || ((nums[i] == nums[i+1] && nums[i] == nums[i+2]) && t[i+3]);\n if(t[i]) continue;\n t[i] = t[i] || ((nums[i]+1 == nums[i+1] && nums[i+1]+1 == nums[i+2]) && t[i+3]);\n }\n }\n return t[0];\n }\n};",
"memory": "86700"
} |
2,443 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p>
<p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p>
<ol>
<li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li>
<li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li>
<li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li>
</ol>
<p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,4,5,6]
<strong>Output:</strong> true
<strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6].
This partition is valid, so we return true.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no valid partition for this array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool validPartition(vector<int>& nums) {\n\n int n = nums.size();\n vector<bool> dp(n, false); // valid arr: is nums arr valid from start to curr i\n\n dp[0]=false; // single num must be invalid\n\n for(int i=1; i<n; i++){\n\n //case1: \n if(nums[i]==nums[i-1]){\n if(i-2<0){\n dp[i]=true;\n }\n else if(dp[i-2]){\n dp[i]=true;\n }\n }\n\n //case2:\n if(i-2>=0 && nums[i]==nums[i-1] && nums[i-1]==nums[i-2]){\n if(i-3<0){\n dp[i]=true;\n }\n else if(dp[i-3]){\n dp[i]=true;\n }\n }\n\n //case3:\n if(i-2>=0 && nums[i]==nums[i-1]+1 && nums[i-1]==nums[i-2]+1){\n if(i-3<0){\n dp[i]=true;\n }\n else if(dp[i-3]){\n dp[i]=true;\n }\n }\n\n }\n\n return dp.back();\n\n }\n};",
"memory": "86800"
} |
2,443 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p>
<p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p>
<ol>
<li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li>
<li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li>
<li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li>
</ol>
<p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,4,5,6]
<strong>Output:</strong> true
<strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6].
This partition is valid, so we return true.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no valid partition for this array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool validPartition(vector<int>& nums) {\n\n int n = nums.size();\n vector<bool> dp(n, false); // valid arr: is nums arr valid from start to curr i\n\n dp[0]=false; // single num must be invalid\n\n for(int i=1; i<n; i++){\n\n //case1: \n if(nums[i]==nums[i-1]){\n if(i-2<0){\n dp[i]=true;\n }\n else if(dp[i-2]){\n dp[i]=true;\n }\n }\n\n //case2:\n if(i-2>=0 && nums[i]==nums[i-1] && nums[i-1]==nums[i-2]){\n if(i-3<0){\n dp[i]=true;\n }\n else if(dp[i-3]){\n dp[i]=true;\n }\n }\n\n //case3:\n if(i-2>=0 && nums[i]==nums[i-1]+1 && nums[i-1]==nums[i-2]+1){\n if(i-3<0){\n dp[i]=true;\n }\n else if(dp[i-3]){\n dp[i]=true;\n }\n }\n\n }\n\n return dp.back();\n\n }\n};",
"memory": "86800"
} |
2,443 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p>
<p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p>
<ol>
<li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li>
<li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li>
<li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li>
</ol>
<p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,4,4,5,6]
<strong>Output:</strong> true
<strong>Explanation:</strong> The array can be partitioned into the subarrays [4,4] and [4,5,6].
This partition is valid, so we return true.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no valid partition for this array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool validPartition(vector<int>& nums) {\n\n int n = nums.size();\n vector<bool> dp(n, false); // valid arr: is nums arr valid from start to curr i\n\n dp[0]=false; // single num must be invalid\n\n for(int i=1; i<n; i++){\n\n //case1: \n if(nums[i]==nums[i-1]){\n if(i-2<0){\n dp[i]=true;\n }\n else{\n if(dp[i-2]){\n dp[i]=true;\n }\n }\n }\n\n //case2:\n if(i-2>=0 && nums[i]==nums[i-1] && nums[i-1]==nums[i-2]){\n if(i-3<0){\n dp[i]=true;\n }\n else{\n if(dp[i-3]){\n dp[i]=true;\n }\n }\n }\n\n //case3:\n if(i-2>=0 && nums[i]==nums[i-1]+1 && nums[i-1]==nums[i-2]+1){\n if(i-3<0){\n dp[i]=true;\n }\n else{\n if(dp[i-3]){\n dp[i]=true;\n }\n }\n }\n\n }\n\n return dp.back();\n\n }\n};",
"memory": "86900"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.