id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
2,206 | <p>You are given a list of bombs. The <strong>range</strong> of a bomb is defined as the area where its effect can be felt. This area is in the shape of a <strong>circle</strong> with the center as the location of the bomb.</p>
<p>The bombs are represented by a <strong>0-indexed</strong> 2D integer array <code>bombs</... | 3 | {
"code": "const int INF = numeric_limits<int>::max();\n\nclass CustomGraph{\n private:\n int V;\n vector<vector<int>> adj;\n\n public:\n CustomGraph(int v);\n void addEdge(int u, int v);\n int maximumDetonation(vector<bool>& visited, int start);\n};\n\nCustomGraph::CustomGrap... |
2,206 | <p>You are given a list of bombs. The <strong>range</strong> of a bomb is defined as the area where its effect can be felt. This area is in the shape of a <strong>circle</strong> with the center as the location of the bomb.</p>
<p>The bombs are represented by a <strong>0-indexed</strong> 2D integer array <code>bombs</... | 3 | {
"code": "class Solution {\npublic:\n\n bool IsEdgeFormed(vector<int> a, vector<int> b)\n {\n long long r1 = static_cast<long long>(a[2]);\n long long r2 = static_cast<long long>(b[2]); \n\n long long x = abs(a[0]-b[0]);\n long long y = abs(a[1]-b[1]);\n\n long long in... |
2,206 | <p>You are given a list of bombs. The <strong>range</strong> of a bomb is defined as the area where its effect can be felt. This area is in the shape of a <strong>circle</strong> with the center as the location of the bomb.</p>
<p>The bombs are represented by a <strong>0-indexed</strong> 2D integer array <code>bombs</... | 3 | {
"code": "//this is max of component? WRONG, it's directed graph A can detonate B but B might not be able to detonate A like in example1\n\nclass Solution {\npublic:\n int maximumDetonation(vector<vector<int>>& bombs) {\n unordered_map<int, vector<int>> adj;\n for(int i = 0; i < bombs.size(); i++) {... |
2,206 | <p>You are given a list of bombs. The <strong>range</strong> of a bomb is defined as the area where its effect can be felt. This area is in the shape of a <strong>circle</strong> with the center as the location of the bomb.</p>
<p>The bombs are represented by a <strong>0-indexed</strong> 2D integer array <code>bombs</... | 3 | {
"code": "class Solution {\npublic:\n\n bool isInRange(vector<int> bomb1, vector<int> bomb2) {\n\n long long dis = ((long long)(bomb1[0] - bomb2[0])*(bomb1[0] - bomb2[0])) + ((long long)(bomb1[1] - bomb2[1])*(bomb1[1] - bomb2[1]));\n\n if(dis <= (long long) bomb1[2]*bomb1[2]) {\n return t... |
2,206 | <p>You are given a list of bombs. The <strong>range</strong> of a bomb is defined as the area where its effect can be felt. This area is in the shape of a <strong>circle</strong> with the center as the location of the bomb.</p>
<p>The bombs are represented by a <strong>0-indexed</strong> 2D integer array <code>bombs</... | 3 | {
"code": "class Solution {\npublic:\n\n void dfs(int u, vector<int>* graph, vector<int> &vis, int &k){\n if(vis[u]==1) return;\n vis[u] = 1;\n k++;\n for(auto v : graph[u]){\n dfs(v,graph,vis,k);\n }\n return;\n }\n int maximumDetonation(vector<vector<int... |
2,206 | <p>You are given a list of bombs. The <strong>range</strong> of a bomb is defined as the area where its effect can be felt. This area is in the shape of a <strong>circle</strong> with the center as the location of the bomb.</p>
<p>The bombs are represented by a <strong>0-indexed</strong> 2D integer array <code>bombs</... | 3 | {
"code": "class Solution {\npublic:\n int maximumDetonation(vector<vector<int>>& bombs) {\n return getMaxDetonation(bombs);\n }\n\n int getMaxDetonation(vector<vector<int>>& bomb)\n {\n \n vector<unordered_map<int, int>> distMap;\n // vector<int> steps1(bomb.size(), 0);\n ... |
2,206 | <p>You are given a list of bombs. The <strong>range</strong> of a bomb is defined as the area where its effect can be felt. This area is in the shape of a <strong>circle</strong> with the center as the location of the bomb.</p>
<p>The bombs are represented by a <strong>0-indexed</strong> 2D integer array <code>bombs</... | 3 | {
"code": "class Solution {\npublic:\n int maximumDetonation(vector<vector<int>>& bombs) {\n return getMaxDetonation(bombs);\n }\n\n int getMaxDetonation(vector<vector<int>>& bomb)\n {\n \n // vector<unordered_map<int, int>> distMap;\n // vector<int> steps1(bomb.size(), 0);\n ... |
2,206 | <p>You are given a list of bombs. The <strong>range</strong> of a bomb is defined as the area where its effect can be felt. This area is in the shape of a <strong>circle</strong> with the center as the location of the bomb.</p>
<p>The bombs are represented by a <strong>0-indexed</strong> 2D integer array <code>bombs</... | 3 | {
"code": "#define ll long long\n\nclass Solution {\npublic:\n int dfs(vector<vector<int>>& adj, int node, vector<int>& isVisited){\n isVisited[node]=1;\n int answer=1;\n for(auto it:adj[node]){\n if(!isVisited[it]) answer+=dfs(adj, it, isVisited);\n }\n return answer;... |
2,206 | <p>You are given a list of bombs. The <strong>range</strong> of a bomb is defined as the area where its effect can be felt. This area is in the shape of a <strong>circle</strong> with the center as the location of the bomb.</p>
<p>The bombs are represented by a <strong>0-indexed</strong> 2D integer array <code>bombs</... | 3 | {
"code": "class Solution {\npublic:\n int maximumDetonation(vector<vector<int>>& bombs) {\n int n = bombs.size();\n vector<vector<int>> adj(n);\n\n for(int i=0;i<n;i++) {\n for(int j=0;j<n;j++) {\n if(i!=j && is_connected(bombs[i], bombs[j])) {\n a... |
2,206 | <p>You are given a list of bombs. The <strong>range</strong> of a bomb is defined as the area where its effect can be felt. This area is in the shape of a <strong>circle</strong> with the center as the location of the bomb.</p>
<p>The bombs are represented by a <strong>0-indexed</strong> 2D integer array <code>bombs</... | 3 | {
"code": "class Solution {\npublic:\n void dfs(unordered_map<int, vector<int>> &adj, int node, int &tmp, vector<int> &visited) {\n visited[node] = 1;\n tmp++;\n\n for(int x: adj[node]) {\n if(!visited[x]) {\n dfs(adj, x, tmp, visited);\n }\n }\n\n ... |
2,206 | <p>You are given a list of bombs. The <strong>range</strong> of a bomb is defined as the area where its effect can be felt. This area is in the shape of a <strong>circle</strong> with the center as the location of the bomb.</p>
<p>The bombs are represented by a <strong>0-indexed</strong> 2D integer array <code>bombs</... | 3 | {
"code": "class Solution {\npublic:\n\n bool isIntersect(vector<int> a, vector<int> b) {\n // (x1 - x2)^2 + (y1 - y2)^2 = r^2\n long long eq = ((a[1] - b[1]) * 1ll * (a[1] - b[1])) + ((a[0] - b[0]) * 1ll * (a[0] - b[0])) - (a[2] * 1ll * a[2]);\n return eq <= 0; \n }\n\n int maximumDeton... |
2,206 | <p>You are given a list of bombs. The <strong>range</strong> of a bomb is defined as the area where its effect can be felt. This area is in the shape of a <strong>circle</strong> with the center as the location of the bomb.</p>
<p>The bombs are represented by a <strong>0-indexed</strong> 2D integer array <code>bombs</... | 3 | {
"code": "class Solution {\npublic:\n bool willDetonate(vector<int> a, vector<int> b)\n {\n long long rad = 1ll*a[2]*a[2];\n long long dist = 1ll*pow(a[0]-b[0], 2) + 1ll*pow(a[1]-b[1], 2);\n return rad >= dist ? true : false;\n }\n\n int dfs(int src, vector<vector<int>>& graph, vecto... |
2,206 | <p>You are given a list of bombs. The <strong>range</strong> of a bomb is defined as the area where its effect can be felt. This area is in the shape of a <strong>circle</strong> with the center as the location of the bomb.</p>
<p>The bombs are represented by a <strong>0-indexed</strong> 2D integer array <code>bombs</... | 3 | {
"code": "class Solution {\npublic:\n long sq(int x){\n return (long) x * (long) x;\n }\n bool inrange(vector<int> b1, vector<int> b2){ //if b1 -> b2, b2 in range of b1\n long dist = sq(b2[0] - b1[0]) + sq(b2[1]-b1[1]);\n return dist <= sq(b1[2]);\n } \n int maximumDetonation(vect... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 0 | {
"code": "/*\n1. Start at 0 and end at 0.\n2. traverse further iff apples left to take.\n3. Answer is twice edges visited.\n*/\nconst int speedup = [] { ios::sync_with_stdio(0); cin.tie(0); return 0; }();\nclass Solution {\n void dfs(int node,int parent,vector<vector<int>>& adj,vector<bool>& hasApple,int& apples,... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 0 | {
"code": "class Solution {\npublic:\n int dfs(int curr_node, int prev_node, vector<int> adj[], vector<bool>& hasApple)\n {\n int time = 0;\n\n for(auto it: adj[curr_node])\n {\n if(it == prev_node)\n continue;\n\n int time_taken_by_child = dfs(it, curr_... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 0 | {
"code": "class Solution {\npublic:\n int dfs(vector<int>adj[],vector<bool>&hasapple,int node,int parent){\n int distance=0;\n for(auto it : adj[node]){\n if(it!=parent){\n int temp=dfs(adj,hasapple,it,node);\n if(hasapple[it]){\n distance... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 0 | {
"code": "class Solution {\npublic:\n\n int func(int i,int parent, vector<int> adj[], vector<bool> &hasApple){\n if(adj[i].size() == 1 && i!=0){\n if(hasApple[i]) return 1;\n else return 0;\n }\n\n int ans=0;\n\n for(auto it : adj[i]){\n if(it == parent... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 0 | {
"code": "class Solution {\n bool dfs(vector<int> adj[], int vertex, vector<bool>& hasApple, int& time, vector<bool>& vis){\n // mark the vertex as visited\n vis[vertex] = true; \n\n // if the vertex itself contains an apple, initialize found to true, false otherwise\n bool found = has... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 0 | {
"code": "class Solution {\npublic:\n void adjList(vector<vector<int>>& edg, vector<vector<int>>& adj){\n for(auto& edge : edg){\n adj[edge[0]].push_back(edge[1]);\n adj[edge[1]].push_back(edge[0]);\n }\n }\n\n int traverse(vector<vector<int>>& adj, int start, vector<bool... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 1 | {
"code": "class Solution {\npublic:\n\n int dfs(int node, int currCost, vector<int> adj[], vector<bool>& hasApple, vector<int>& vis){\n if(vis[node] == 1)return 0;\n vis[node] = 1;\n int childCost = 0;\n for(auto it:adj[node]){\n childCost += dfs(it,2, adj, hasApple, vis);\n... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 1 | {
"code": "class Solution {\npublic:\n\nint dfs(vector<int>adj[], vector<bool>& hasApple,int node,vector<int>&vis){\n vis[node]=1;\n int total_cost=0;\n for(auto it:adj[node]){\n if(!vis[it]){\n int res=dfs(adj,hasApple,it,vis);\n if(res>0 || hasApple[it]){\n tota... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 1 | {
"code": "class Solution {\npublic:\n int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {\n\n vector<vector<int>>adj(n+1);\n vector<int>visited(n+1,0);\n for(int i=0;i<edges.size();i++)\n {\n int start = edges[i][0];\n int end = edges[i][1];\n... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 1 | {
"code": "class Solution {\npublic:\n int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {\n\n vector<vector<int>>adj(n+1);\n vector<int>visited(n+1,0);\n for(int i=0;i<edges.size();i++)\n {\n int start = edges[i][0];\n int end = edges[i][1];\n... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 1 | {
"code": "class Solution {\npublic:\n\n void dfs(int node,int parent,vector<int> adj[],vector<bool>& hasApple,int& ans){\n for(auto &i:adj[node]){\n if(i==parent) continue;\n dfs(i,node,adj,hasApple,ans);\n if(hasApple[i]){\n ans+=2;\n hasApple... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 1 | {
"code": "class Solution {\npublic:\n\n int rec( vector<int> adj[], vector<bool>& hasApple, int parent, int curr) {\n\n int dist = 0;\n int ans=0;\n for(int i:adj[curr]) {\n if ( i != parent) {\n\n int temp = rec(adj, hasApple, curr, i);\n\n dist += te... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 1 | {
"code": "class Solution {\npublic:\n vector<int> vis;\n int dfs(int node, vector<vector<int>> &adj, int cost, vector<bool>& hasApple){\n if(vis[node]) return 0;\n vis[node] = 1;\n int childCost = 0;\n for(auto child : adj[node]){\n childCost += dfs(child, adj, 2, hasAppl... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 1 | {
"code": "class Solution {\npublic:\n\n void dfs(int root,int p,vector<vector<int>>&adj,vector<int> &par){\n for(auto v:adj[root]){\n if(v!=p){\n par[v]=root;\n dfs(v,root,adj,par);\n }\n }\n }\n int minTime(int n, vector<vector<int>>& edges,... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 1 | {
"code": "class Solution {\npublic:\n vector<bool> vis;\n int ans;\n void dfs(int curr,int par,vector<bool>& hasApple, vector<vector<int>> &adj,vector<int> &siz){\n for(auto nb:adj[curr]){\n if(nb==par) continue;\n dfs(nb,curr,hasApple,adj,siz);\n if(hasApple[nb]==tru... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 1 | {
"code": "class Solution {\npublic:\n int dfsrec(int src, vector<vector<int>>& adj, vector<bool>& hasApple, vector<int>& mp, vector<bool>& visited){\n \n visited[src]=true;\n int total = 0; // Initialize total time for the current subtree\n \n for (auto x : adj[src]) {\n if (!vi... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 1 | {
"code": "#define ll long long\n#define pb push_back\nclass Solution {\npublic:\n ll tot = 0;\n bool f(ll node,vector<ll>adj[],vector<ll>& vis, vector<bool>& app){\n bool poke = false;\n for(auto it:adj[node]){\n if(vis[it]==0){\n vis[it]=1;\n if(f(it,adj,... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 1 | {
"code": "class Solution {\npublic:\n int dfs(int node,int parent, vector<vector<int>>& adj,vector<bool>& hasApple){\n int edges = 0;\n for(auto child : adj[node]){\n if(child == parent) continue;\n edges += dfs(child, node, adj, hasApple);\n } \n if(node == 0) re... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 1 | {
"code": "class Solution {\npublic:\n int dfs(vector<vector<int>>&adj,vector<bool>& hasAple,int node,int prnt){\n int ans=0;\n for(auto it:adj[node])\n if(it!=prnt){\n int dist = dfs(adj,hasAple,it,node);\n ans += dist;\n if(dist || hasAple[it]... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 1 | {
"code": "class Solution {\npublic:\n int dfs(int node,int parent,vector<int>adj[],vector<bool>& hasApple){\n int edges = 0;\n for(auto child : adj[node]){\n if(child==parent) continue;\n edges += dfs(child,node,adj,hasApple);\n } \n if(node==0) return edges;\n ... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 1 | {
"code": "class Solution {\npublic:\n int solve(int i, int parent, vector<int> adj[], vector<bool>& hasApple) {\n int ans = 0;\n\n for (auto c : adj[i]) {\n if (c == parent) continue; \n ans += solve(c, i, adj, hasApple);\n }\n\n \n if (i != 0 && (ans == 0 ... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 1 | {
"code": "class Solution {\npublic:\n bool dfs(vector<vector<int>> &adj,int node,int parent,int &cnt, vector<bool>& hasApple){\n bool con=false;\n if(hasApple[node]){\n con=true;\n }\n for(auto it:adj[node]){\n if(it==parent) continue;\n if(dfs(adj,it,n... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 1 | {
"code": "class Solution {\npublic:\nint ans = 0;\nint f(int node , int parent,vector<int> adj[] ,vector<bool>&hasApples){\n int curr = 0;\n\n for(auto it : adj[node]){\n if(it==parent){\n continue;\n }\n\n int child = f(it,node,adj,hasApples);\n if(child>=2 || hasApples[... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 1 | {
"code": "class Solution {\npublic:\nint dfs(int node,int parent,vector<int>adj[],vector<bool>&hasApple){\n int time=0;\n for(auto it :adj[node]){\n if(it==parent)continue;\n int t=dfs(it,node,adj,hasApple);\n if(hasApple[it]==true || t>0){\n time+=(t+2);\n }\n }\n ... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 1 | {
"code": "class Solution {\npublic:\n int dfs(vector<vector<int>> &graph, vector<bool>& hasApple, int par, int root) {\n int res = 0;\n for (auto child : graph[root]) {\n if (child == par) continue;\n int num = dfs(graph, hasApple, root, child);\n if (hasApple[child]... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 1 | {
"code": "class Solution {\npublic:\n int dfs(int node, int parent, vector<vector<int>>& adj, vector<bool>& hasApple){\n int totalTime = 0, childTime = 0;\n for(auto child : adj[node]){\n if(child == parent)\n continue;\n childTime = dfs(child, node, adj, hasAppl... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 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++)\nclass Solution {\n vector<vector<int>>adj;\n int n;\n int solve(int root, int parent,vector<bool>& has){\n int cnt = 0;\n int childcnt = 0;\n for(auto it:adj[root]){\n if(it != parent){\n... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 2 | {
"code": "class Solution {\npublic:\n int dfs(vector<int> adj[],int node, vector<int>&vis,vector<bool>& hasApple){\n int time=0;\n vis[node]=1;\n for(auto it:adj[node]){\n if(vis[it]==-1){\n int timefromchild=dfs(adj,it,vis,hasApple);\n if(timefrom... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 2 | {
"code": "class Solution {\npublic:\nint func(vector<int>&vis,int i,vector<int>adj[],vector<bool>&h){\n int ans=0;\n vis[i]=1;\n\n for(auto it:adj[i]){\n if(!vis[it]){\n ans+=func(vis,it,adj,h);\n }\n }\n if(i==0)return ans;\n if(ans>0||h[i])return ans+2;\n return ans;\n... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 2 | {
"code": "class Solution {\npublic:\n int dfs(vector<int> adj[], int v, vector<int> &vis, vector<bool> &a){\n vis[v] = 1;\n int ans = 0;\n for(auto it : adj[v]){\n if(!vis[it]){\n ans += dfs(adj, it, vis, a);\n }\n }\n if(ans || a[v]){\n ... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 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 {\n vector<vector<int>> adj;\n int n;\n vector<int> dp; // dp vector to store the minimum time at each node\n\n // DFS function to calculate the minimum time\n bool dfs(int root, int pa... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 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 {\n vector<vector<int>> adj;\n int n;\n vector<int> dp; // dp vector to store the minimum time at each node\n\n // DFS function to calculate the minimum time\n void dfs(int root, int pa... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 2 | {
"code": "class Solution {\npublic:\n void bfs(int node, int paren, vector<vector<int>>&adj,vector<int>&par, vector<int>&sd ){\n par[node]=paren;\n for(auto child:adj[node]){\n if(child==paren)continue;\n bfs(child,node,adj,par,sd);\n }\n }\n int minTime(int n, vec... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 2 | {
"code": "class Solution {\nprivate:\n bool setApple(int node, vector<vector<int>> &adj, vector<bool> & hasApple, vector<int> &vis){\n vis[node]=true;\n for(auto it: adj[node]){\n if(!vis[it])\n hasApple[node]=setApple(it, adj, hasApple, vis)||hasApple[node];\n }\n ... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 2 | {
"code": "class Solution {\npublic:\n bool dfs(int u, vector<vector<int>> &adj, vector<bool> &vis, int prev, vector<bool> &hasApple,set<pair<int,int>> &mp){\n vis[u] = true;\n\n bool flg = false;\n\n if(hasApple[u]){\n if(prev != -1)\n {\n mp.insert({prev,... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 2 | {
"code": "class Solution {\npublic:\n int dfs(int i, const vector<vector<int>>& adj, const vector<bool>& hasApple, unordered_set<int> &visited) {\n int totalTime = 0;\n\n for (int child : adj[i]) {\n if (visited.find(child) == visited.end()) {\n visited.insert(child);\n ... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 2 | {
"code": "class Solution {\npublic:\n\n int dfs (int node, int parent, unordered_map<int, vector<int>>& adj, vector<bool>& hasApple) {\n int time = 0 ;\n\n for (auto& it : adj[node]) {\n if (it == parent) continue ;\n\n int fromtheChild = dfs(it, node, adj, hasApple) ;\n\n ... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 2 | {
"code": "class Solution {\npublic:\n int solve(unordered_map<int,vector<int>> &adj,vector<bool> &hasApple,int node,int par){\n int ans=0;\n for(auto nbr:adj[node]){\n if(nbr!=par){\n int temp=solve(adj,hasApple,nbr,node);\n if(temp!=0){\n ... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 2 | {
"code": "class Solution {\npublic:\n int dfs(unordered_map<int,vector<int>>& adj,vector<bool>& hasApple, int current , int parent)\n { int time =0;\n for(auto& child: adj[current]){\n if(child == parent) continue;\n \n int time_from_child= dfs(adj, hasApple, child, ... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 2 | {
"code": "class Solution {\npublic:\n bool helper(map<int,int>&h,int i,int par,vector<bool>& hasApple,vector<vector<int>>&adj){\n bool c=0;\n for(int j:adj[i]){\n if(j==par){\n continue;\n }\n c|=helper(h,j,i,hasApple,adj);\n if(hasApple[j])... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 2 | {
"code": "class Solution {\npublic:\n \n int DFS(unordered_map<int, vector<int>> &adj, int curr, int parent, vector<bool>& hasApple) {\n int time = 0;\n \n for(int &child : adj[curr]) {\n if(child == parent)\n continue;\n \n int time_from_bac... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 2 | {
"code": "class Solution {\npublic:\n//TC:- O(V+E)\n int DFS( unordered_map<int,vector<int>>&adj,int curr,int parent,vector<bool>&hasApple){\n int time=0;\n for(int child:adj[curr]){\n if(child==parent)\n continue;\n int time_from_my_child=DFS(adj,child,curr,hasApple... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 2 | {
"code": "\nclass Solution {\npublic:\n \n int DFS(unordered_map<int, vector<int>> &adj, int curr, int parent, vector<bool>& hasApple) {\n int time = 0;\n \n for(int &child : adj[curr]) {\n if(child == parent)\n continue;\n \n int time_from_b... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 2 | {
"code": "\nclass Solution {\npublic:\n \n int DFS(unordered_map<int, vector<int>> &adj, int curr, int parent, vector<bool>& hasApple) {\n int time = 0;\n \n for(int &child : adj[curr]) {\n if(child == parent)\n continue;\n \n int time_from_b... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\npublic:\n int dfs(unordered_map<int, vector<int>>& adj, int node, vector<bool>& hasApple, vector<int>& vis) {\n // cout << \"Exploring current node \" << node << endl;\n \n vis[node] = true;\n int ans = 0;\n\n for (auto nbr : adj[node]) {\n ... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\npublic:\n int dfs(unordered_map<int, vector<int>>& adj, int node, vector<bool>& hasApple, vector<int>& vis) {\n cout << \"Exploring current node \" << node << endl;\n \n vis[node] = true;\n int ans = 0;\n\n for (auto nbr : adj[node]) {\n if... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\npublic:\n int counter = 0;\n\n // DFS function that returns true if the node or its subtree contains an apple\n bool dfs(int node, vector<int> a[], vector<int>& vis, set<int>& seti) {\n vis[node] = 1;\n bool found = false; // To track if any apple is found in this s... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\npublic:\n unordered_set<int> visited;\n pair<int,bool> process(vector<vector<int>> &adj, vector<bool>& hasApple, int index)\n {\n bool hasIt=false;\n if(hasApple[index])\n hasIt=true;\n int res=0;\n for(int x:adj[index])\n {\n ... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\npublic:\n unordered_set<int> visited;\n pair<int,bool> process(vector<vector<int>> &adj, vector<bool>& hasApple, int index)\n {\n bool hasIt=false;\n if(hasApple[index])\n hasIt=true;\n int res=0;\n for(int x:adj[index])\n {\n ... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\nprivate:\n unordered_map<int, vector<int>> adjList;\n vector<bool> hasApple;\n vector<bool> visited;\n\n int count(int node) {\n visited[node] = true;\n int result = 0;\n for (int neighbor : adjList[node]) {\n if (visited[neighbor])\n ... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\npublic:\nint dfs(map<int,vector<int>>&mp,int node,vector<bool>&hasApple,vector<int>&visited)\n{\n int time=0;\n visited[node]=1;\n for(auto it:mp[node])\n {\n if(visited[it]==0)\n {\n int st=dfs(mp,it,hasApple,visited);\n if(st!=0)\n ... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\npublic:\n map<int,vector<int>> adj;\n vector<bool> vis;\n int dfs(int source, vector<bool>& hasApple ,int now){\n vis[source]=1;\n int c=0;\n for(int pk: adj[source]){\n if(vis[pk])continue;\n c += dfs(pk, hasApple, 2);\n }\n ... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\npublic:\n int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {\n // Create an adjacency list from the given edges\n map<int, vector<int>> adjlist;\n for(auto& edge : edges) {\n adjlist[edge[0]].push_back(edge[1]);\n adjlist... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\npublic:\n int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {\n int ans = 0;\n vector<bool> visited(n);\n vector<int> parents(n);\n vector<vector<int>> adjacent(n, vector<int>{});\n unordered_set<int> countedVertex;\n queue... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\npublic:\n int bfs(int src,vector<int>adj[],int cost,vector<bool>&hasApple,unordered_map<int,bool>&visited){\n if(visited[src]) return 0;\n visited[src]=true;\n int cnt=0;\n for(int ind:adj[src]){\n cnt+=bfs(ind,adj,2,hasApple,visited);\n }\... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\npublic:\n void dfs(int node,int par,vector<int> adj[],vector<pair<int,int>>& isVisited,set<pair<int,int>>& st,vector<bool>& hasApple,vector<int>& visit){\n isVisited.push_back({node,par});\n visit[node]=1;\n if(hasApple[node]){\n for(auto i:isVisited... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\npublic:\n vector<int> G[100005];\n unordered_map<int, bool> vis;\n vector<bool> apple;\n int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {\n for (auto e : edges) {\n int a = e[0], b = e[1];\n G[a].push_back(b);\n G... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\npublic:\n int dfs(unordered_map<int, vector<int>>&mp, vector<bool>& hasApple,\n int node, int parent) {\n int time = 0;\n for (auto child : mp[node]) {\n if (child == parent)\n continue;\n int time_from_child = dfs(mp, hasAp... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\n // bool modify(int i,int parent,unordered_map<int,vector<int>>&adj,vector<bool>&hasApple)\n // {\n // bool ans=false;\n\n // for(auto x:adj[i])\n // {\n // if(x!=parent)\n // ans=ans | modify(x,i,adj,hasApple);\n // }\n\n // ... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\n bool modify(int i,int parent,unordered_map<int,vector<int>>&adj,vector<bool>&hasApple)\n {\n bool ans=false;\n\n for(auto x:adj[i])\n {\n if(x!=parent)\n ans=ans | modify(x,i,adj,hasApple);\n }\n\n return hasApple[i]=hasA... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\npublic:\n unordered_map<int,vector<int>>m;\n int dfs(int node,int parent, vector<bool>& found){\n int ans=0;\n for(auto i:m[node]){\n if(i!=parent){\n int temp=dfs(i,node,found);\n if(found[i]){\n ans+=temp+2;... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\npublic:\n int dfs(unordered_map<int,vector<int>>&m,vector<bool>& hasApple,int index,int time,vector<int>&visited){\n if(visited[index]==1){\n return 0;\n }\n visited[index]=true;\n int cost=0;\n for(int u:m[index]){\n \n c... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\nprivate:\n int dfs(unordered_map<int,vector<int>>&graph,int u,vector<int>&visited,vector<bool>&hasApple){\n visited[u]=1;\n int totalcost=0;\n for(int v:graph[u]){\n if(!visited[v]){\n int costfromchild=dfs(graph,v,visited,hasApple);\n ... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\npublic:\n int dfs(unordered_map<int, vector<int>> &adj, int node, vector<int> &visited, vector<bool>& has_apple, int par) {\n\n // visited[node] = 1;\n\n int ans = 0;\n for(int x: adj[node]) {\n if(x == par) continue;\n\n int temp = dfs(adj, x... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\npublic:\n bool dfs(unordered_map<int, list<int>>& adj, int node, vector<bool>& hasApple, vector<bool>& visited, int& time) {\n visited[node] = true;\n bool hasAppleInSubtree = hasApple[node];\n\n for (int child : adj[node]) {\n if (!visited[child]) {\n ... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\nunordered_map<int, vector<int>> graph;\nvector<bool> visited;\n\npair<int, bool> dfs(int node, vector<bool>& hasApple) {\n int time = 0;\n bool inPath = false;\n visited[node] = true;\n if (hasApple[node]) {\n inPath = true;\n }\n for (int child : graph[node]) {\n... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\nunordered_map<int, vector<int>> graph;\nvector<bool> visited;\n\npair<int, bool> dfs(int node, vector<bool>& hasApple) {\n int time = 0;\n bool inPath = false;\n visited[node] = true;\n if (hasApple[node]) {\n inPath = true;\n }\n for (int child : graph[node]) {\n... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\n int solve(unordered_map<int, vector<int>> &m, vector<bool> &hasApple, int u, vector<bool> &vis) {\n if (vis[u]) {\n return -1;\n }\n\n vis[u] = true;\n vector<int> child = m[u];\n\n int ans = 0;\n for (int i = 0; i < child.size(); i... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\npublic:\n void dfs(unordered_map<int,list<int>>&adj,int node,vector<bool>&vis,vector<int>&need,vector<bool>&hasApple){\n vis[node]=true;\n \n for(auto i:adj[node]){\n if(!vis[i]){\n dfs(adj,i,vis,need,hasApple);\n need[node]+=need[i];\n ... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\npublic:\n pair<int, int> solve(int index, map<int, vector<int>>& mp, int n , vector<bool>& hasApple, vector<bool>& vis) {\n //cout<<index<<endl;\n vis[index] = true;\n if (mp[index].size() == 0) {\n return {hasApple[index], 0};\n }\n int an... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\npublic:\n pair<int, int> solve(int index, map<int, vector<int>>& mp, int n , vector<bool>& hasApple, vector<bool>& vis) {\n cout<<index<<endl;\n vis[index] = true;\n if (mp[index].size() == 0) {\n return {hasApple[index], 0};\n }\n int ans ... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\npublic:\n vector <int> sub;\n map <int,vector<int>> adj;\n void dfs(int node , int p , vector <bool> &hasApple) {\n if(hasApple[node]) sub[node] = 2;\n if(adj[node].size() == 1 && node != 0) {\n // if(hasApple[node]) sub[node] = 2;\n return;\n ... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\npublic:\n void dfs(unordered_map<int,list<int>>&adj,int node,vector<bool>&vis,vector<int>&need,vector<bool>&hasApple){\n vis[node]=true;\n for(auto i:adj[node]){\n if(!vis[i]){\n dfs(adj,i,vis,need,hasApple);\n need[node]+=need[i];\n }\n ... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "/* \nreturn the minimum number of edges you need to cross to reach ever apple in the tree.\n*/\n\nclass Solution {\npublic:\n int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {\n unordered_map<int, vector<int>> adjList;\n unordered_set<int> visited;\n for(int i... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\npublic:\n int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {\n unordered_map<int, vector<int>> adjList;\n for(int i=0; i<edges.size(); i++) {\n adjList[edges[i][0]].push_back(edges[i][1]);\n adjList[edges[i][1]].push_back(edges[... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\npublic:\n int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {\n for(int i = 0; i < edges.size(); ++i){\n tree[edges[i][0]].push_back(edges[i][1]);\n tree[edges[i][1]].push_back(edges[i][0]);\n }\n\n //for(int i = 0; i < ha... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "/* \nreturn the minimum number of edges you need to cross to reach ever apple in the tree.\n*/\n\nclass Solution {\npublic:\n int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {\n unordered_map<int, vector<int>> adjList;\n unordered_set<int> visited;\n for(int i... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\npublic:\n unordered_map<int, vector<int>> adj;\n set<int> vis;\n\n int dfs(int node, vector<bool>& hasApple)\n {\n int sum = 0;\n for(auto v:adj[node])\n {\n if(vis.find(v)!=vis.end())\n {\n continue;\n }\n ... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\n int solve(int node, unordered_map<int, list<int>>&adj, vector<bool>& hasApple, int parent){\n int ans = 0;\n for(auto i:adj[node]){\n if(i==parent){\n continue;\n }\n int child = solve(i,adj,hasApple, node);\n if... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\npublic:\n int dfs(unordered_map<int, list<int>>&adj, int node, int parent, vector<bool>& hasApple){\n int time = 0;\n\n for(auto child: adj[node]){\n if(child == parent){\n continue;\n }\n int time_from_child = dfs(adj, chil... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\npublic:\n int dfs(unordered_map<int,list<int>> &adj, vector<bool>& hasApple,\n int parent,int i){\n int time = 0;\n for(auto n : adj[i]){\n if(parent!=n){\n int child = dfs(adj,hasApple,i,n);\n if(child > 0 || hasApple[n])\n ... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\npublic:\n void func(int node,vector<bool> &visited, vector<int>& temp, unordered_map<int, vector<int>>& mp,\n vector<vector<int>>& paths, vector<bool>& hasApple) {\n\n temp.push_back(node);\n visited[node] = true;\n if (hasApple[node] == true)\n ... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\npublic:\n int dfs(unordered_map<int,list<int>> &adj, vector<bool>& hasApple,\n vector<bool> & visited,int i){\n int time = 0;\n for(auto n : adj[i]){\n if(!visited[n]){\n visited[n] = true;\n int child = dfs(adj,hasApple,visited... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\npublic:\n unordered_map<int, vector<int>> edgeMap;\n\n int collectApples(int idx, int parent, vector<bool>& hasApple) {\n auto edges = edgeMap[idx];\n int branchCost = 0;\n for(auto edge: edges) {\n if (edge == parent)\n continue;\n ... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\npublic:\n unordered_map<int, vector<int>> edgeMap;\n\n int collectApples(int idx, int parent, vector<bool>& hasApple) {\n auto edges = edgeMap[idx];\n int branchCost = 0;\n for(auto edge: edges) {\n if (edge == parent)\n continue;\n ... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\npublic:\n int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {\n map<int, vector<int>> g;\n for(auto e:edges)\n {\n g[e[0]].push_back(e[1]);\n g[e[1]].push_back(e[0]);\n }\n vector<bool> visited(n, false);\n ... |
1,554 | <p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro... | 3 | {
"code": "class Solution {\npublic:\n int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {\n for(int i = 0; i < edges.size(); ++i){\n tree[edges[i][0]].push_back(edges[i][1]);\n tree[edges[i][1]].push_back(edges[i][0]);\n }\n\n for(int i = 0; i < hasA... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.