id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "struct my{\n int l,s,check;\n};\nclass Solution {\npublic:\n vector<int> parent;\n vector<my> max_values;\n vector<vector<int>> graph;\n void fill(int node){\n int l = INT_MIN,s = INT_MIN;\n for(auto &child: graph[node]){\n if(parent[node] == child)\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "class Solution {\npublic:\n void dfs(int i,int par,vector<vector<int>> &adj,vector<int> &res){\n res[i]=0;\n\n for(int it:adj[i]){\n\n if(it^par){\n\n dfs(it,i,adj,res);\n\n int curr=res[it]+1+(it+1)%2;\n\n if(res[i]==0){\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "class Solution {\npublic:\n int mark(vector<vector<int>> &adj,vector<int> &vis,int node,vector<pair<int,int>>& p,vector<int>& par){\n vis[node]=1;\n int mx1=0,mx2=0;\n for(auto it:adj[node]){\n if(!vis[it]){\n par[it]=node;\n int l=mark(adj,v...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
1
{ "code": "class Solution {\npublic:\n vector<int> timeTaken(vector<vector<int>>& edges) {\n int n = edges.size() + 1; // Number of nodes\n\n // Step 1: Build the adjacency list\n vector<vector<int>> adj(n);\n for (auto& edge : edges) {\n int u = edge[0], v = edge[1];\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
1
{ "code": "class Solution {\npublic:\n int n, MIN = -2000000000;\n vector<int> g[100005], res;\n int up[100005], down[100005][2];\n \n void calc_down(int i, int fa){\n int t;\n down[i][0] = down[i][1] = MIN;\n if (g[i].size() == 0 || (g[i].size() == 1 && g[i][0] == fa)){\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
1
{ "code": "class Solution {\nprivate:\n int triv(int parent, int node, vector<vector<int>>& graph, vector<int>& far1, vector<int>& far2) {\n \n for (int neigh : graph[node]) {\n if (neigh == parent) {\n continue;\n }\n int cost = (neigh % 2 == 1) ? 1 : ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
1
{ "code": "class Solution {\npublic:\n int getDist(int node, int parent, vector<vector<int>> &children, vector<int> &maxDist, vector<int> &secondMaxDist){\n int newMax = 0;\n int newSecondMax = 0;\n for(auto child : children[node]) {\n if(child == parent) continue;\n int ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
1
{ "code": "class Solution {\npublic:\n vector<int> dp, ans;\n vector<vector<int>> g;\n\n void dfs1(int u, int p){\n for(auto v: g[u]){\n if(v==p) continue;\n dfs1(v, u);\n if(v%2){\n dp[u]=max(dp[u], 1+dp[v]);\n }\n else{\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
1
{ "code": "class Solution {\n void dfs(int i, vector<vector<int>>& dp, vector<int>& vis, vector<int> adj[]) {\n vis[i] = 1;\n //dp[i][0] = \n for (int j = 0; j < adj[i].size(); j++) {\n int k = adj[i][j], v = 2 - (k % 2);\n if (!vis[k]) {\n dfs(k, dp, vis, ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
1
{ "code": "class Solution {\n void dfs(int i, vector<vector<int>>& dp, vector<int>& vis, vector<int> adj[]) {\n vis[i] = 1;\n for (int j = 0; j < adj[i].size(); j++) {\n int k = adj[i][j], v = 2 - (k % 2);\n if (!vis[k]) {\n dfs(k, dp, vis, adj);\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
1
{ "code": "class Solution {\npublic:\n void dfs(int node,int par,vector<int>&far1,vector<int>&far2,vector<int>g[]){\n int dist1=0,dist2=0;\n for(auto newnode:g[node]){\n if(newnode==par)continue;\n dfs(newnode,node,far1,far2,g);\n int dist=((newnode&1)?1:2...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
1
{ "code": "class Solution {\npublic:\n vector<int> timeTaken(vector<vector<int>>& edges) {\n int n = edges.size() + 1;\n vector<int> adj[n];\n for(auto v : edges){\n adj[v[0]].push_back(v[1]);\n adj[v[1]].push_back(v[0]);\n }\n \n vector<int> sub(n, 0);\n...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
1
{ "code": "class Solution {\npublic:\n\n#define pii pair<long long ,int >\n #define ll long long \n\n\n vector<int> timeTaken(vector<vector<int>>& edges) {\n int n=edges.size()+1;\n vector<int>res(n,0);\n vector<vector<pair<ll,ll>>>adj(n);\n\n for(auto &it:edges)\n {\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
1
{ "code": "class Solution {\npublic:\n\n#define pii pair<long long ,long long >\n #define ll long long \n\n\n vector<int> timeTaken(vector<vector<int>>& edges) {\n int n=edges.size()+1;\n vector<int>res(n,0);\n vector<vector<pair<ll,ll>>>adj(n);\n\n for(auto &it:edges)\n {\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
2
{ "code": "class Solution {\n void solve(vector<vector<int>>&adj,vector<int>&ans,vector<vector<int>>&dp,int root, int incoming,int par)\n { ans[root]=max(incoming,dp[root][0]);\n int add=(root%2==1?1:2);\n int incomhelper=dp[root][0];\n for(auto x: adj[root]) \n { if(x!=par)\n {...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
2
{ "code": "class Solution {\npublic:\n void bfs(int node,int prev,vector<vector<int>> &dp,vector<int> adj[]){\n\n for(auto it:adj[node]){\n if(it!=prev){\n bfs(it,node,dp,adj);\n int curr=dp[0][it] + (it%2 ? 1 : 2);\n if(curr>dp[0][node]){\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
2
{ "code": "class Solution {\npublic:\n vector<int> timeTaken(vector<vector<int>>& edges) {\n const int n = edges.size() + 1;\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<int> p(n, -1)...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> adj,dp;\n vector<int> ans,max_child;\n int calc(int node)\n {\n return 1 + (node%2 == 0);\n }\n int dfs1(int node,int par)\n {\n int max1 = 0, max2 = 0,cost;\n max_child[node] = -1;\n for(auto child : adj[node]...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
2
{ "code": "class Solution {\npublic:\n // unordered_map<int, unordered_map<int, int>> pos_child;\n // vector<unordered_map<int, int>> pos_child;\n vector<vector<pair<int, int>>> pos_child;\n // unordered_map<int, int> pos_child[100000];\n vector<int> timeTaken(vector<vector<int>>& edges) {\n int...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
2
{ "code": "class Solution {\npublic:\n int dfs(int u, int p, vector<vector<int>>& adj, vector<int>& parent, vector<int>& temp){\n\n int res=0;\n\n parent[u]=p;\n\n for(auto v:adj[u]){\n if(v==p)continue;\n if(v%2==1)res=max(res, 1+dfs(v, u, adj, parent, temp));\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
2
{ "code": "class Solution {\npublic:\n\n vector< vector<int> > adj;\n vector< vector< pair<int, int> > > dist;\n vector<int> ans;\n int n;\n\n vector<int> timeTaken(vector<vector<int>>& edges) {\n n = edges.size() + 1;\n adj.assign(n, vector<int>());\n for (auto& item : edges)\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
2
{ "code": "class Solution {\npublic:\n\n vector< vector<int> > adj;\n vector< vector< pair<int, int> > > dist;\n vector<int> ans;\n int n;\n\n vector<int> timeTaken(vector<vector<int>>& edges) {\n n = edges.size() + 1;\n adj.assign(n, vector<int>());\n for (auto& item : edges)\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
2
{ "code": "class Solution {\npublic:\n\n vector<int> ans;\n vector<vector<int>> dp;\n void maxTime(int node,int par,vector<int> adj[]){\n for(auto it: adj[node]){\n if(it!=par){\n\n maxTime(it,node,adj);\n int add= ((it&1) ? 1 : 2);\n if(add+dp[i...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
2
{ "code": "class Solution {\npublic:\n\n void dfs(vector<int> adj[], int curr, int prev,vector<vector<int>>&dp){\n int dis;\n for(auto x: adj[curr]){\n if(x!=prev){\n //if(x<=curr) continue;\n dfs(adj, x, curr,dp);\n \n dis = dp[x...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
2
{ "code": "class Solution {\npublic:\n vector<int> ans;\n vector<int> asc;\n vector<vector<int>> dp;\n int dfs1(int node,int par,vector<int> tree[]) {\n int curr = 0;\n for(auto child:tree[node]) {\n if(child != par) {\n curr=(child%2)?1+dfs1(child,node,tree):2+dfs1(...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
2
{ "code": "class Solution {\npublic:\n bool isleaf(int start,vector<int> adj[])\n {\n if(adj[start].size()==1 && start!=0)\n {\n return true;\n }\n return false;\n }\n int dfs(int start,int parent,vector<int>adj[],vector<vector<pair<int,int>>>& v)\n {\n pai...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
2
{ "code": "#define pb push_back\nclass Solution {\npublic:\n void dfs(vector<vector<int>>&adj,int node,int parent,vector<int>&dp,int par_ans){\n vector<int>pre,suff;\n for(auto &i:adj[node]){\n if(i!=parent){\n pre.pb(dp[i]+((i&1)?1:2));\n suff.pb(dp[i]+((i&1)?1:2));\n...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> dp;\n vector<vector<int>> g;\n vector<int> ans;\n void helper1(int i,int par){\n int mx1=0,mx2=0;\n for(auto &j:g[i]){\n if(j!=par){\n helper1(j,i);\n int add=(j%2)?1:2;\n int c...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
2
{ "code": "class Solution {\npublic:\n int dfs(int node,vector<int> & parent , vector<int> & dp , vector<vector<int>> & list){\n int ans =0 ;\n for(auto child: list[node]){\n if(parent[child]!=-1){\n continue ;\n }\n parent[child] = node ;\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
2
{ "code": "class Solution {\npublic:\n int n;\n vector<vector<int>> adj;\n unordered_map<long long, int> child_time;\n vector<int> res;\n long long get_key(int u, int v){\n long long k = u;\n k = k * 1000000 + v;\n return k;\n }\n vector<int> timeTaken(vector<vector<int>>& ed...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
2
{ "code": "class Solution {\npublic:\n void dfs(int node,int parent,vector<vector<int>>&adj,vector<vector<int>>&dp)\n {\n int curr;\n for(auto it:adj[node])\n {\n if(it==parent)\n continue;\n dfs(it,node,adj,dp);\n curr=dp[it][0]+(it%2?1:2);\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
2
{ "code": "#define pb push_back\n#define ll long long\n#define f(i,s,e) for(long long i=s;i<e;i++)\n\nclass Solution {\npublic:\n void dfs(int root,int parent,vector<vector<int>>&adj, vector<vector<int>>&dp){\n for(auto it:adj[root]){\n if(it != parent){\n dfs(it,root,adj,dp);\n int a...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
2
{ "code": "class Solution {\npublic:\n#define F first\n#define S second\n#define ld long double\n#define pii pair<int, int>\n \n void dfs1(int i, int par, vector<pii>in[], vector<int>adj[]){\n bool leaf=true;\n for(auto &j:adj[i]){\n if(j!=par){\n dfs1(j,i,in,adj);\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
2
{ "code": "class Solution {\npublic:\n int dfs(int node, vector<int> &vis, vector<vector<int>> &maxSecondMaxInd, vector<int> adj[]) {\n vis[node] = 1;\n int maxTime = 0, secondMaxTime = 0, ind = -1;\n for(auto it: adj[node]) {\n if(vis[it] == 1) {\n continue;\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
2
{ "code": "class Solution {\npublic:\n void dfs(vector<vector<int>>& tree, int node, int par, vector<int>& time){\n int flag=0;\n for(auto child:tree[node]){\n if(child==par)continue;\n time[child]=time[node]+(child%2?1:2);\n dfs(tree,child,node,time);\n }\n }\n\n int dfs2(vector<vector<...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
2
{ "code": "class Solution {\n vector<vector<int>>gr;\n vector<int>par;\n vector<int>ans;\n vector<vector<int>>dist;\npublic:\n int dfs(int c,int p)\n {\n // par[c]=p;\n int a=0;\n int ind=0;\n for(auto u:gr[c])\n {\n if(u!=p)\n {\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
2
{ "code": "class Solution {\npublic:\n int n;\n // vector<vector<int>> adj;\n vector<int> res;\n // unordered_map<int, unordered_map<int, int>> chil, unordered_map<int, int>> child_time;\n // unordered_map<int, unordered_map<int, int>> child_time;\n \n unordered_map<long long, int> child_time;\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
2
{ "code": "class Solution {\npublic:\n int n;\n vector<vector<int>> adj;\n unordered_map<long long, int> child_time;\n vector<int> res;\n long long get_key(int u, int v){\n long long k = u;\n k = k * 1000000 + v;\n return k;\n }\n vector<int> timeTaken(vector<vector<int>>& ed...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
2
{ "code": "vector<vector<int>>adj;\nvector<int>dp;\nvector<vector<int>>cc;\nvector<int>ans;\nvoid dfs(int node,int par){\n int leaf=0;\n for(auto child:adj[node]){\n if(child!=par){\n leaf++;\n dfs(child,node);\n if(child%2==0){\n dp[node]=max(dp[node],dp[c...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
2
{ "code": "class Solution {\npublic:\n vector<int> timeTaken(vector<vector<int>>& edges) {\n int n = 0;\n for(auto e : edges) {\n n = max(n, max(e[0], e[1]));\n }\n n++;\n vector<int> in(n, 0), out(n, 0);\n vector<int> maxi1(n, -1), maxi2(n, -1);\n vector...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
2
{ "code": "class Solution {\n int n;\n vector<vector<int>> a;\n vector<int> dp, ans;\n\n void dfs(int s , int p = -1) {\n for (auto it : a[s]) {\n if (it == p) continue;\n dfs(it, s);\n dp[s] = max(dp[s], dp[it] + 1 + (it%2 == 0));\n }\n }\n\n void dfs1...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
2
{ "code": "class Solution {\npublic:\n\n\n void disBelow(vector<int> adj[] , int node , int par , vector<int> &dp){\n int ans = 0;\n for(auto child : adj[node]){\n if(child!=par){\n disBelow(adj , child , node , dp);\n ans =max( ans , (child%2==0?2:1) + dp[chi...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> dist, adj, node;\n vector<int> par, soln;\n void dfs1(int i, int p)\n {\n int x = 0;\n for(int j : adj[i])\n {\n if(j == p) continue;\n dfs1(j, i);\n if(j % 2) x = dist[j][1] + 1;\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
2
{ "code": "class Solution {\npublic:\n int dfs(int node,vector<int> &v,vector<vector<int>> &adj,vector<vector<pair<int,int>>> &dist)\n {\n v[node]=1;\n dist[node].push_back({-1,0});\n dist[node].push_back({-1,0});\n for(int i=0;i<adj[node].size();i++)\n {\n int val=...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "// https://leetcode.com/problems/time-taken-to-mark-all-nodes/\n\n\n#include <bits/stdc++.h>\nusing namespace std;\n#include<ext/pb_ds/assoc_container.hpp>\n#include<ext/pb_ds/tree_policy.hpp>\nusing namespace __gnu_pbds;\ntypedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_upda...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\n int h[100001];\n vector<int>ans;\n void maxh(int node,int par,vector<pair<int,int>>adj[]){\n h[node]=0;\n for(auto it:adj[node]){\n if(it.first!=par){\n maxh(it.first,node,adj);\n h[node]=max(h[node],h[it.first]+it.second);\...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n #define ll long long\n #define vll vector<ll>\n\n struct seg{\n vll v, lazy;\n int sz = 1;\n\n seg(int len, int val ){\n while(sz <= len) sz *= 2;\n v.assign(2 * sz , val);\n lazy.assign(2 * sz, 0ll);\n }\n\n ll q...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>>adj,dp;\n vector<int>v;\n void dfs(int node,int par){\n bool leaf=1;\n for(auto i:adj[node]){\n if(i==par)continue;\n leaf=0;\n dfs(i,node);\n }\n if(leaf)return ;\n int mx=0,mx1=0;...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "\n#define pii pair<int, int>\nstruct Edge{\n int from, to, weight;\n};\nstruct Node{\n int best = 0, second = 0;\n vector<Edge> edges;\n int vis = 0;\n};\nclass Solution {\npublic:\n vector<int> timeTaken(vector<vector<int>>& edges) {\n int n = 0;\n unordered_map<int, Node> gra...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n void dfs(int node, int pp, vector<int> adj[], vector<int>& dist) {\n for (auto it : adj[node]) {\n if (it != pp) {\n dfs(it, node, adj, dist);\n dist[node] = max(dist[node], (it & 1) ? 1 + dist[it] : 2 + dist[it]);\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class NodeInfo{\npublic :\n int node ;\n int time ;\n NodeInfo(int n=0 , int t=0){\n node = n , time = t ;\n }\n};\n\nclass Solution {\npublic:\n void solve(int node , int parent , unordered_map<int,list<int>> &adj , vector<pair<NodeInfo,NodeInfo>> &dp ){\n\n for(auto el : adj[...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define ll long long int\n#define INF 1000000000000000 //1e17\nconst ll mod = 998244353;\n\nvoid dfs1(ll vertex , ll par ,vector<ll>adj[] ,ll vis[] ,ll dp[] ){\n vis[vertex] =1 ; \n \n bool leaf = true ;\n for(auto child:adj[vertex] ){\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\n vector<vector<int>> dp; // dp[i][0] is answer for ith node\n int dfs(int node, int par, vector<int> adj[], int &n){\n if(dp[node][0] != -1){\n return dp[node][0];\n }\n\n dp[node].pop_back(); // remove -1\n\n vector<int> temp;\n for(...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n int n;\n vector<vector<int>> adj;\n unordered_map<long long, int> child_time;\n vector<int> res;\n long long get_key(int u, int v){\n long long k = u;\n k = k * 1000000 + v;\n return k;\n }\n vector<int> timeTaken(vector<vector<int>>& ed...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n\n vector<vector<pair<int,int>>> ans;\n\n void put_it(int cur,int val,int nei)\n {\n if(val>ans[cur][0].first)\n {\n ans[cur][1].first=ans[cur][0].first;\n ans[cur][1].second=ans[cur][0].second;\n\n ans[cur][0].first=val;\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n void dfs(int node, int parent, unordered_map<int,vector<int>>& adj, vector<vector<int>>& dp){\n\n int cur = 0;\n for(auto &v: adj[node]){\n if(v == parent)\n continue;\n \n dfs(v, node, adj, dp);\n\n cur...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution \n{\npublic:\n vector<priority_queue<pair<int, int>>> times;\n vector<vector<int>> adj;\n vector<int> ans;\n //dfs, postorder\n int postorder(int ptr, int parent)\n {\n int t = 0;\n for(auto& it : adj[ptr])\n {\n if(it == parent)\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\nprivate:\n // attributes, to represent a tree/graph\n int n;\n vector<vector<int>> adj;\n vector<int> subtree_ans;\n vector<int> non_subtree_ans;\n\n // methods\n /*\n dfs to find the subtree ans\n that is what is the maximum time it would take to mark \n eve...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n vector<int> timeTaken(vector<vector<int>>& edges) {\n int n = edges.size() + 1;\n vector<vector<int>> graph(n);\n for (auto& edge : edges) {\n graph[edge[0]].push_back(edge[1]);\n graph[edge[1]].push_back(edge[0]);\n }\n\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n void timeTakenForSubtree(int node, int prev, vector<vector<int>>&adj, vector<int>&dp) {\n int ans = 0;\n for(int next : adj[node]) {\n if(next == prev) {\n continue;\n }\n timeTakenForSubtree(next, node, adj, dp);\...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "// Idea: Give the value to the nodes\n// Give value = 1 to odd nodes\n// Give value = 2 to even nodes\n// now to calc time for a node i , you need to assume a tree root at i and then find max path sum from node i to any leaf node of that tree\n\nclass Solution {\n int getValue(int i)\n {\n ret...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "// Idea: Give the value to the nodes\n// Give value = 1 to odd nodes\n// Give value = 2 to even nodes\n// now to calc time for a node i , you need to assume a tree root at i and then find max path sum from node i to any leaf node of that tree\n\nclass Solution {\n int getValue(int i)\n {\n ret...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\n public:\n vector<int> dst;\npublic:\n int dfs(int node,int parent,vector<int> adj[]){\n int mx=0;\n for(auto it:adj[node]){\n if(it==parent)continue;\n if(it&1){\n mx=max(mx,1+dfs(it,node,adj));\n }else{\n mx=max(...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n vector<int> timeTaken(vector<vector<int>>& edge) {\n int n = (int)edge.size() + 1;\n vector<vector<int>> edges(n);\n for(auto i : edge){\n int u = i[0],v = i[1];\n edges[u].push_back(v);\n edges[v].push_back(u);\n }...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\n void dfs(int node,int parent,vector<vector<int>>&g,vector<int>&temp)\n {\n int mx=0;\n for(auto child : g[node])\n {\n if(child==parent)\n continue;\n dfs(child,node,g,temp);\n mx=std::max(mx,temp[child]+2-(child%...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\n void dfs(int node,int parent,vector<vector<int>>&g,vector<int>&temp)\n {\n int mx=0;\n for(auto child : g[node])\n {\n if(child==parent)\n continue;\n dfs(child,node,g,temp);\n mx=std::max(mx,temp[child]+2-(child%...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n vector<int> timeTaken(vector<vector<int>>& edges) {\n int n = edges.size() + 1;\n vector<vector<int>> graph(n, vector<int>());\n for(auto e: edges) {\n int u = e[0], v = e[1];\n graph[u].emplace_back(v);\n graph[v].emplace...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n vector<int> timeTaken(vector<vector<int>>& edges) {\n int n = edges.size() + 1;\n vector<vector<int>> graph(n, vector<int>());\n for(auto e: edges) {\n int u = e[0], v = e[1];\n graph[u].emplace_back(v);\n graph[v].emplace...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n\n int dfs(vector<vector<int> > &v, int s, int p, vector<pair<int, int> > &dp1, vector<pair<int, int> > &dp2)\n {\n vector<pair<int, int> > B; \n B.push_back({0, -1}); B.push_back({0, -1});\n for(auto &it : v[s]) {\n if(it != p) {\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\n void dfs(int node, int parent, unordered_map<int, list<int>>& adj, vector<vector<int>>& dp){\n for(auto nbr: adj[node]){\n if(nbr != parent){\n dfs(nbr, node, adj, dp);\n\n int curr = dp[nbr][0] + ((nbr&1)?1:2);\n if(dp[no...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "#include <vector>\n#include <unordered_map>\n#include <queue>\n#include <algorithm>\nusing namespace std;\n\nclass Solution {\npublic:\n vector<int> timeTaken(vector<vector<int>>& edges) {\n unordered_map<int, vector<int>> edgesMap;\n for (auto& edge : edges) {\n edgesMap[edge[0...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n int n;\n vector<vector<int>> adj;\n unordered_map<long long, int> child_time;\n vector<int> res;\n long long get_key(int u, int v){\n long long k = u;\n k = k * 1000000 + v;\n return k;\n }\n vector<int> timeTaken(vector<vector<int>>& ed...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n\n void dfs(int u, vector<vector<int>>&a, vector<int>&dp, int p = -1)\n {\n\n for(int i:a[u])\n {\n if(p!=i)\n {\n dfs(i, a, dp, u);\n if(i%2==0)\n dp[u] = max(dp[i] + 2, dp[u]);\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n const int INF = 1e8;\n \n vector<int> timeTaken(vector<vector<int>>& edges) {\n int n = edges.size() + 1;\n \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 ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n void S_ans(int cur,int parent, vector<int> &SubtreeAns,vector<int> adj[]){\n int ans1 = 0;\n for(auto child:adj[cur]){\n if(child!=parent){\n S_ans(child,cur,SubtreeAns,adj);\n int temp = (child%2==0) ? 2 : 1;\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n vector<int> timeTaken(vector<vector<int>>& edges) {\n int n= (int)edges.size() + 1;\n vector<vector<int>> graph(n);\n for (vector<int>& each: edges) {\n graph[each[0]].push_back(each[1]);\n graph[each[1]].push_back(each[0]);\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n void preDfs(int node, int par, vector<vector<int>> &adj, vector<int> &dp, vector<multiset<int>> &ms){\n int t=0;\n for(auto it:adj[node]){\n if(it!=par){\n preDfs(it, node, adj, dp, ms);\n if(it&1){\n t...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n void preDfs(int node, int par, vector<vector<int>> &adj, vector<int> &dp, vector<multiset<int>> &ms){\n int t=0;\n for(auto it:adj[node]){\n if(it!=par){\n preDfs(it, node, adj, dp, ms);\n if(it&1){\n t...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\n int find_height(int node, vector<int> adj[], vector<int>&height, vector<bool>&vis, vector<set<pair<int,int>, greater<>>> &bigChild, vector<int>&par){\n int maxi = 0;\n for(auto subnode:adj[node]){\n if(vis[subnode]) continue;\n vis[subnode]=1;\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n vector<int> sub,ans;\n vector<vector<int>> g;\n int n;\n void fn(int x=0,int par=-1)\n {\n sub[x]=0;\n for(int u:g[x])\n {\n if(u!=par)\n {\n fn(u,x);\n if(u%2==0)\n sub[x]...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n vector<int> timeTaken(vector<vector<int>>& edges) {\n long long n = edges.size() + 1;\n vector<vector<int>> adj(n);\n for (auto edge: edges) {\n int a = edge[0], b = edge[1];\n adj[a].push_back(b);\n adj[b].push_back(a);\n...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n vector<multiset<int>>v;\n vector<int>parent,adj[(int)1e5+1];\n vector<int>ans;\n int dfs(int i,int p) {\n\n parent[i]=p;\n int curr = 0;\n for (int j: adj[i]) {\n if (j == p)continue;\n int now = dfs(j, i);\n v[i]...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>>a;\n int n;\n vector<int>down;\n vector<int>up;\n\n void pre(int x , int p) {\n down[x] = 0;\n for(auto &u : a[x]) {\n if(u == p) continue;\n pre(u,x);\n if(u%2 == 1) {\n down[x] = ma...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>>a;\n int n;\n vector<int>down;\n vector<int>up;\n\n void pre(int x , int p) {\n down[x] = 0;\n for(auto &u : a[x]) {\n if(u == p) continue;\n pre(u,x);\n if(u%2 == 1) {\n down[x] = ma...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "int dfs(vector<vector<int>>&graph, vector<int>&vis, int u, vector<int>&t){\n vis[u] = 1;\n for(int v : graph[u]){\n if(vis[v])continue;\n int x = dfs(graph,vis,v,t);\n if(v%2)t[u] = max(t[u], x+1);\n else\n t[u] = max(t[u],x+2);\n }\n return t[u];\n\n}\nvo...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n vector<int> grph[100001];\n int times[100001];\n int vs[100001];\n \n void dfs(int i, int par){\n times[i]=0;\n for(auto p:grph[i]){\n if(p==par) continue;\n dfs(p,i);\n if(p%2) times[i]=max(times[i],1+times[p]);\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n unordered_map<int, unordered_map<int, int>> child_time;\n vector<int> timeTaken(vector<vector<int>>& edges) {\n int n = edges.size() + 1;\n cout << child_time.bucket_count() << '\\n';\n child_time.reserve(100 * n);\n \n cout << child_time...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n vector<int>sub;\n int dfs1(int node, int par, vector<int> adj[]) {\n for(auto it:adj[node]) {\n if(it != par) {\n dfs1(it,node,adj);\n sub[node] = max(sub[it],sub[node]);\n }\n }\n if(node & 1) sub[no...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n int dfs(int ind, int par, vector<vector<int>>& g, vector<int>& dp, vector<multiset<int>>& cc) {\n if (dp[ind] != -1) return dp[ind];\n\n int self = 2 - (ind % 2);\n int maxChild = 0;\n\n for (auto it : g[ind]) {\n if (it != par) {\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n\n int dfs(int ind, int par, vector<vector<int>>& g, vector<int>& dp, vector<multiset<int>>& cc){\n \n if(dp[ind] != -1) return dp[ind];\n\n int self = 2 - (ind % 2);\n\n int maxChild = 0;\n\n for(auto it : g[ind]){\n if(it != pa...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>>g;\n vector<int> sub,ans;\n int dfs(int node,int par)\n { \n int ans = 0;\n for(auto child: g[node])\n {\n if(child == par)continue;\n ans=max(ans,dfs(child,node));\n }\n if(node&1) sub[nod...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n\tvector<vector<int>> e;\n\tvector<int> f, d, u;\n\tint n;\n\tint dfs(int x, int fa) {\n\t\tf[x] = fa;\n\t\tfor (auto y : e[x]) {\n\t\t\tif (y == fa) continue;\n\t\t\tint tmp = dfs(y, x) + 1 + ((y+1) % 2);\n\t\t\td[x] = max(tmp, d[x]);\n\t\t\t// cout << \"dfs \" << x << \" \" << ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n vector<int> subtree_ans;\n vector<int> non_subtree;\n void dfs1(int node,int parent,vector<vector<int>>&graph)\n {\n // cout<<subtree_ans[node]<<\" \";\n for(auto it:graph[node])\n {\n if(it==parent)\n continue;\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n vector<int>time;\n int n;\n vector<vector<int>>g;\n vector<multiset<int>>v;\n int f(int node,int pa){\n v[node].insert(0);\n for(auto&i:g[node]){\n if(i==pa)continue;\n int curr=f(i,node);\n int fac=i%2?1:2;\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n vector<int> dp;\n vector<vector<int>> adj;\n vector<int> ans;\n\n void dfs( int node , int par ){\n if( adj[node].size() == 1 && par != -1 ){\n dp[node] = 0;\n return;\n }\n int val = 0;\n for( auto i : adj[node] ){\n...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n \n vector<int> maxPt, res;\n \n int util(vector<vector<int>> &g, int index, int prev) {\n int maxEl = 0;\n \n for(auto x: g[index]) {\n if(x == prev) continue;\n maxEl = max(maxEl, util(g, x, index));\n }\n \n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "#include <vector>\n#include <set>\n\nusing namespace std;\n\nclass Solution {\npublic:\n vector<int> timeTaken(vector<vector<int>>& edges) {\n int n = static_cast<int>(edges.size()) + 1;\n vector<multiset<int>> s(n);\n vector<vector<int>> g(n);\n\n // Build the graph\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "#define lli long long int \n#define ld long double\n#define vi vector<int>\n#define vlli vector<lli>\n#define vpii vector<pair<int, int>>\n#define pb push_back \n\ntemplate<typename T> void debug(T _a) {cout << _a << \" \";}\ntemplate<typename T1, typename T2> void debug(pair<T1, T2> _p) {cout<<\"{\";debug...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "class Solution {\npublic:\n\n multiset<int> mp[100010];\n\n void dd(int x, vector<vector<int>> &edge, vector<bool> &vis) {\n for(auto y: edge[x]) {\n if (vis[y]) continue;\n vis[y] = true;\n dd(y, edge, vis);\n int d;\n if (y & 1) d = 1;\n...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
3
{ "code": "const int N = 1e5 + 5;\nclass Solution {\n vector<int> tree[N];\n int t[N];\n void dfs(int c,int par)\n {\n t[c] = 0;\n for(auto& ele : tree[c])\n {\n if(ele == par) continue;\n dfs(ele,c);\n t[c] = max(t[c],t[ele] + (ele&1 ? 1 : 2));\n ...