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 b... | 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 stoppin... |
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 b... | 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... |
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 b... | 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 ... |
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 b... | 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 ... |
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 b... | 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... |
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 b... | 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.siz... |
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 b... | 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 reach... |
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 b... | 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 re... |
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 b... | 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... |
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 b... | 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 ... |
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 b... | 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]... |
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 b... | 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... |
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 b... | 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 ... |
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 b... | 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 ... |
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 b... | 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<v... |
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 b... | 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 vect... |
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 b... | 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 ... |
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 b... | 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, vec... |
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 b... | 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, notA... |
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 b... | 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(i... |
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 b... | 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<vec... |
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 b... | 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 ... |
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 b... | 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 rea... |
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 b... | 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 ... |
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 b... | 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 ... |
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 b... | 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 ... |
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 b... | 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... |
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 b... | 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 ... |
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 b... | 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 ... |
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 b... | 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 ... |
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 b... | 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, ... |
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 b... | 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>> gr... |
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 b... | 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 ... |
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 b... | 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 ... |
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 b... | 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 reachableNo... |
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 b... | 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 ... |
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 b... | 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 reachable... |
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 b... | 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> visi... |
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 b... | 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.... |
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 b... | 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;\... |
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 b... | 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... |
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 b... | 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 ... |
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 b... | 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 i... |
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 b... | 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 i... |
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 b... | 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... |
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 b... | 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 reachabl... |
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 b... | 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 ... |
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 b... | 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)) {\... |
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 b... | 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 ... |
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 b... | 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(a... |
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 b... | 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 rea... |
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 b... | 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 ... |
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 b... | 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;\... |
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 b... | 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 ... |
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 b... | 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 ... |
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 b... | 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 ... |
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 b... | 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] = tr... |
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 b... | 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}... |
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 b... | 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... |
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 b... | 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 ... |
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 b... | 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 re... |
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 b... | 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... |
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 b... | 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 reac... |
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 b... | 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 connecti... |
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 b... | 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 ... |
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 b... | 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 ... |
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 b... | 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()) ... |
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 b... | 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 ... |
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 b... | 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 ... |
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 b... | 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 ... |
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 b... | 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 ... |
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 b... | 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(... |
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 b... | 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 i... |
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 b... | 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_se... |
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 b... | 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(in... |
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 b... | 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,coun... |
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 b... | 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 ... |
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 b... | 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(i... |
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 b... | 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... |
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 b... | 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<edge... |
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 b... | 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)==s... |
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 b... | 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 ... |
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 b... | 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 ... |
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 b... | 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... |
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 b... | 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 {... |
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 b... | 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 ... |
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 b... | 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, vi... |
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 b... | 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>&... |
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 b... | 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[nodePai... |
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 b... | 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... |
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 conditi... | 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... |
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 conditi... | 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... |
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 conditi... | 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]; // corres... |
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 conditi... | 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 ... |
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 conditi... | 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: \... |
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 conditi... | 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 ... |
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 conditi... | 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]) continu... |
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 conditi... | 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: \... |
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 conditi... | 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: \... |
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 conditi... | 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: \... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.