id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 0 | {
"code": "class Solution {\nprivate:\n// int cnt(int n, vector<vector<int>> adj){\n// queue<int> q;\n// q.push(n);\n// int c=0;\n// while(!q.empty()){\n// int node=q.front();\n// q.pop();\n// c++;\n// for(auto it : adj[node]){\n// q.push(it);\n// }\... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 0 | {
"code": "vector<int>dp;\n\nint f(int i,int p,vector<int>adj[])\n{\n int cnt=1;\n for(auto x:adj[i])\n {\n if(x!=p)\n {\n cnt+=f(x,i,adj);\n }\n }\n \n return dp[i]=cnt;\n\n}\n\nclass Solution {\npublic:\n int countGoodNodes(vector<vector<int>>& edges) {\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 0 | {
"code": "vector<int>dp;\n\nint f(int i,int p,vector<int>adj[])\n{\n int cnt=1;\n for(auto x:adj[i])\n {\n if(x!=p)\n {\n cnt+=f(x,i,adj);\n }\n }\n \n return dp[i]=cnt;\n\n}\n\nclass Solution {\npublic:\n int countGoodNodes(vector<vector<int>>& edges) {\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 0 | {
"code": "class Solution {\npublic:\n\n vector<int> child[100005];\n int count[100005];\n int ans;\n int check[100005];\n\n void dfs(int parent) {\n\n if(check[parent]) return;\n check[parent] = 1;\n int tmp = -1;\n bool checkGood = true;\n count[parent] = 0;\n\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 0 | {
"code": "class Solution {\npublic:\n // a node is good iff all subtrees rooted at each child have same size\n vector<int> adj[100000];\n // subtree_size[i] size of subtree rooted at i\n int subtree_size[100000];\n int good_nodes = 0;\n int parent[100000];\n\n // node is good iff for all c in ch... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 0 | {
"code": "vector<int> adj[100100];\nint sb[100100];\nbool leaf[100100];\nint ans = 0;\nvoid dfs(int node,int pp){\n sb[node] = 1;\n bool l = true;\n for(auto v : adj[node]){\n if(v!=pp) {\n l = false;\n dfs(v,node);\n sb[node] += sb[v]; \n }\n }\n if(l)... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 0 | {
"code": "vector<int> adj[100100];\nint sb[100100];\nbool leaf[100100];\nint ans = 0;\nvoid dfs(int node,int pp){\n sb[node] = 1;\n bool l = true;\n for(auto v : adj[node]){\n if(v!=pp) {\n l = false;\n dfs(v,node);\n sb[node] += sb[v]; \n }\n }\n if(l)... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 0 | {
"code": "class Solution {\npublic:\n\n int solve(vector<int> nodes[],int node,int par,vector<int>& vis,vector<int>& count){\n int cnt = 1;\n vis[node]=1;\n for(auto a:nodes[node]){\n if(vis[a]==-1 && par!=a ){\n cnt+=solve(nodes,a,node,vis,count)... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 0 | {
"code": "class Solution {\n int ans=0;\npublic:\n int f(int node,vector<int> adj[],int n,vector<int> &vis,vector<int> &cnt)\n {\n vis[node]=1;\n int flag=0;\n int c=0;\n int prev=-1;\n for(auto it:adj[node])\n {\n if(!vis[it])\n {\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 1 | {
"code": "class Solution {\npublic:\n int dfs(vector<int> adj[],int node,int parent,vector<int>& vis,\n vector<int>& desc,int& cnt)\n {\n vis[node]=1;\n int child=0;\n int prev=-1;\n bool flag=false;\n for(auto it: adj[node])\n {\n if(!vis[it])\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 1 | {
"code": "class Solution {\npublic:\n int dfs(vector<int> adj[],int node,int parent,vector<int>& vis,\n vector<int>& desc,int& cnt)\n {\n vis[node]=1;\n int child=0;\n int prev=-1;\n bool flag=false;\n for(auto it: adj[node])\n {\n if(!vis[it] && ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 1 | {
"code": "class Solution {\npublic:\n int dfs(vector<int> adj[], vector<int> & vis, vector<int> &sum, int node, int &cnt, int parent){\n vis[node] = 1;\n if(adj[node].size() == 1 && adj[node][0] == parent){\n sum[node] = 1;\n cnt += 1;\n return 0;\n }\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 1 | {
"code": "class kundli{\n public:\n\n int size;\n\n kundli(int size){\n this->size=size;\n }\n};\n\nclass Solution {\npublic:\n int ans=0;\n\n kundli* dfs(int node,vector<vector<int>>&adj,vector<int>&visited){\n visited[node]=1;\n bool isleaf=true;\n for(auto it:adj[node... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 1 | {
"code": "class Solution {\n int countNodes(int node,vector<int>adj[],int &ans,vector<int>&vis){\n vis[node]=1;\n vector<int>child;\n for(auto it:adj[node]){\n if(!vis[it])\n child.push_back(countNodes(it,adj,ans,vis));\n }\n if(child.size()==0)\n {\... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 1 | {
"code": "class Solution {\npublic:\n vector<int> adj[100005];\n int sz[100005];\n int ans = 0;\n int dfs(int u, int p) {\n sz[u] = 1;\n vector<int> child;\n for (int x : adj[u]) {\n if (x != p) {\n child.push_back(x);\n sz[u] += dfs(x, u);\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 1 | {
"code": "class Solution {\npublic:\n int solve(vector<vector<int>>& adj , int & count , vector<int> & visited , int node){\n visited[node] = 1;\n int sum = 0;\n int num = -1;\n bool b = true;\n for(int i : adj[node]){\n if(!visited[i]){\n int n = solve... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 1 | {
"code": "class Solution {\npublic:\n\n int solve(vector<vector<int>>&tree , int i , int &ans, vector<int>&vis) {\n if(tree[i].size() == 0) {\n return 0;\n }\n int count = 0; \n int notfound = 0;\n int tot = 0;\n vis[i] = 1;\n for(int j = 0 ; j < tree[i]... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 1 | {
"code": "class Solution {\nprivate: \n int dfs(int node, int parent, vector<vector<int>>& edges, vector<int>& subtreeSize, int& goodNodes) {\n if(subtreeSize[node] != -1) return subtreeSize[node];\n int &ans = subtreeSize[node];\n ans = 1;\n int sub = -1;\n bool good = true;\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 1 | {
"code": "class Solution {\npublic:\n int solve(vector<vector<int>>& tree, vector<int>& size, int root, int parent)\n {\n if(parent != -1 &&tree[root].size() == 1)\n {\n size[root] = 1;\n return 1;\n }\n int ans = 0;\n for(auto i:tree[root])\n {\n... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 1 | {
"code": "\nclass Solution {\nprivate:\n int dfs(int node, vector<int> adj[], vector<bool>& visited, vector<int>& subtree_size) {\n visited[node] = true;\n int size = 1; // Include the node itself\n\n for (int neighbor : adj[node]) {\n if (!visited[neighbor]) {\n si... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 1 | {
"code": "class Solution {\npublic:\n vector<int>depth;\n int ans=0;\n // unordered_map<int,vector<int>>adj;\n vector<vector<int>>adj;\n void solve(int node,int par){\n for(auto child:adj[node]){\n if(child==par)continue;\n solve(child,node);\n depth[node]+=dept... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 1 | {
"code": "class Solution {\npublic:\n int ans;\n vector<vector<int>> adj;\n int getSizeOfSubTree(vector<bool> &visit, int cur) {\n visit[cur] = true;\n int sum=0, pre = -1, now, flag = 0;\n for(int pos : adj[cur]) {\n if(!visit[pos]) {\n now = getSizeOfSubTree(... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 1 | {
"code": "class Solution {\nprivate:\n int countHelper(\n vector<vector<int>>& graph,\n int node,\n vector<bool> &visited,\n int &numGoodNodes\n ) {\n int totalNumChildNodes = 0;\n int currentNumChildNodes = -1;\n bool isGoodNode = true;\n for (int neighb... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 1 | {
"code": "class Solution {\npublic:\n void dfs(int i,int p,vector<int>adj[],vector<int>&sub,vector<int> &par){\n for(auto j:adj[i]){\n if(j!=p){\n par[j]=i;\n dfs(j,i,adj,sub,par);\n sub[i]+=sub[j];\n }\n }\n sub[i]+=1;\n }... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 1 | {
"code": "class Solution {\npublic:\n int countGoodNodes(vector<vector<int>>& edges) {\n int n = edges.size() + 1;\n vector<vector<int>> adj(n);\n for (auto e : edges) {\n int u = e[0]; int v = e[1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n\n... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 1 | {
"code": "#include <bits/stdc++.h>\nusing namespace std;\n#define dbg(...) __f(#__VA_ARGS__,__VA_ARGS__)\n// trace std::pair\ntemplate<class L,class R> std::ostream& operator<<(std::ostream& os,std::pair<L,R>& P){\n return os<<\"{\"<<P.first<<\":\"<<P.second<<\"}\";\n}\n// trace std::vector\ntemplate<class T> std... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 1 | {
"code": "#include <iostream>\n#include <vector>\n#include <string>\nusing namespace std;\n\n#if __linux__\n#else\n#ifndef _LOCAL\nstruct TreeNode{\n\tint val;\n};\nstruct ListNode{\n\tint val;\n\tListNode* next;\n\tListNode(){}\n\tListNode(int _val){val = _val;}\n\tListNode(int _val, ListNode* _next){val = _val; ne... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 1 | {
"code": "class Solution {\npublic:\n int dfs(int u, int par, vector<int>g[], vector<int>tot[]){\n int sz = 0;\n for(int v: g[u]){\n if(v==par) continue;\n int curr = 1 + dfs(v, u, g, tot);\n sz+=curr;\n tot[u].push_back(curr);\n }\n return s... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 1 | {
"code": "class Solution {\npublic:\n\n int solve(int v, int p, vector<vector<int>>&adj, int &good_nodes){\n \n set<int> s;\n int total_sz=1;\n for(auto u:adj[v]){\n if(u==p)continue;\n int sz = solve(u,v,adj,good_nodes);\n s.insert(sz);\n to... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 1 | {
"code": "class Solution {\npublic:\n int fa=0;\n void dfs(int i,vector<int> adj[],int n,vector<int>&dp,int root,vector<bool>&vis){\n vis[i]=true;\n if(adj[i].size()==1 and i!=root){\n fa++;\n return;\n }\n set<int> st;\n for(auto it=adj[i].begin();it!=a... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 1 | {
"code": "class Solution {\npublic:\n int n;\n vector<int>k;\n int calsize(int i,int par,vector<vector<int>>&v){\n if(k[i]!=0)return k[i];\n int sum2=0;\n for(auto j:v[i]){\n if(j==par)continue;\n sum2+=calsize(j,i,v);\n }\n return k[i]=sum2+1;\n }\n int countGoo... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 1 | {
"code": "class Solution {\npublic:\n int dfs(int s, int par, int &res, vector<vector<int>> &adj, vector<int> &sub)\n {\n int subsz = 1;\n int chlsz = -1;\n bool chk = true;\n\n for(int i:adj[s])\n {\n if(i == par)\n continue;\n\n int chls... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 1 | {
"code": "int count(vector<vector<int>>& adjlist, int node, vector<vector<int>>& desc, vector<bool>& visited) {\n visited[node] = true;\n int total_desc = 0;\n \n for (int i = 0; i < adjlist[node].size(); i++) {\n int child = adjlist[node][i];\n if (!visited[child]) {\n int cnt =... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 2 | {
"code": "class Solution {\npublic:\n vector<vector<int>> adj;\n int dfs(int cur, int parent, vector<int>& allsize){\n int size = 1;\n for(int ngb: adj[cur]){\n if(ngb != parent){\n size+=dfs(ngb, cur, allsize);\n }\n }\n allsize[cur] = size;\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 2 | {
"code": "class Solution {\n int f(int node, vector<int> adj[], vector<int>& vis, int& ans) {\n vis[node] = 1;\n int val = -1, res = 0;\n bool p = true;\n\n for(auto adjNode : adj[node]) {\n if(!vis[adjNode]) {\n int tmp = f(adjNode, adj, vis, ans);\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 2 | {
"code": "class Solution {\n int ans = 0;\n int solve(int node ,vector<bool> &visited, vector<int> graph[])\n {\n visited[node] = true;\n vector<int> cnt;\n int sum = 0;\n for(auto x : graph[node])\n {\n if(!visited[x]){\n int res = solve(x,visite... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 2 | {
"code": "class Solution {\npublic:\n int countGoodNodes(vector<vector<int>>& edges) {\n int n = edges.size() + 1;\n\n vector<vector<int>> adjacent(n);\n for (auto &e : edges) {\n adjacent[e[0]].push_back(e[1]);\n adjacent[e[1]].push_back(e[0]);\n }\n\n int... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 2 | {
"code": "class Solution {\npublic:\n int final = 0;\n void dfs(int node, int parent , vector<int>adj[] , vector<int>&chh){\n \n for(int child : adj[node]){\n if(child!=parent){\n dfs(child,node,adj,chh);\n chh[node]+=chh[child];\n }\n }\... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 2 | {
"code": "class Solution {\npublic:\nint ans=0;\nint f(int node,int parent,map<int,vector<int>>&adj){\n int sum=0;\n bool isgood=true;\n int size=-1;\nfor(auto x: adj[node]){\n if(x==parent) continue;\n int currsize=f(x,node,adj);\n if(size==-1){\n size=currsize;\n }else if(currsize!=size... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 2 | {
"code": "class Solution {\npublic:\n int getGood(int src, vector<vector<int>>& graph, int pnode, int &good)\n {\n int n = graph[src].size();\n if(n == 1 && src != 0)\n {\n good++;\n return 1;\n }\n\n vector<int> sizes;\n for(int i=0;i<n;i++)\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 2 | {
"code": "class Solution {\npublic:\n int total = 0;\n void adj(vector<vector<int>> &edges, map<int, vector<int>> &mp){\n for(int i = 0; i < edges.size(); i++){\n int u = edges[i][0];\n int v = edges[i][1];\n mp[u].push_back(v);\n mp[v].push_back(u);\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 2 | {
"code": "class Solution {\npublic:\n int cnt = 0;\n int rec(int node, vector<vector<int>>& adj, vector<bool>& vis){\n vis[node] = 1;\n int totalSize = 0;\n int subTreeSize = 0;\n bool flag = true;\n // cout << \"NODE IS \" << node << endl;\n vector<int> res; \n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 2 | {
"code": "class Solution {\npublic:\n int solve(vector<int> adj[],int v,vector<bool> &vis,int &ans){\n set<int> s;\n vis[v]=1;\n int sz = 1;\n for(auto u:adj[v]){\n if(vis[u]) continue;\n int x = solve(adj,u,vis,ans);\n sz += x;\n s.insert(x)... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 2 | {
"code": "class Solution {\nprivate:\n int dfs(unordered_map<int, vector<int>>& adj, vector<int>& visited, vector<int>& arr, int node){\n visited[node] = 1;\n \n int ans = 0;\n for(auto neighbor : adj[node]){\n if(!visited[neighbor]){\n ans += 1 + dfs(adj, vis... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 2 | {
"code": "class Solution {\npublic:\n int countGoodNodes(vector<vector<int>>& edges) {\n int size = edges.size() + 1; \n vector<int> dp(size + 1, -1); \n unordered_map<int, vector<int>> edge;\n vector<int> parent(size + 1, -1); \n\n for (auto& arr : edges) {\n int fi... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 2 | {
"code": "class Solution {\n bitset<100001> visited;\n int ret;\npublic:\n int visit(const vector<vector<int>>& nodes, int root) {\n vector<int> cnts;\n visited[root] = true;\n for(const auto& c : nodes[root]) {\n if (!visited[c]) {\n cnts.push_back(visit(nodes, c));\n }\n... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 2 | {
"code": "#include <vector>\n#include <map>\n#include <algorithm>\n#include <numeric> // For std::accumulate\n\nusing namespace std;\n\nclass Solution {\npublic:\n int good = 0;\n vector<vector<int>>mp;\n \n int helper(int cur, int par) {\n // Base case: If the node is a leaf node\n if (cur... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 2 | {
"code": "class Solution {\npublic:\n int dfs(int idx, vector<vector<int>>& v, int& ans, unordered_set<int>& s) {\n if (s.count(idx))\n return 0;\n\n s.insert(idx);\n vector<int> sum;\n for(int next: v[idx]) {\n if (!s.count(next)) {\n int ret = dfs... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 2 | {
"code": "class Solution {\npublic:\n unordered_map<int,set<int>> mp;\n vector<vector<int>> g;\n int ans;\n int dfs(int node,int par){\n int size=0;\n for(auto &v:g[node]){\n if(v!=par){\n int temp=dfs(v,node);\n mp[node].insert(temp);\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 2 | {
"code": "class Solution {\npublic:\n int dfs(vector<vector<int>>& pare,vector<int>& child, vector<vector<int>> &adj, int node, int par) {\n \n int count = 0;\n for(auto nbh: adj[node]) {\n if(nbh != par) {\n count += 1+dfs(pare, child, adj, nbh, node);\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 2 | {
"code": "class Solution {\npublic:\n int countNodes(int node, int parent, vector<vector<int>>& adj, vector<int>& nodeCt, vector<vector<int>>& children){\n int ct = 1;\n for(int i = 0; i < adj[node].size(); i++){\n int next = adj[node][i];\n if(next != parent){\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 2 | {
"code": "class Solution {\npublic:\n bool checkDuplicate(vector<int> v) {\n if (v.size() == 0) {\n return true;\n }\n set<int> st;\n for (auto e : v) {\n st.insert(e);\n }\n if (st.size() == 1) {\n return true;\n } else {\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 2 | {
"code": "class Solution {\nprivate:\n int dfs(vector<int> adj[], vector<int>& vis, int& cnt, int node) {\n vis[node] = 1;\n vector<int> subtree;\n int overall=0;\n\n for (int i : adj[node]) {\n if (!vis[i]) {\n int val = dfs(adj, vis, cnt, i);\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 2 | {
"code": "class Solution {\npublic:\n int count = 0;\n unordered_set<int>vis;\n unordered_map<int,vector<int>>adj;\n int countGoodNodes(vector<vector<int>>& arr) {\n //undirected tree --> cycle will be not present\n //rooted at node 0\n \n //subtrees rooted at its children have... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 2 | {
"code": "class Solution {\npublic:\n int dfs(int node,vector<int>&vis,vector<int>&v,vector<int>adj[])\n {\n int s=1;\n vis[node]=1;\n for(auto it:adj[node])\n {\n if(!vis[it])\n s+=dfs(it,vis,v,adj);\n }\n v[node]=s;\n return s;\n }\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 2 | {
"code": "struct node {\n int _ind;\n vector<node*> children;\n node(int i) {\n _ind = i;\n }\n};\nclass Solution {\npublic:\n int countGoodNodes(vector<vector<int>>& edges) {\n map<int, node*> m;\n for (int i= 0; i < edges.size(); i++) {\n int p = edges[i][0];\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 2 | {
"code": "class Solution {\npublic:\n int cnt = 0;\n \n int dfs(int node,int par,vector<int> adj[],vector<int> &st,int &gn,vector<int> &vis){\n st[node] = 1;\n vis[node] = 1;\n unordered_set<int> child;\n \n for(int cd:adj[node]){\n if(cd == par)continue;\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 2 | {
"code": "class Solution {\npublic:\n int countGoodNodes(vector<vector<int>>& edges) {\n int n = edges.size() + 1; // Number of nodes\n vector<vector<int>> tree(n);\n \n // Build the tree as an adjacency list\n for (const auto& edge : edges) {\n int a = edge[0], b = e... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 2 | {
"code": "class Solution {\npublic:\n int calculateSubtreeSizes(int node, vector<int> adj[], vector<int>& subtreeSize, vector<bool>& visited) {\n visited[node] = true;\n int size = 1;\n \n for (int neighbor : adj[node]) {\n if (!visited[neighbor]) {\n size += ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 2 | {
"code": "class Solution {\npublic:\n int countGoodNodes (vector<vector<int>>& edges) {\n int n = edges.size() + 1;\n vector<vector<int>> adj(n);\n vector<int> subtree_size(n, 1);\n for (auto& edge : edges) {\n adj[edge[0]].push_back(edge[1]);\n adj[edge [1]].push... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 2 | {
"code": "class Solution {\nprivate:\n int dfs(int node, map<int, vector<int>> &adj, vector<int> &vis, int &ans){\n vis[node]=1;\n \n vector<int> totalSubtreeNode;\n for(int n: adj[node]){\n if(vis[n]==0)\n totalSubtreeNode.push_back(dfs(n, adj, vis, ans));\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 3 | {
"code": "class Solution {\npublic:\nint dfs(vector<vector<int>>&adj,int s,int par,vector<int>&sub_tree, int &count)\n{\n sub_tree[s]=1;\n unordered_set<int>freq;\n \n for (auto it:adj[s])\n {\n if (it==par) continue;\n \n int sizes=dfs(adj,it,s,sub_tree,count);\n sub_tree[... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 3 | {
"code": "class Solution {\npublic:\n int dfs(int node,vector<vector<int>>&adj,vector<int>&vis,int&ans){\n vis[node] = 1;\n unordered_set<int>sizes;\n int node_size = 1;\n for(auto&it:adj[node]){\n if(!vis[it]){\n int child_size = dfs(it,adj,vis,ans);\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 3 | {
"code": "class Solution {\nprivate:\n int n;\n vector<bool> visited;\n vector<vector<int>> tree;\n int goodNodesCount = 0;\npublic:\n int getGoodNodes(int node = 0) {\n int totalNodes = 1;\n unordered_set<int> childrenNodeCountSet;\n \n visited[node] = true;\n\n for... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 3 | {
"code": "class Solution\n{\n int dfs(int node, vector<vector<int>> &adj, vector<bool> &visited, int &cnt)\n {\n visited[node] = true;\n unordered_set<int> child_sizes;\n int own_size = 1;\n for(auto &child:adj[node])\n {\n if(visited[child]) continue;\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 3 | {
"code": "class Solution {\npublic:\n int dfs(int root,unordered_map<int,vector<int>> &mp,int &count,int par){\n \n if(mp.find(root)==mp.end()){\n count++;\n return 1;\n }else{\n vector<int> temp=mp[root];\n int size=0;\n bool good=true;\n... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 3 | {
"code": "class Solution {\nprivate:\n int getChildrenCount(int root, int& goodNodes, vector <bool>& vis, vector <vector <int>>& tree)\n {\n vis[root] = 1;\n\n unordered_map <int, int> freq;\n\n for (int& child : tree[root])\n {\n if (not vis[child])\n {\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 3 | {
"code": "class Solution {\n int solve(int node, vector<vector<int>>&adj, vector<bool>&visited, int&good_nodes) {\n visited[node] = true;\n vector<int>sizes;\n for (auto nbr: adj[node]) {\n if (!visited[nbr]) {\n sizes.push_back(solve(nbr, adj, visited, good_nodes));... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 3 | {
"code": "#include <vector>\n#include <queue>\n#include <unordered_map>\nusing namespace std;\n\nclass Solution {\npublic:\n int countGoodNodes(vector<vector<int>>& edges) {\n int n = edges.size() + 1;\n vector<vector<int>> adj(n);\n vector<int> indegree(n, 0);\n \n // Create ad... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 3 | {
"code": "class Solution {\npublic:\n int dfs(int root, vector<int> adj[],vector<int>& vis, int& ans) {\n vis[root]=1;\n int sz=1;\n set<int> us;\n for(int i=0;i<adj[root].size();i++) {\n if (vis[adj[root][i]]==0) {\n int subtree_sz = dfs(adj[root][i], adj, vi... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 3 | {
"code": "class Solution {\npublic:\n \n int solve(int node , vector<int>&vis , unordered_map<int , vector<int>>&mp , int &ans ){\n vis[node]=1;\n int val = 0;\n set<int>s;\n for(auto i: mp[node]){\n if(!vis[i]){\n int temp = solve(i ,vis , mp , ans);\n val += temp;\n s.inse... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 3 | {
"code": "class Solution {\npublic:\n \n int cnt=0;\n void solve(unordered_map<int,int> &mp){\n if(mp.size()<=1){\n cnt++;\n }\n }\n int fn(int node,vector<int>&vis,vector<int>&ans,vector<int>adj[]){\n vis[node]=1;\n int n=1;\n unordered_map<int,int>mp;\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 3 | {
"code": "class Solution {\npublic:\n \n unordered_map<int,int>mp;\n\n int dfs(int src, int prev,vector<vector<int>>& adj) {\n \n // cout<<src<<\" \";\n int sum = 1;\n\n for (auto it : adj[src]) {\n \n if(it!=prev)\n {\n sum+=dfs(it,src,a... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 3 | {
"code": "// class Solution {\n// public:\n// int f(int node,vector<int>adjls[],vector<int>&ans,int parent)\n// {\n// if(adjls[node].size()==0)\n// {\n// cout<<node<<\" \";\n// ans.push_back(node);\n// return 0;\n// }\n// unordered_set<int>st;\n// vector<int>temp;\n// ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 3 | {
"code": "class Solution {\npublic:\n int cnt=0;\n int fn(vector<int>adj[],int start,vector<int>&vis){\n vis[start]=1;\n unordered_set<int>st;\n int size=0;\n int sum=0;\n for(auto it:adj[start]){\n if(!vis[it]){\n size=1+fn(adj,it,vis);\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 3 | {
"code": "class Solution {\npublic:\n int dfs(int node, int parent, unordered_map<int, vector<int>>& tree, vector<int>& subtree_size) {\n int size = 1; // Include the current node\n for (int neighbor : tree[node]) {\n if (neighbor != parent) { // Avoid going back to the parent node\n size... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 3 | {
"code": "class Solution {\npublic:\n int countGoodNodes(vector<vector<int>>& edges) {\n int n = edges.size() + 1;\n unordered_map<int, vector<int>> tree;\n\n // Build the tree\n for (auto& edge : edges) {\n int u = edge[0], v = edge[1];\n tree[u].push_back(v);\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 3 | {
"code": "class Solution {\npublic:\n int n;\n vector<int> parentOf;\n bool isGood(int node, unordered_map<int, vector<int>>& adj, vector<int>& nodeSize){\n int parent = parentOf[node];\n \n unordered_set<int> sums;\n for(auto nbr : adj[node]){\n if(nbr != parent){\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 3 | {
"code": "class Solution {\nprivate:\n int dfs(int node,vector<int> &parent,unordered_map<int,list<int>> &adj,vector<bool> &visited,vector<int> &subtreeSize) {\n visited[node] = true;\n\n for(int neighbour : adj[node]) {\n // if(neighbour == parent) {\n // continue;\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 3 | {
"code": "\n\nclass Solution {\npublic:\n int countGoodNodes(vector<vector<int>>& edges) {\n int n = edges.size() + 1;\n unordered_map<int, vector<int>> tree;\n \n // Step 1: Build the tree as an adjacency list\n for (const auto& edge : edges) {\n tree[edge[0]].push_b... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 3 | {
"code": "class Solution {\npublic:\n int childcheck(int current, vector<vector<int>>& graph, set<int>& seen, int& count){\n vector<int> size;\n for (int child : graph[current]){\n if (seen.find(child) == seen.end()){\n seen.insert(child);\n int temp = childc... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 3 | {
"code": "class Solution {\n struct Node{\n int val;\n vector<Node*> neighbors;\n Node(int x): val(x){} \n };\n struct info{\n int ind;\n Node* nd;\n int size1 = 0;\n int childSizes = 0;\n bool sameSize = true;\n };\npublic:\n int countGoodNodes(... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 3 | {
"code": "class Solution {\npublic:\n int ans = 0;\n int solve(map<int,vector<int>> &m,int i){\n if(m[i].size() == 0){\n cout<<i<<endl;\n ans++;\n return 1;\n }\n if(m[i].size() == 1){\n cout<<i<<endl;\n ans++;\n return 1+so... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 3 | {
"code": "class Solution {\npublic:\n int solve(int node,int &result,vector<vector<int>>&adj,vector<int>&vis){\n unordered_map<int,int>subs;\n vis[node]=1;\n int sum=0;\n for(auto neigh:adj[node]){\n if(!vis[neigh]){\n int cnt=solve(neigh,result,adj,vis);\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 3 | {
"code": "class Solution \n{\nprivate:\n vector<unordered_set<int>> g;\n vector<bool> vis;\n int res = 0;\n \npublic:\n int countGoodNodes(vector<vector<int>>& edges) \n {\n createGraph(edges);\n \n int n = edges.size() + 1;\n vis.resize(n, false);\n dfs(0);\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 3 | {
"code": "class Solution {\npublic:\n\n int dfs(int src, vector<int>& subtreeSizes, unordered_map<int, list<int>>& adj, vector<bool>& vis) {\n int size = 1;\n vis[src] = true;\n for (int nbr : adj[src]) {\n if (!vis[nbr]) {\n size += dfs(nbr, subtreeSizes, adj, vis);... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 3 | {
"code": "class Solution {\npublic:\n bool ch(vector<int> v1) {\n if (v1.size() < 2)\n return true;\n bool b1 = true;\n for (int i = 0; i < v1.size() - 1; i++) {\n if (v1[i] != v1[i + 1])\n b1 = false;\n }\n return b1;\n }\n\n unordered... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 3 | {
"code": "class Solution {\npublic:\n int ans=0;\n int recurse(vector<vector<int>>&g,int i,set<int>&vis) {\n if(i>=g.size()||vis.find(i)!=vis.end())\n return 0;\n vector<int>p=g[i];\n set<int>sizes;\n int cnt=1;\n vis.insert(i);\n for(int j=0;j<p.size();j++)... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. 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 th... | 3 | {
"code": "class Solution {\npublic:\n long long dfs(unordered_map<int, vector<int>> &adj, int &count, int curr, vector<bool> &visit){\n visit[curr] = true;\n // if(adj[curr].size() == 1){\n // count++;\n // return 1;\n // }\n long long sum = 0;\n unordered_... |
3,535 | <p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p>
<p>We call a pair of <strong>non-negative</strong> integer arrays <code>(arr1, arr2)</code> <strong>monotonic</strong> if:</p>
<ul>
<li>The lengths of both arrays are <code>n</code>.</li>
<li><code>arr1</... | 0 | {
"code": "class Solution {\npublic:\n int countOfPairs(vector<int>& nums) {\n int cur_pos = nums[nums.size() - 1], cur_neg = 0;\n //\n for(int i = nums.size() - 2; i >= 0; i--) {\n int cur = nums[i];\n int pos_gap = 0, neg_gap = cur_neg;\n if(cur > cur_pos) {\... |
3,535 | <p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p>
<p>We call a pair of <strong>non-negative</strong> integer arrays <code>(arr1, arr2)</code> <strong>monotonic</strong> if:</p>
<ul>
<li>The lengths of both arrays are <code>n</code>.</li>
<li><code>arr1</... | 0 | {
"code": "const int A=1e3+1, MOD=1e9+7;\n\nclass Solution {\npublic:\n int countOfPairs(vector<int>& v) {\n static int dp[A], ndp[A];\n memset(dp, 0, sizeof dp);\n for (int i=0; i<=v[0]; i++) {\n dp[i]=1;\n }\n for (int i=1; i<v.size(); i++) {\n int x=v[i-1... |
3,535 | <p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p>
<p>We call a pair of <strong>non-negative</strong> integer arrays <code>(arr1, arr2)</code> <strong>monotonic</strong> if:</p>
<ul>
<li>The lengths of both arrays are <code>n</code>.</li>
<li><code>arr1</... | 0 | {
"code": "constexpr int kMod = 1000000007;\nstruct CountsByEnd {\n int offset;\n vector<int> counts;\n};\n\nCountsByEnd kEmptyCounts{\n .offset = 0\n};\n\nclass Solution {\npublic:\n CountsByEnd helper(const vector<int>& nums, int end_index) {\n if (end_index == 0) {\n return CountsByEn... |
3,535 | <p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p>
<p>We call a pair of <strong>non-negative</strong> integer arrays <code>(arr1, arr2)</code> <strong>monotonic</strong> if:</p>
<ul>
<li>The lengths of both arrays are <code>n</code>.</li>
<li><code>arr1</... | 0 | {
"code": "auto fast = (ios::sync_with_stdio(0), cout.tie(0), cin.tie(0), false);\nconstexpr int kMod = 1000000007;\nstruct CountsByEnd {\n int offset;\n vector<int> counts;\n};\nCountsByEnd kEmptyCounts{\n .offset = 0\n};\nstruct Solution {\n CountsByEnd helper(const vector<int>& nums, int end_index) {\n... |
3,535 | <p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p>
<p>We call a pair of <strong>non-negative</strong> integer arrays <code>(arr1, arr2)</code> <strong>monotonic</strong> if:</p>
<ul>
<li>The lengths of both arrays are <code>n</code>.</li>
<li><code>arr1</... | 0 | {
"code": "#include <iostream>\n#include <random>\n#include <array>\n#include <vector>\n#include <string>\n#include <map>\n#include <unordered_map>\n#include <set>\n#include <unordered_set>\n#include <algorithm>\n#include <functional>\n#include <queue>\n#include <stack>\n#include <bitset>\n\n#include <math.h>\n\nusin... |
3,535 | <p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p>
<p>We call a pair of <strong>non-negative</strong> integer arrays <code>(arr1, arr2)</code> <strong>monotonic</strong> if:</p>
<ul>
<li>The lengths of both arrays are <code>n</code>.</li>
<li><code>arr1</... | 0 | {
"code": "class Solution {\npublic:\n int countOfPairs(vector<int>& nums) {\n int n=nums.size();\n int m=nums[0];\n vector<int> dp(m+1,1);\n int mo=1e9+7;\n for(int i=1;i<n;i++){\n vector<int> neww(m+1,0);\n int c=nums[i]-nums[i-1];\n if(c>=0){\n... |
3,535 | <p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p>
<p>We call a pair of <strong>non-negative</strong> integer arrays <code>(arr1, arr2)</code> <strong>monotonic</strong> if:</p>
<ul>
<li>The lengths of both arrays are <code>n</code>.</li>
<li><code>arr1</... | 0 | {
"code": "class Solution {\npublic:\n int countOfPairs(vector<int>& nums) {\n int mod=1e9+7;\n int n=nums.size();\n long long dp[2001][1001];\n memset(dp,-1,sizeof(dp));\n auto get=[&](auto&& get,int i,int prev)->long long{\n if(i>=n)return 1;\n if(dp[i][pr... |
3,535 | <p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p>
<p>We call a pair of <strong>non-negative</strong> integer arrays <code>(arr1, arr2)</code> <strong>monotonic</strong> if:</p>
<ul>
<li>The lengths of both arrays are <code>n</code>.</li>
<li><code>arr1</... | 0 | {
"code": "class Solution {\npublic:\n int dp[2005][2005];\n int mod=1e9+7;\n int f(int idx,int prev,vector<int>&nums){\n if(idx>=nums.size()) return 1;\n\n if(dp[idx][prev]!=-1)\n return dp[idx][prev];\n\n int ans=0;\n for(int k=prev;k<=nums[idx];k++){\n int val1=k, val2=nums[idx]-val... |
3,535 | <p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p>
<p>We call a pair of <strong>non-negative</strong> integer arrays <code>(arr1, arr2)</code> <strong>monotonic</strong> if:</p>
<ul>
<li>The lengths of both arrays are <code>n</code>.</li>
<li><code>arr1</... | 1 | {
"code": "class Solution {\npublic:\n int dp[2001][52][52];\n int mod=1e9+7;\n int f(int i, int first, int second, vector<int> &a, int n){\n if(i>=n) return 1;\n if(dp[i][first][second]!=-1) return dp[i][first][second];\n \n int ans=0;\n for(int j=0; j<=a[i]; j++){\n ... |
3,535 | <p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p>
<p>We call a pair of <strong>non-negative</strong> integer arrays <code>(arr1, arr2)</code> <strong>monotonic</strong> if:</p>
<ul>
<li>The lengths of both arrays are <code>n</code>.</li>
<li><code>arr1</... | 1 | {
"code": "class Solution {\npublic:\n int mod=1e9+7;\n int dp[2001][52][52];\n int solve(int i,int n,vector<int>&v,int prev1,int prev2) {\n if(i==n)\n return 1;\n if(dp[i][prev1][prev2]!=-1)\n return dp[i][prev1][prev2];\n int ans=0;\n for(int j=prev1;j<=v[i... |
3,535 | <p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p>
<p>We call a pair of <strong>non-negative</strong> integer arrays <code>(arr1, arr2)</code> <strong>monotonic</strong> if:</p>
<ul>
<li>The lengths of both arrays are <code>n</code>.</li>
<li><code>arr1</... | 2 | {
"code": "class Solution {\n int count;\n vector<vector<int>> dp;\npublic:\n int findNumPairs(vector<int>& nums, vector<int>& arr1, vector<int>& arr2, int off) {\n if (off >= nums.size()) {\n return 1;\n }\n \n if (off > 0) {\n if (dp[off][arr1.back()] != -... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.