id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A...
3
{ "code": "class Solution {\npublic:\nlong long countSubarrays(vector<int>& a, int k) {\n int n=a.size();\n vector<long long> maxIndex;\n int maxIdx=0;\n for (int i=0; i<n; i++) {\n if (a[maxIdx]<a[i]) maxIdx=i;\n }\n for (int i=0; i<n; i++) {\n if (a[i]==a[maxIdx]) maxIndex.pus...
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A...
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n int n = nums.size();\n int maxx = *max_element(nums.begin(), nums.end());\n vector<int> prefixes, suffixes;\n int count = 0, total = 0;\n for (int i = 0; i < n; i++) {\n count...
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A...
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n int maxVal = *std::max_element(nums.begin(), nums.end());\n int len = nums.size();\n vector<long long> prefix(len);\n prefix[0] = nums[0] == maxVal;\n for (int i = 1; i < len; i++) {\n ...
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A...
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n \n int n=nums.size(), i=0, mx=0, f=0;\n long long ans=0;\n vector<long long> dp(n, 0);\n\n for(int j=0; j<nums.size(); j++)\n {\n mx=max(mx, nums[j]);\n }\n ...
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A...
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n \n int n=nums.size(), i=0, mx=0, f=0;\n long long ans=0;\n vector<long long> dp(n, 0);\n\n for(int j=0; j<nums.size(); j++)\n {\n mx=max(mx, nums[j]);\n }\n ...
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A...
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n \n int n = nums.size();\n\n vector<long long> sumArr(n, 0);\n\n int maxNum = nums[0];\n\n for(int i = 1 ; i < n ; i++){\n\n maxNum = max(maxNum, nums[i]);\n \n ...
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A...
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n \n int n = nums.size();\n\n vector<long long> sumArr(n, 0);\n\n int maxNum = nums[0];\n\n for(int i = 1 ; i < n ; i++){\n\n maxNum = max(maxNum, nums[i]);\n \n ...
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A...
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n int target=*max_element(nums.begin(),nums.end()),n=nums.size();\n vector<int> p(n,0);\n // if(n==1)return 1ll;\n if(nums[0]==target)p[0]=1;\n long long ans=0;\n vector<int> mp(n+1...
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A...
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n vector<long long> v;\n v.push_back(0);\n long long n = nums.size();\n int mx = nums[0];\n for(int i=0;i<n;i++) {\n mx = max(mx, nums[i]);\n }\n\n for(int i=1;i<=...
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A...
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n \n vector<long long> v;\n v.push_back(0);\n long long n = nums.size();\n int mx = nums[0];\n for(int i=0;i<n;i++) {\n mx = max(mx, nums[i]);\n }\n\n for(i...
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A...
3
{ "code": "//////////// Sliding Window solution ///////////\n\n// class Solution {\n// public:\n// long long countSubarrays(vector<int>& nums, int k) {\n// int maxElement = *max_element(nums.begin(), nums.end());\n// int i = 0, j = 0, n = nums.size(), maxCount = 0;\n// long long ans = 0;\n...
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A...
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n int n = nums.size();\n int maxi = *max_element(nums.begin(),nums.end());\n unordered_map<int,int> mp;\n long long ans =0;\n int i=0,j=0;\n while(j<n){\n mp[nums[j]]++;...
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A...
3
{ "code": "#include <iostream>\n#include <vector>\n#include <unordered_map>\n\nusing namespace std;\n\nclass Solution {\npublic:\n long countSubarrays(vector<int>& nums, int k) {\n int max = 0;\n for(int i: nums)\n max = std::max(i, max);\n vector<int> cnt(nums.size());\n int...
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A...
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n\n int maxi=0;\n for(int i=0;i<nums.size();i++) maxi=max(maxi,nums[i]);\n vector<int>p(nums.size(),0);\n for(int i=0;i<nums.size();i++){\n if(nums[i]==maxi) p[i]++;\n if(i!...
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A...
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n\n int maxi=0;\n for(int i=0;i<nums.size();i++) maxi=max(maxi,nums[i]);\n vector<int>p(nums.size(),0);\n for(int i=0;i<nums.size();i++){\n if(nums[i]==maxi) p[i]++;\n if(i!...
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A...
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n long long sub_num = 0;\n int n = nums.size();\n int h = 0; int max_val = 0;\n unordered_map<int, int> counts;\n for (int i = 0; i < n; i++) {\n max_val = std::max(max_val, num...
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A...
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n int n=nums.size();\n int maxElement=0;\n\n for(auto it : nums){\n maxElement=max(it,maxElement);\n }\n int first=0,last=0;\n long long ans=0;\n unordered_map<int...
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A...
3
{ "code": "class Solution {\npublic:\n\n int findMax(vector<int>& nums)\n {\n int maxEle = INT_MIN;\n for (int i=0; i<nums.size(); i++) {\n maxEle = max(maxEle, nums[i]);\n }\n return maxEle;\n }\n\n long long countSubarrays(vector<int>& nums, int k) {\n long ...
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A...
3
{ "code": "class Solution {\npublic:\n long long int countSubs(vector<int>& nums, int k, int mx) {\n long long int count = 0;\n int left = 0;\n unordered_map<int, int> freq;\n\n for (int right = 0; right < nums.size(); ++right) {\n \n freq[nums[right]]++;\n ...
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A...
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n long long i=0,j=0,ans=0;\n unordered_map<int,int> umap;\n int max_element;\n for(auto x:nums){\n max_element=max(max_element,x);\n }\n while(j<nums.size()){\n ...
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A...
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& arr, int k)\n {\n int mx = INT_MIN;\n for(int i=0; i<arr.size(); i++)\n mx = max(mx, arr[i]);\n\n long long start=0, end=0, ans=0;\n unordered_map<int, int> m;\n\n while(end<arr.size())\n ...
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A...
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& A, int k) {\n long long ans=0;\n int l=0;\n int mx = *max_element(A.begin(), A.end());\n unordered_map<int,int> m;\n int n=A.size();\n for(int i=0;i<n;i++){\n m[A[i]]++;\n wh...
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A...
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n int maxiEle=*max_element(nums.begin(),nums.end());\n long long ans=0;\n int i=0,j=0,cnt=0;\n int n=nums.size();\n unordered_map<int,int>mp;\n while(j<n){\n mp[nums[j]]++;...
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A...
3
{ "code": "static const bool Booster = [](){\n #pragma GCC optimize(\"OFast\")\n std::ios_base::sync_with_stdio(0);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return true;\n}();\n\nclass Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n int n = nums.size()...
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A...
3
{ "code": "class Solution {\npublic:\n Solution(){\n ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n }\n long long countSubarrays(vector<int>& nums, int k) {\n int n=nums.size(),maxElement=0;\n for(auto it:nums){\n maxElement=max(it,maxElement);\n }\n int fir...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
0
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = roads.size() + 1;\n vector<int> deg(n, 0), gragh(n, 0);\n for (auto& road : roads) {\n int node = road[0], vec = road[1];\n gragh[node] ^= vec;\n ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
0
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = roads.size() + 1;\n vector<int> deg(n, 0), gragh(n, 0);\n for (auto& road : roads) {\n int node = road[0], vec = road[1];\n gragh[node] ^= vec;\n ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
0
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n=roads.size()+1;\n vector<int>graph[n];\n vector<int>indegree(n);\n vector<int> people(n+1,1);\n people[0]=0;\n for(auto&x:roads)\n {\n graph[...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
0
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n=roads.size()+1;\n vector<int>indegree(n);\n vector<int>adj[n];\n for(int i=0;i<roads.size();i++)\n {\n adj[roads[i][0]].push_back(roads[i][1]);\n ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
0
{ "code": "class Solution {\n long long ans = 0;\n int s;\n\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n=roads.size()+1;\n vector<int> graph[n];\n vector<int> deg(n, 0);\n for (vector<int>& r : roads) {\n graph[r[0]].push_back(r[...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
0
{ "code": "class Solution {\npublic:\n vector<int> adj[100005];\n bool visit[100005];\n long long s, ans = 0;\n int dfs(int cur){\n if(visit[cur]){\n return 0;\n }\n visit[cur]=true;\n int p = 0;\n for(int x : adj[cur]){\n p+=dfs(x);\n }\n ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
0
{ "code": "class Solution {\npublic:\n int k;\n long long ans=0;\n long long dfs(vector<int>adj[],int node,int par){\n long long count=1; //Count==>Total nodes in subtree with node as parent\n for(int child:adj[node]){\n if(child!=par){\n count+=dfs(adj,child,node);\n ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
0
{ "code": "class Solution {\npublic:\n long long ans = 0;\n int seat;\n int dfs(int node , int prev , vector<int>adj[]){\n int people = 1;\n for(auto it : adj[node]){\n if(prev == it){\n continue;\n }\n people+= dfs(it, node , adj);\n }\n ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
0
{ "code": "class Solution {\npublic:\n long long ans=0;\n long long solve(int i,int j,int s,vector<int>a[])\n { \n long long x=0;\n for(int c:a[i])\n {\n if(c==j)continue;\n x+=solve(c,i,s,a);\n }\n x+=1;\n if(i!=0){\n ans+=x/s;\n ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
0
{ "code": "class Solution {\npublic:\n\nlong long ans = 0;\n\n int dfs(int ver,int parent, vector<int> adj[], int seats){\n int people = 1;\n for(auto it: adj[ver]) if(it != parent) people += dfs(it,ver, adj,seats);\n\n if(ver > 0) ans += ceil( (double) people / (double) seats );\n retu...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
0
{ "code": "class Solution {\npublic:\n int dfs(int u, int par, vector<int> adj[], long long &ans, int seats) {\n int passengers = 0;\n for(auto &child : adj[u]) {\n if (child!=par) {\n int p = dfs(child, u, adj, ans, seats);\n passengers += p;\n ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
0
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n vector<int> adjList[roads.size()+1];\n for (const auto& r : roads) {\n adjList[r[0]].push_back(r[1]);\n adjList[r[1]].push_back(r[0]);\n }\n auto [_, FuelCos...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
0
{ "code": "class Solution {\npublic:\n int dfs(int u, int prev, vector<int> g[], int seats, long long &ans) {\n long long count = 1;\n for(auto v:g[u]) {\n if(v!=prev) {\n count+=dfs(v,u,g,seats, ans);\n }\n \n }\n if(u!=0) {\n ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
0
{ "code": "class Solution {\npublic:\n void dfs(int s, vector<int> adj[], bool vis[], vector<int> &st) {\n vis[s] = 1;\n for(auto &a : adj[s]) {\n if(!vis[a])\n dfs(a, adj, vis, st);\n }\n st.push_back(s);\n }\n int calculateFule(int s, vector<int> adj[], boo...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
0
{ "code": "class Solution {\nusing ll = long long; \nprivate: ll totalFuel= 0;\n int dfs(int curr, int parent, vector<ll> *adj, int seats){\n int count = 1;\n for(auto &u: adj[curr]){\n if(u!=parent){\n count += dfs(u, curr, adj, seats);\n }\n }\n ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
0
{ "code": "class Solution {\nprivate:\n int dfs(vector<int> adjList[], int src, int par, long long& total, int seats) {\n int people = 0;\n for (int nei : adjList[src]) {\n if (nei == par) continue;\n int peopleFromNeiCity = dfs(adjList, nei, src, total, seats);\n peo...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
0
{ "code": "class Solution {\npublic:\n long long rec(int src ,vector<int> adj[],vector<int> &visited,int seats,long long &ans){\n int passenger_count=0;\n visited[src]=1; \n\n for(auto x: adj[src]){\n if(!visited[x]){\n int pas=rec(x,adj,visited,seats,ans);\n ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
0
{ "code": "class Solution {\npublic:\n long long fuel = 0;\n long long dfs(int i,vector<int>&visited,vector<int>adj[],int size,int seats){\n visited[i] = 1;\n size++;\n for(auto it:adj[i]){\n if(!visited[it]){\n size += dfs(it,visited,adj,0,seats);\n }\n...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
0
{ "code": "class Solution {\npublic:\nlong long ans=0;\nint dfs(int node, vector<int> adj[], int seat, vector<int> &vis){\n vis[node]=1;\n int child=1;\n for(auto it:adj[node]){\n if(vis[it]==0){\n int her= dfs(it,adj,seat,vis); \n int car=her/seat;\n if(her%seat!=0) ca...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
0
{ "code": "class Solution {\npublic:\n long long ans=0;\n long long dfs(vector<int>adj[],int node,int seats,vector<int>&vis){\n vis[node]=1;\n long long cnt=1;\n for(auto it:adj[node]){\n if(vis[it]==0)\n cnt+=dfs(adj,it,seats,vis);\n }\n long long x=cnt/seats;\n ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
0
{ "code": "class Solution {\npublic:\n long long ans = 0; int s;\n vector<int> graph[300005];\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n s = seats;\n for(vector<int>& r : roads){\n graph[r[0]].push_back(r[1]);\n graph[r[1]].push_back(r[0]);\n ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
0
{ "code": "class Solution {\npublic:\n long long ans = 0; int s;\n vector<int> graph[300005]; \n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n s = seats;\n for (vector<int>& r: roads) {\n graph[r[0]].push_back(r[1]);\n graph[r[1]].push_back(r[0]);\n ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
0
{ "code": "class Solution {\npublic:\n long long ans = 0; int s;\n vector<int> graph[300005];\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n s = seats;\n for(vector<int>& r : roads){\n graph[r[0]].push_back(r[1]);\n graph[r[1]].push_back(r[0]);\n ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
0
{ "code": "class Solution {\npublic:\n int dfs(int node,int vis[],vector<int> adj[],int ct[],int seats)\n {\n vis[node]=1;\n int ans = 0;\n for(auto child: adj[node])\n {\n if(!vis[child])\n {\n int p = dfs(child,vis,adj,ct,seats);\n ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
0
{ "code": "class Solution {\npublic:\n long long bfs(int n, vector<vector<int>>& adj, vector<int>& degree, int& seats) \n {\n queue<int> q;\n for (int i = 1; i < n; i++) \n {\n if (degree[i] == 1) \n {\n q.push(i);\n }\n }\n\n ve...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
0
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = roads.size() + 1;\n vector<vector<int>> graph(n);\n vector<int> degree(n, 0);\n vector<int> people(n, 1); // Mỗi thành phố ban đầu có 1 người\n vector<bool> visite...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
0
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = roads.size() + 1;\n vector<int> people(n, 1), degree(n, 0);\n vector<vector<int>> graph(n);\n people[0] = 0;\n\n for (auto& road : roads) {\n graph[road[...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
0
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = roads.size();\n vector<vector<int>> adj(n + 1); // adjacency list\n vector<int> degree(n + 1, 0); // out-degree count\n vector<bool> vis(n + 1, false); // visited arr...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
0
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n long long minCost=0,n=roads.size()+1;\n vector<int>parent(n,-1);\n vector<vector<int>>adj(n);\n for(int i=0;i<n-1;i++){\n adj[roads[i][0]].push_back(roads[i][1]);\n ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
0
{ "code": "class Solution {\npublic:\n long long ans = 0;\n \n void dfs(int v,int par,vector<int> g[],vector<long long>& child,int seats){\n for(auto c:g[v]){\n if(c == par) continue;\n dfs(c,v,g,child,seats);\n //this is the number of children from a branch that is\n ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
0
{ "code": "class Solution {\npublic:\n pair<long long,int> dfs(vector<vector<int>>& tree,int seats,int parent,int node) {\n int subNodes = 1;\n long long fuel = 0;\n for (int i : tree[node]) {\n if (i != parent) {\n pair<long long,int> temp = dfs(tree,seats,node,i);\n...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
0
{ "code": "class Solution {\nvector<vector<int>>g;\n\nlong long dfs(int node, int parent, int seats, long long &ans){\n\n long long contri = 1;\n for(auto &child : g[node]){\n if(child != parent){\n long long dis = dfs(child, node, seats, ans); \n contri += dis;\n ans += ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
1
{ "code": "class Solution {\npublic:\n#if 1\n //有點像車行發車的感覺,由乘客數量決定每個站點的發車數量,最後再由每站的發車數量來得知總體油量消耗,\n //return值表示這一站要搭車的乘客數量\n long long answer = 0;\n int DFS(int index, vector<vector<int>>& adj, vector<unsigned char>& visited, int seats) {\n visited[index] = 1;\n //if(visited[index] == true)\...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
1
{ "code": "class Solution {\npublic:\n#if 1\n //有點像車行發車的感覺,由乘客數量決定每個站點的發車數量,最後再由每站的發車數量來得知總體油量消耗,\n //return值表示這一站要搭車的乘客數量\n long long answer = 0;\n int DFS(int index, vector<vector<int>>& adj, vector<unsigned char>& visited, int seats) {\n visited[index] = 1;\n //if(visited[index] == true)\...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
1
{ "code": "class Solution {\npublic:\n int dfs(int node, vector<vector<int>>& roads, vector<vector<int>>& adj,\n vector<int>& vis, long long& ans, int seats) {\n vis[node] = 1;\n int cnt = 1;\n\n for (int adjNode : adj[node]) {\n if (!vis[adjNode])\n cnt +=...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
1
{ "code": "class Solution {\npublic:\n int n;\n vector<vector<int>> g;\n vector<int> vis;\n vector<int> subsize;\n\n long long dfs(int node, int seats, long long &fuel) {\n vis[node] = 1;\n int representatives = 1; // Each city has one representative\n\n // Traverse all neighbors\...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
1
{ "code": "class Solution {\npublic:\n\n long long int dfs(int index, vector<vector<int>>& graph, int seats, long long int& ans, vector<int>& visited){\n long long int count=1;\n visited[index]=1;\n for(auto i:graph[index]){\n if(visited[i]==0){\n long long int a=dfs(...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
1
{ "code": "class Solution {\nprivate:\n std::vector<std::vector<int> > RebuildMap(std::vector<std::vector<int> > &roads) {\n std::vector<std::vector<int> > country(roads.size() + 1);\n for (auto &road : roads) {\n country[road[0]].push_back(road[1]);\n country[road[1]].push_back(road[0]);\n }\n ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
1
{ "code": "#include <iostream>\n#include <vector>\n#include <cmath>\n\nusing namespace std;\n\nclass Solution {\npublic:\n // Function to calculate the minimum fuel cost\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = roads.size() + 1; // Number of nodes (cities)\n ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
1
{ "code": "class Solution {\npublic:\n\n void dfs(int node , vector<vector<int>>&adj , vector<int>&vis , vector<long long int> &people)\n {\n vis[node]=1;\n\n \n\n \n people[node]=1;\n long long int child =0;\n\n for(auto it: adj[node])\n {\n if(!vis[i...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
1
{ "code": "class Solution {\npublic:\n\n void dfs(int v, int par, vector<vector<int>> &adj, vector<int> &size, int seats, vector<long long> &ans){\n for(int &u: adj[v]){\n if(u==par){\n continue;\n }\n dfs(u,v,adj,size,seats,ans);\n ans[v]+=(size[u]...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
1
{ "code": "class Solution {\npublic:\n \n int dfs(int cur,int parent,vector<int> adjaList[],int& seats,long long& result){\n int representatives = 1;\n\n for(auto children:adjaList[cur]){\n if(children == parent)continue;\n\n int path = dfs(children,cur,adjaList,seats,result)...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
1
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n \n int n = roads.size() + 1;\n long long res=0;\n vector<int> graph[n];\n for(auto edge:roads){\n int u=edge[0],v=edge[1];\n graph[u].push_back(v);\n ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
1
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n \n int n = roads.size() + 1;\n long long res=0;\n vector<int> graph[n];\n for(auto edge:roads){\n int u=edge[0],v=edge[1];\n graph[u].push_back(v);\n ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
1
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n=roads.size()+1;\n long long ans=0;\n vector<vector<int>> V(n,vector<int>());\n vector<int> deg(n,0);\n vector<int> pep(n,1);\n for(vector<int> v:roads){\n ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
1
{ "code": "class Solution {\npublic:\n\tint dis[100100], sz[100100]; vector<int> adj[100100];\n long long ans = 0;\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n \tfor (auto &e : roads) {\n \t\tadj[e[0]].push_back(e[1]);\n \t\tadj[e[1]].push_back(e[0]);\n \t} \n f...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
2
{ "code": "class Solution {\npublic:\n pair<long long,long long> dfs(long long x,long long p,vector<vector<long long>> &adj,long long& ans,long long h,long long s){\n if((adj[x].size()==1 && adj[x][0]==p)){\n ans += h;\n return {1,s};\n }\n long long cap = 0;\n lon...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
2
{ "code": "class Solution {\npublic:\n void dfs(vector<vector<int>> &adj,vector<long long> &child,int seats,int par,int curr,long long &ans){\n for(auto it : adj[curr]){\n if(it == par) continue;\n dfs(adj,child,seats,curr,it,ans);\n int bchild = 1 + child[it];\n\n ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
2
{ "code": "class Solution {\npublic:\n\n long long dfs(int node, long long &ans, vector<bool> &visited, int seats, vector<vector<int>> &adj) {\n\n visited[node] = true;\n long long cnt = 1;\n for (auto child : adj[node]) {\n if (visited[child] == false)\n cnt += dfs(c...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
2
{ "code": "class Solution {\npublic:\n \n long long ans = 0;\n \n int dfs(vector<vector<int>>& adj, int u, int seats, vector<bool>& vis)\n {\n // mark the current node as visited \n vis[u] = true; \n // count will keep track of no. of nodes (descendent + including) ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
2
{ "code": "class Solution {\npublic:\n //有點像車行發車的感覺,由乘客數量決定每個站點的發車數量,最後再由每站的發車數量來得知總體油量消耗,\n //return值表示乘客數量\n long long answer = 0;\n vector<vector<int>> adj;\n long long DFS(vector<bool>& visited, int index, int seats) {\n visited[index] = true;\n //if(visited[index] == true)\n /...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
2
{ "code": "class Solution {\npublic:\n //有點像車行發車的感覺,由乘客數量決定每個站點的發車數量,最後再由每站的發車數量來得知總體油量消耗,\n //return值表示乘客數量\n long long answer = 0;\n vector<vector<int>> adj;\n vector<bool> visited;\n long long DFS(int index, int seats) {\n visited[index] = true;\n //if(visited[index] == true)\n ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
2
{ "code": "#define ll long long \nclass Solution {\npublic:\n ll res = 0;\n vector<ll> cars;\n vector<ll> ssiz;\n int st;\n vector<vector<int>> adj;\n void dfs(int nod,int par){\n ssiz[nod] = 1;\n cars[nod] = 1;\n for(auto i : adj[nod]){\n if(i!=par){\n ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
2
{ "code": "void dfs(int curr,vector<int>& child,vector<vector<int>>& g,vector<bool>& vis){\n vis[curr] = 1;\n for(auto & it : g[curr]){\n if(vis[it]) continue;\n dfs(it,child,g,vis);\n child[curr] += child[it];\n }\n}\nclass Solution {\npublic:\n long long minimumFuelCost(vector<vecto...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
2
{ "code": "class Solution {\npublic:\n vector<int> endNodes;\n\n int dfs(int node, vector<int> g[], vector<int>& vis, int& seats,\n long long& p) {\n vis[node] = 1;\n int flag = 0, ans = 0;\n for (auto child : g[node]) {\n if (vis[child])\n continue;\n\n...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
2
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n auto [cost, num] = dfs(0, getAdj2(roads), seats);\n return cost;\n }\n\n vector<vector<int>> getAdj(const vector<vector<int>>& roads) {\n unordered_map<int, vector<int>> edges;\n ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
2
{ "code": "class Solution {\npublic:\n void countChild(int i, vector<int> adj[], vector<int> &child){\n child[i] = 1;\n for(auto it:adj[i]){\n if(!child[it]){\n countChild(it, adj, child);\n child[i] += child[it];\n }\n }\n }\n\n long l...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
2
{ "code": "class Solution {\n typedef long long ll;\n struct triple {\n ll first, second, third;\n };\n triple dfs(int p, int u, int seats, vector<int> graph[]) {\n ll dist = 0, cars = u != 0, rem_seats = u == 0 ? 0 : seats - 1;\n for(int v: graph[u]) {\n if (v == p) contin...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
2
{ "code": "class Solution {\npublic:\n int dfs(vector<vector<int>>& adj, int n, int node, vector<bool>& visited, int& seats, long long& cost)\n {\n visited[node] = true;\n int subtreeSize = 0;\n for(int i=0; i<adj[node].size(); i++)\n {\n if(!visited[adj[node][i]])\n ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
2
{ "code": "class Solution {\npublic:\n int dfs(vector<vector<int>>& adj, int n, int node, vector<bool>& visited, int& seats, long long& cost)\n{\n visited[node] = true;\n int subtreeSize = 0;\n for(int i=0; i<adj[node].size(); i++)\n {\n if(!visited[adj[node][i]])\n {\n visited...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
2
{ "code": "class Solution {\npublic:\n // number of seats available, number of cars taken, ltrs of fuel\n pair<long long, pair<long long, long long>> dfs(long long node, long long par, vector<vector<long long>>& adj, long long seats, vector<long long>& sub) {\n pair<long long, pair<long long, long long>>...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
2
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = roads.size() + 1;\n vector<vector<int>> g(n);\n for(vector<int> x : roads) {\n g[x[0]].push_back(x[1]);\n g[x[1]].push_back(x[0]);\n }\n funct...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
2
{ "code": "class Solution {\npublic:\n long long myceil(long long x,int seats){\n if(x%seats==0) return x/seats;\n return (long long)ceil(x/seats)+1;\n }\n vector<long long> func(int node,vector<int> adj[],vector<int>& vis,int seats){\n vis[node]=1;\n long long people=1;long long ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
2
{ "code": "class Solution {\npublic:\n long long myceil(long long x,int seats){\n if(x%seats==0) return x/seats;\n return (long long)(x/seats)+1;\n }\n vector<long long> func(int node,vector<int> adj[],vector<int>& vis,int seats){\n vis[node]=1;\n long long people=1;long long fuel...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
2
{ "code": "class Solution {\npublic:\n struct City\n {\n City()\n : cost(0ll), car(0), seats(0), id(-1)\n {\n }\n City(long long cost, int car, int seats, int id)\n : cost(cost), car(car), seats(seats), id(id)\n {\n\n ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
2
{ "code": "typedef long long ll;\nclass Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n ll n=roads.size() + 1;\n vector<ll> subtreeSz(n,0);\n vector<vector<int>> adj(n);\n for(auto it:roads){\n adj[it[0]].push_back(it[1]);\n ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
3
{ "code": "class Solution {\npublic:\n long long dfs(vector<vector<int>>& graph,int source,long long& fuelConsumed,vector<bool>& visited,int seats) {\n visited[source] = true;\n\n long long countPeople = 1;\n \n for(auto i: graph[source]) {\n if(visited[i]) continue;\n ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
3
{ "code": "class Solution {\npublic:\n long long dfs(vector<vector<int>>& graph,int source,long long& fuelConsumed,vector<bool>& visited,int seats) {\n visited[source] = true;\n\n long long countPeople = 1;\n \n for(auto i: graph[source]) {\n if(visited[i]) continue;\n ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
3
{ "code": "class Solution {\npublic:\n\n long long answer = 0;\n\n void dfs(vector<vector<int>>& g, vector<pair<int, int> >& ans, int u, int p, int seats) {\n vector<pair<int, int> > pairs;\n for (const auto& ne : g[u]) {\n if (ne == p) continue;\n dfs(g, ans, ne, u, seats);\...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
3
{ "code": "class Solution {\npublic:\nvector<list<int>>graph;\nvector<pair<long long,int> >price;//{fuel and no of person}\nvector<bool>visited;\nvoid dfs(int idx,int seats){\n for(auto &i:graph[idx]){\n if(!visited[i]){\n visited[i] = true;\n dfs(i,seats);\n price[idx].firs...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
3
{ "code": "class Solution {\npublic:\n long long dfs(int node, vector<bool> &vis, int seats, vector<int> &p, vector<vector<int>> &adj)\n{\n vis[node] = true;\n long long fuel = 0;\n for (auto k : adj[node])\n {\n if (!vis[k]){\n fuel += dfs(k, vis, seats, p, adj);\n p[n...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
3
{ "code": "#define ll long long\nclass Solution {\n private:\n ll netFuel = 0;\n int dfs(int node, int seats, int parent, unordered_map<int, vector<int>>& adjMat) {\n int netPass = 0;\n for (int i : adjMat[node]) {\n if (parent != i)\n netPass += dfs(i, seats, node, adj...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
3
{ "code": "class Solution {\npublic:\n\n unordered_map<int,vector<int>> hash;\n int seats;\n long long res=0;\n int dfs(int node,int prev){\n int p=0;\n\n for(int num:hash[node]){\n if(num!=prev){\n int pass=dfs(num,node);\n p+=pass;\n ...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
3
{ "code": "class Solution {\npublic:\n void dfs(unordered_map<int,vector<int>> &adj, int node, int par, vector<int> &size){\n size[node] = 1;\n for(auto it:adj[node]){\n if(it!= par){\n dfs(adj,it,node,size);\n }\n }\n if(par!=-1){\n size[...
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> wher...
3
{ "code": "class Solution {\npublic:\n long long totalCost = 0;\n \n int dfs(int x, unordered_map<int, vector<int>>&adj, int seats, vector<int>&vis, int n){\n if(x>=n || x<0) return 0;\n\n vis[x] = 1;\n\n int totalPeople = 1, sz = adj[x].size();\n\n\n for(int i=0;i<sz;i++){\n ...