id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
1,503 | <p>A chef has collected data on the <code>satisfaction</code> level of his <code>n</code> dishes. Chef can cook any dish in 1 unit of time.</p>
<p><strong>Like-time coefficient</strong> of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. <code>ti... | 1 | {
"code": "class Solution {\npublic:\n int maxSatisfaction(vector<int>& s) {\n sort(s.begin(),s.end());\n int n=s.size();\n vector<int> suff(n);\n vector<int> pre(n);\n pre[0]=s[0];\n suff[n-1]=s[n-1];\n for(int i=1;i<n;i++){\n pre[i]+=pre[i-1]+s[i];\n ... |
1,503 | <p>A chef has collected data on the <code>satisfaction</code> level of his <code>n</code> dishes. Chef can cook any dish in 1 unit of time.</p>
<p><strong>Like-time coefficient</strong> of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. <code>ti... | 1 | {
"code": "class Solution {\npublic:\n int maxSatisfaction(vector<int>& satisfaction) {\n vector<int> stf = satisfaction;\n sort(stf.begin(), stf.end(), greater<int>());\n int sz = (int)stf.size();\n vector<int> dp;\n for (int i = 0; i < sz; i++) {\n int tmp = 0, idx =... |
1,503 | <p>A chef has collected data on the <code>satisfaction</code> level of his <code>n</code> dishes. Chef can cook any dish in 1 unit of time.</p>
<p><strong>Like-time coefficient</strong> of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. <code>ti... | 1 | {
"code": "class Solution {\npublic:\n int maxSatisfaction(vector<int>& s) {\n sort(s.begin(),s.end());\n int n = s.size();\n vector<int> next(n+2,INT_MIN);\n vector<int> curr(n+2,INT_MIN);\n for (int i=0; i<=n+1; i++) {\n next[i] = 0;\n }\n for (int i=n-... |
1,503 | <p>A chef has collected data on the <code>satisfaction</code> level of his <code>n</code> dishes. Chef can cook any dish in 1 unit of time.</p>
<p><strong>Like-time coefficient</strong> of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. <code>ti... | 1 | {
"code": "auto speedUP = [](){\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();\nclass Solution {\npublic:\n //Recursion\n // int maxSatifactionUsingRecursion(vector<int> &satis, int index, int time){\n // if(index >= satis.size()) return 0;\n\n // int i... |
1,503 | <p>A chef has collected data on the <code>satisfaction</code> level of his <code>n</code> dishes. Chef can cook any dish in 1 unit of time.</p>
<p><strong>Like-time coefficient</strong> of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. <code>ti... | 1 | {
"code": "class Solution {\npublic:\nint m[501][501];\nint solve(vector<int>& satisfaction,int n,int idx,int t){\n if(idx>n-1) return 0;\n if(m[idx][t]!=-1) return m[idx][t];\n int take=satisfaction[idx]*t+solve(satisfaction,n,idx+1,t+1);\n int nottake=solve(satisfaction,n,idx+1,t);\n int p=max(take,n... |
1,503 | <p>A chef has collected data on the <code>satisfaction</code> level of his <code>n</code> dishes. Chef can cook any dish in 1 unit of time.</p>
<p><strong>Like-time coefficient</strong> of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. <code>ti... | 1 | {
"code": "class Solution {\npublic:\n int t[501][501];\n\n int solve(vector<int>&sat, int n, int index, int time){\n if(index >= n){\n return 0;\n }\n\n if(t[index][time] != -1) return t[index][time];\n\n int exclude = solve(sat, n, index + 1, time);\n\n int includ... |
1,503 | <p>A chef has collected data on the <code>satisfaction</code> level of his <code>n</code> dishes. Chef can cook any dish in 1 unit of time.</p>
<p><strong>Like-time coefficient</strong> of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. <code>ti... | 1 | {
"code": "class Solution {\n int dp[510][510];\n vector<int> v;\n int n;\npublic:\n int rec(int taken,int level){\n if(level==n){\n return 0;\n }\n if(dp[taken][level]!=-1){\n return dp[taken][level];\n }\n int ans=0;\n ans=max(ans,rec(taken... |
1,503 | <p>A chef has collected data on the <code>satisfaction</code> level of his <code>n</code> dishes. Chef can cook any dish in 1 unit of time.</p>
<p><strong>Like-time coefficient</strong> of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. <code>ti... | 1 | {
"code": "class Solution {\npublic:\n int maxSatisfaction(vector<int>& satisfaction) {\n sort(rbegin(satisfaction), rend(satisfaction));\n int ans = 0, s = 0;\n for (int x : satisfaction) {\n s += x;\n if (s <= 0) {\n break;\n }\n ans... |
1,503 | <p>A chef has collected data on the <code>satisfaction</code> level of his <code>n</code> dishes. Chef can cook any dish in 1 unit of time.</p>
<p><strong>Like-time coefficient</strong> of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. <code>ti... | 1 | {
"code": "class Solution {\npublic:\n int maxSatisfaction(vector<int>& satisfaction) {\n int n = satisfaction.size(), total = 0, ans = 0;\n sort(satisfaction.rbegin(),satisfaction.rend());\n for(int i=0;i<n and total+satisfaction[i]>0;i++){\n total+=satisfaction[i];\n an... |
1,503 | <p>A chef has collected data on the <code>satisfaction</code> level of his <code>n</code> dishes. Chef can cook any dish in 1 unit of time.</p>
<p><strong>Like-time coefficient</strong> of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. <code>ti... | 1 | {
"code": "class Solution {\npublic:\n int maxSatisfaction(vector<int>& st) {\n sort(st.rbegin(), st.rend());\n int ans =0, last = 0;\n for (int i = 0; i < st.size(); i++) {\n if (i != 0)\n ans = max(ans, ans + last + st[i]);\n else\n ans = m... |
1,503 | <p>A chef has collected data on the <code>satisfaction</code> level of his <code>n</code> dishes. Chef can cook any dish in 1 unit of time.</p>
<p><strong>Like-time coefficient</strong> of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. <code>ti... | 1 | {
"code": "class Solution {\npublic:\n int maxSatisfaction(vector<int>& vec) {\n sort (vec.rbegin(), vec.rend());\n int sum = 0;\n int pref = 0;\n int n = vec.size();\n for (int i = 0; i < n; i++) {\n sum += pref;\n pref += vec[i];\n if (pref < 0)... |
1,503 | <p>A chef has collected data on the <code>satisfaction</code> level of his <code>n</code> dishes. Chef can cook any dish in 1 unit of time.</p>
<p><strong>Like-time coefficient</strong> of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. <code>ti... | 1 | {
"code": "class Solution {\npublic:\n int maxSatisfaction(vector<int>& sa) {\n sort(sa.rbegin(),sa.rend());\n vector<int> pr(sa.size());\n pr[0]=sa[0];\n int a=0;\n for(int i=1;i<sa.size();i++) pr[i]=sa[i]+pr[i-1];\n for(int i=0;i<sa.size();i++) if(pr[i]>=0)a+=pr[i];\n ... |
1,503 | <p>A chef has collected data on the <code>satisfaction</code> level of his <code>n</code> dishes. Chef can cook any dish in 1 unit of time.</p>
<p><strong>Like-time coefficient</strong> of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. <code>ti... | 1 | {
"code": "class Solution {\npublic:\nint dp[501][1001];\nint solve(vector<int>& s,int c,int i){\nif(i>=s.size())return 0;\nif(dp[i][c]!=-1)return dp[i][c];\nint take=s[i]*c+solve(s,c+1,i+1);\nint nt=solve(s,c,i+1);\nreturn dp[i][c]=max(take,nt);\n}\n int maxSatisfaction(vector<int>& s) {\n sort(s.begin(),s... |
1,503 | <p>A chef has collected data on the <code>satisfaction</code> level of his <code>n</code> dishes. Chef can cook any dish in 1 unit of time.</p>
<p><strong>Like-time coefficient</strong> of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. <code>ti... | 1 | {
"code": "class Solution {\npublic:\n int t[501][501];\n int solveTopDown(vector<int>& satisfaction){\n int n = satisfaction.size();\n int t[n+1][n+1];\n for(int i=0; i<n+1; i++){\n for(int j=0; j<n+1; j++){\n t[i][j] =0;\n }\n }\n for(int... |
1,503 | <p>A chef has collected data on the <code>satisfaction</code> level of his <code>n</code> dishes. Chef can cook any dish in 1 unit of time.</p>
<p><strong>Like-time coefficient</strong> of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. <code>ti... | 1 | {
"code": "class Solution {\npublic:\nint dp[1001][501];\n int solve(int i,int n,vector<int>& arr,int time)\n {\n if(i>=n ){\n return 0;\n }\n // return 0;\n if(dp[i][time]!=-1)\n return dp[i][time];\n\n int take = arr[i]*time+solve(i+1,n,arr,time+1);\n ... |
1,503 | <p>A chef has collected data on the <code>satisfaction</code> level of his <code>n</code> dishes. Chef can cook any dish in 1 unit of time.</p>
<p><strong>Like-time coefficient</strong> of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. <code>ti... | 1 | {
"code": "class Solution {\npublic:\n int dp[501][1000];\n int rec(int ind,vector<int>&ss,int n,int t){\n if(ind==n)return 0;\n if(dp[ind][t]!=0)return dp[ind][t];\n return dp[ind][t]=max(rec(ind+1,ss,n,t),t*ss[ind]+rec(ind+1,ss,n,t+1));\n }\n int maxSatisfaction(vector<int>& ss) {\n... |
1,503 | <p>A chef has collected data on the <code>satisfaction</code> level of his <code>n</code> dishes. Chef can cook any dish in 1 unit of time.</p>
<p><strong>Like-time coefficient</strong> of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. <code>ti... | 1 | {
"code": "class Solution {\npublic:\nint dp [550][550];\n\n int solve (vector <int>&v , int i , int cnt ){\n\n\n if (i == 0)\n\n {\n return max(v[i]*cnt, 0);\n }\n \n\n if (dp[i][cnt]!=-1)\n return dp[i][cnt];\n\n int nt =solve(v , i-1 ,cnt );\n int take = cnt * v[i] ... |
1,503 | <p>A chef has collected data on the <code>satisfaction</code> level of his <code>n</code> dishes. Chef can cook any dish in 1 unit of time.</p>
<p><strong>Like-time coefficient</strong> of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. <code>ti... | 1 | {
"code": "#define min_pq priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>\n#define vishal_mishra ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)\n#define print(x) { cout << x << \"\\n\"; }\n#define all(v) v.begin(), v.end()\n#define rall(v) v.rbegin(), v.rend()\n#define read(arr) for(a... |
1,503 | <p>A chef has collected data on the <code>satisfaction</code> level of his <code>n</code> dishes. Chef can cook any dish in 1 unit of time.</p>
<p><strong>Like-time coefficient</strong> of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. <code>ti... | 1 | {
"code": "class Solution {\n int dp[1001][1001];\n int solve(int i , int ttc , vector<int>& arr){\n if(i >= arr.size()){\n return 0;\n }\n\n if(dp[i][ttc] != -1){\n return dp[i][ttc];\n }\n\n int new_ttc = ttc+1;\n int take = (arr[i]*ttc )+ solve... |
1,503 | <p>A chef has collected data on the <code>satisfaction</code> level of his <code>n</code> dishes. Chef can cook any dish in 1 unit of time.</p>
<p><strong>Like-time coefficient</strong> of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. <code>ti... | 1 | {
"code": "class Solution {\npublic:\n\n // int tt;\n\n int dp[1000][1000];\n\n\n int solve(vector<int>& vec , int ind , int t){\n if(ind>=vec.size()) return 0;\n if(dp[ind][t] != -1) return dp[ind][t];\n\n int take= vec[ind]*t + solve(vec , ind+1 , t+1);\n\n int ntk=solve(vec, in... |
1,503 | <p>A chef has collected data on the <code>satisfaction</code> level of his <code>n</code> dishes. Chef can cook any dish in 1 unit of time.</p>
<p><strong>Like-time coefficient</strong> of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. <code>ti... | 1 | {
"code": "class Solution {\npublic:\n int dp[1000][1000];\n int solve(int i, int time,vector<int>& st){\n if(i>=st.size()){\n return 0;\n }\n if(dp[i][time]!=-1){\n return dp[i][time];\n }\n int incl=st[i]*(time+1)+solve(i+1,time+1,st);\n ... |
1,503 | <p>A chef has collected data on the <code>satisfaction</code> level of his <code>n</code> dishes. Chef can cook any dish in 1 unit of time.</p>
<p><strong>Like-time coefficient</strong> of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. <code>ti... | 1 | {
"code": "class Solution {\npublic:\n int dp[1010][1010];\n int solve(int i,int t,vector<int>&S){\n //base case:\n if(i==S.size()) return 0;\n if(dp[i][t]!=-1) return dp[i][t];\n // take this :\n int take=S[i]*(t+1) +solve(i+1,t+1,S);\n int not_take=0+solve(i+1,t,S);\n... |
1,503 | <p>A chef has collected data on the <code>satisfaction</code> level of his <code>n</code> dishes. Chef can cook any dish in 1 unit of time.</p>
<p><strong>Like-time coefficient</strong> of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. <code>ti... | 1 | {
"code": "class Solution {\npublic:\n int calculateScore(vector<int> satisfaction, int splitIndex, int multiplier) {\n if (splitIndex == satisfaction.size()) {\n return 0;\n }\n int result = satisfaction[splitIndex] * multiplier;\n multiplier++;\n for (int j = splitIn... |
1,503 | <p>A chef has collected data on the <code>satisfaction</code> level of his <code>n</code> dishes. Chef can cook any dish in 1 unit of time.</p>
<p><strong>Like-time coefficient</strong> of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. <code>ti... | 1 | {
"code": "class Solution {\nlong long dp[1005][1005] = {0};\npublic:\n int maxSatisfaction(vector<int>& satisfaction) {\n sort(satisfaction.begin(), satisfaction.end());\n int n = satisfaction.size();\n memset(dp,0,sizeof(dp[0][0])*((n+5)*(n+5)));\n for(int i = 1; i<n+1;i++){\n ... |
1,503 | <p>A chef has collected data on the <code>satisfaction</code> level of his <code>n</code> dishes. Chef can cook any dish in 1 unit of time.</p>
<p><strong>Like-time coefficient</strong> of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. <code>ti... | 1 | {
"code": "class Solution {\npublic:\n\n bool check(stack<int>& p,int& maxi)\n {\n int sum=0;\n stack<int> temp;\n int i=1;\n while(!p.empty())\n {\n int top = p.top();\n temp.push(top);\n p.pop();\n sum+=i*top;\n i++;\n ... |
1,503 | <p>A chef has collected data on the <code>satisfaction</code> level of his <code>n</code> dishes. Chef can cook any dish in 1 unit of time.</p>
<p><strong>Like-time coefficient</strong> of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. <code>ti... | 1 | {
"code": "class Solution {\n int solve(vector<int> &satisfaction , int n){\n vector<int> next(n+1, 0);\n for(int i=n-1; i >= 0 ; i--){\n vector<int> curr(n+1, 0);\n for(int j=i; j >= 0 ; j--){\n int include = ((j + 1) * satisfaction[i]) +next[j+1];\n int e... |
1,503 | <p>A chef has collected data on the <code>satisfaction</code> level of his <code>n</code> dishes. Chef can cook any dish in 1 unit of time.</p>
<p><strong>Like-time coefficient</strong> of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. <code>ti... | 1 | {
"code": "class Solution {\npublic:\n int solve(int ind,int time,vector<int>&satisfaction,vector<vector<int>>&dp){\n if(ind>=satisfaction.size())return 0;\n if(dp[ind][time]!=-1)return dp[ind][time];\n int include=satisfaction[ind]*time+solve(ind+1,time+1,satisfaction,dp);\n int notinc... |
1,503 | <p>A chef has collected data on the <code>satisfaction</code> level of his <code>n</code> dishes. Chef can cook any dish in 1 unit of time.</p>
<p><strong>Like-time coefficient</strong> of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. <code>ti... | 1 | {
"code": "class Solution {\npublic:\n int maxsatbyDP(vector<int>& sat)\n {\n vector<vector<int>>dp(sat.size()+1, vector<int>(sat.size()+2, 0));\n for(int i=sat.size()-1;i>=0;--i)\n {\n for(int time = sat.size(); time>=1; --time)\n {\n int include = time*sat[i] + dp[i+1][time+1];... |
1,503 | <p>A chef has collected data on the <code>satisfaction</code> level of his <code>n</code> dishes. Chef can cook any dish in 1 unit of time.</p>
<p><strong>Like-time coefficient</strong> of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. <code>ti... | 1 | {
"code": "class Solution {\npublic:\n vector<vector<int>>dp;\n int rec(int index, int time, vector<int>& satisfaction){\n if(index == satisfaction.size()){\n return 0;\n }\n\n if(dp[index][time] != -1) return dp[index][time];\n\n int take = rec(index + 1, time, satisfacti... |
1,503 | <p>A chef has collected data on the <code>satisfaction</code> level of his <code>n</code> dishes. Chef can cook any dish in 1 unit of time.</p>
<p><strong>Like-time coefficient</strong> of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. <code>ti... | 2 | {
"code": "class Solution {\npublic:\n int solve(int i,int n,vector<int>& arr,int time,vector<vector<int>>&dp)\n {\n // if(i==j)\n if(i>=n ){\n return 0;\n }\n // return 0;\n if(dp[i][time]!=-1)\n return dp[i][time];\n\n int take = arr[i]*time+solve(i+... |
1,503 | <p>A chef has collected data on the <code>satisfaction</code> level of his <code>n</code> dishes. Chef can cook any dish in 1 unit of time.</p>
<p><strong>Like-time coefficient</strong> of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. <code>ti... | 2 | {
"code": "#pragma GCC optimize(\"O3\",\"unroll-loops\")\nclass Solution {\npublic:\n int maxSatisfaction(vector<int>& v) {\n sort(v.begin(),v.end());\n int n=v.size();\n vector<vector<int>> dp(n,vector<int>(n+1,-1e9));\n int ans=0;\n dp[0][0]=0;\n dp[0][1]=v[0];\n ... |
1,503 | <p>A chef has collected data on the <code>satisfaction</code> level of his <code>n</code> dishes. Chef can cook any dish in 1 unit of time.</p>
<p><strong>Like-time coefficient</strong> of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. <code>ti... | 2 | {
"code": "class Solution {\npublic:\n int sol(vector<vector<int>>&dp,vector<int>& sat,int i,int t)\n {\n if(i>=sat.size())return 0;\n if(dp[i][t]!=-1)return dp[i][t];\n\n int a=(sat[i]*t)+sol(dp,sat,i+1,t+1);\n int b= sol(dp,sat,i+1,t);\n int mini=max(a,b);\n\n return dp[... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 0 | {
"code": "class Solution {\npublic:\n int V,comp;\n vector<int> parent;\n int find(int u){\n while(1){\n if(parent[u]<0)\n return u;\n u=parent[u];\n }\n }\n \n void union2(int u,int v){\n int p1=find(u);\n int p2=find(v);\n if... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 0 | {
"code": "class Solution {\npublic:\n int V,comp;\n vector<int> parent;\n int find(int u){\n while(1){\n if(parent[u]<0)\n return u;\n u=parent[u];\n }\n }\n \n void union2(int u,int v){\n int p1=find(u);\n int p2=find(v);\n if... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 0 | {
"code": "class Solution {\npublic:\n class dsu {\n int n;\n vector<int> parent, rank;\n public:\n dsu(int n) {\n this->n = n;\n parent.resize(n);\n rank.resize(n, 1);\n for (int i = 0; i < n; i++) {\n parent[i] = i;\n }... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 0 | {
"code": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <climits>\n#include <numeric>\n\nclass Solution {\npublic:\n int vertices, components;\n std::vector<int> parent;\n\n int find(int node) {\n while (true) {\n if (parent[node] < 0) return node;\n nod... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 0 | {
"code": "class A {\npublic:\n vector<int> parent;\n A(int n) {\n parent.resize(n);\n for (int i = 0; i < n; i++) {\n parent[i] = i; \n }\n }\n\n int f(int node) {\n if (node == parent[node])\n return node;\n return parent[node] = f(parent[node]);\... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 0 | {
"code": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <climits>\n\nusing namespace std;\n\nclass UnionFind {\npublic:\n vector<int> parent;\n\n UnionFind(int n) {\n parent.resize(n);\n for (int i = 0; i < n; i++) {\n parent[i] = i;\n }\n }\n\n in... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 1 | {
"code": "// Зручна структура для роботи з потоком ребер\nstruct Edge {\n int a, b, ind;\n};\n\n\n// Структура для пошуку циклів\nstruct RevEdge {\n int from, ind;\n};\n\nclass Solution {\npublic:\n vector<vector<int>> findCriticalAndPseudoCriticalEdges(int n, vector<vector<int>>& edges) {\n int m = ... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 1 | {
"code": "class Dsu{\n vector<int> parent;\n int n;\npublic:\n Dsu(int _n){\n n = _n;\n parent.resize(n, -1);\n }\n\n int findParent(int a)\n {\n if(parent[a]<0)\n return a;\n return parent[a] = findParent(parent[a]);\n }\n\n void unionNodes(int a, int b... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 1 | {
"code": "class Solution {\npublic:\n int find(int u, vector<int> &parent){\n if(parent[u]<0){\n return u;\n }\n return parent[u]=find(parent[u], parent);\n }\n \n void unite( int &comp ,int u , int v, vector<int> &parent){\n int p1=find(u, par... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 1 | {
"code": "class Solution {\npublic:\n int find(int u, vector<int> &parent){\n while(1){\n if(parent[u]<0){\n return u;\n }\n u=parent[u];\n }\n }\n \n void unite( int &comp ,int u , int v, vector<int> &parent){\n ... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 1 | {
"code": "class UnionFind {\npublic:\n int n,set_size, *parent, *rank;\n UnionFind(){}\n UnionFind(int a) { //a: set size, {1,2...n}\n n=set_size=a;\n parent=new int[n];\n rank=new int[n];\n for (int i=0;i<n;i++) parent[i]=i, rank[i]=1;\n }\n\n int find(int x) { //O(A(n))\n... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 1 | {
"code": "class UnionFind {\npublic:\n int n,set_size, *parent, *rank;\n UnionFind(){}\n UnionFind(int a) { //a: set size, {1,2...n}\n n=set_size=a;\n parent=new int[n];\n rank=new int[n];\n for (int i=0;i<n;i++) parent[i]=i, rank[i]=1;\n }\n\n int find(int x) { //O(A(n))\n... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 2 | {
"code": "#include <vector>\n#include <algorithm>\n\nusing namespace std;\n\nclass UnionFind {\npublic:\n vector<int> parent, rank;\n \n UnionFind(int n) {\n parent.resize(n);\n rank.resize(n, 1);\n for (int i = 0; i < n; ++i)\n parent[i] = i;\n }\n \n int find(int u... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 2 | {
"code": "class Disjoint{\n private:\n vector<int> rank,parent;\n public:\n Disjoint(int n){\n rank.resize(n,0);\n parent.resize(n);\n for(int i=0;i<n;i++){\n parent[i]=i;\n }\n }\n int ulp(int node){\n if(node==parent[node]){\n return node;\... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 2 | {
"code": "class Solution {\npublic:\n class DSU {\n public:\n vector<int> parent, size;\n DSU(int n) {\n parent.resize(n + 1);\n size.resize(n + 1);\n for (int i = 0; i <= n; i++) {\n parent[i] = i;\n size[i] = 1;\n }\n ... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 2 | {
"code": "class DisjointSet{\n vector<int> parent,size;\npublic:\n DisjointSet(int n){\n parent.resize(n+1);\n size.resize(n+1,1);\n for(int i=0;i<=n;i++){\n parent[i] = i;\n }\n }\n int findP(int i){\n if(parent[i]==i){\n return i;\n }\n ... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 2 | {
"code": "class Solution {\npublic:\n class UnionFind{\n public:\n UnionFind(int n) : n(n), link(n), size(n) {\n for(int i = 0; i < n; i++){\n link[i] = i;\n size[i] = 1;\n }\n\n };\n\n int find(int x){\n\n while(x != link[... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 2 | {
"code": "\n#include <bits/stdc++.h>\nusing namespace std;\n\nstatic const int _ = []()\n{\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();\n\nclass UnionFind\n{\npublic:\n vector<int> parent, size;\n UnionFind(int n)\n {\n parent.resize(n, 0);\n ... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class Solution {\n class DisjointSet{\n public:\n int* parent;\n int* rank;\n DisjointSet(int n){\n parent=new int[n];\n rank=new int[n];\n for(int i=0;i<n;i++){\n parent[i]=i;\n rank[i]=0;\n }\n ... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class DisjointSet\n{\n vector<int> rank, parent, size;\n\npublic:\n DisjointSet(int n)\n {\n rank.resize(n, 0);\n parent.resize(n);\n size.resize(n);\n for (int i = 0; i < n; i++)\n {\n parent[i] = i;\n size[i] = 1;\n }\n }\n\n ... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class DSU{\n public:\n vector<int> size;\n vector<int> parent;\n public:\n DSU(int n){\n size.resize(n,1);\n parent.resize(n);\n for(int i =0;i<n;i++) parent[i]=i;\n }\n \n int find(int node){\n if(parent[node]==node) return node;\n return parent[n... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class DSU{\n public:\n vector<int> size;\n vector<int> parent;\n public:\n DSU(int n){\n size.resize(n,1);\n parent.resize(n);\n for(int i =0;i<n;i++) parent[i]=i;\n }\n \n int find(int node){\n if(parent[node]==node) return node;\n return parent[n... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> findCriticalAndPseudoCriticalEdges(int n, vector<vector<int>>& edges) {\n vector<int> parent(n), rank(n);\n \n // Initialize Union-Find structure\n function<int(int)> find = [&](int u) {\n return parent[u] == u ? u : ... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class Solution {\npublic:\n class disjoint{\n public:\n vector<int> parent,rank;\n disjoint(int n)\n {\n parent.resize(n);\n rank.resize(n, 1);\n for (int i = 0; i < n; i++) {\n parent[i] = i;\n }\n }\n int findpar(int u)\n... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class DSU{\n public:\n vector<int> par, siz;\n DSU() {}\n DSU(int n) {\n par.resize(n,0);\n siz.resize(n,1);\n for(int i=0;i<n;i++) par[i]=i;\n }\n\n int fin(int p) {\n if(par[p]==p) return p;\n return par[p] = fin(par[p]);\n }\n\n bool uni(int a, ... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class Solution {\npublic:\n struct union_find {\n vector<int> parent;\n vector<int> size; \n union_find(int n){\n parent=vector<int>(n);\n size=vector<int>(n, 1);\n for(int i=0;i<n;i++) parent[i]=i;\n }\n int _find(int x){\n ... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "struct DisjoinSetUnion {\npublic:\n DisjoinSetUnion(int _size): size(_size){\n for (int i = 0; i < _size; ++i)\n parent.emplace_back(i);\n }\n bool isUnion(const int &a, const int &b) {\n return getParent(a) == getParent(b); \n }\n void unionSets(int a, int b){\n ... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "struct DisjoinSetUnion {\npublic:\n DisjoinSetUnion(int _size): size(_size){\n for (int i = 0; i < _size; ++i)\n parent.emplace_back(i);\n }\n bool isUnion(const int &a, const int &b) {\n return getParent(a) == getParent(b); \n }\n void unionSets(int a, int b){\n ... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "using vi = vector<int>;\nusing vvi = vector<vi>;\n\nstruct Edge{\n int a;\n int b;\n int w;\n int ind;\n};\n\nclass dsu{\n public:\n vi parent;\n vi rank;\n\n dsu(int n){\n rank.assign(n, 1);\n for(int i=0; i<n; i++) parent.push_back(i);\n }\n\n int find(int a){\... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "using vi = vector<int>;\nusing vvi = vector<vi>;\n\nstruct Edge{\n int a;\n int b;\n int w;\n int ind;\n};\n\nclass dsu{\n public:\n vi parent;\n vi rank;\n\n dsu(int n){\n rank.assign(n, 1);\n for(int i=0; i<n; i++) parent.push_back(i);\n }\n\n int find(int a){\... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": " class DisjointSet{\n\tvector<int> rank,parent,size;\npublic : \n DisjointSet(int n){\n \tsize.resize(n+1,1);\n \trank.resize(n+1,0);\n \tparent.resize(n+1);\n \tfor(int i=0;i<=n;i++){\n \t\tparent[i]= i; \n \t}\n }\n int findUPar(int node){\n \tif(node==p... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class DisjointSet{\n vector<int> parent, size;\n public:\n\n DisjointSet(int v){\n parent.resize(v+1);\n size.resize(v+1);\n\n for(int i = 0 ; i <= v ; i++){\n parent[i] = i;\n }\n\n for(int i = 0 ; i <= v ; i++){\n size[i] = 1;\n }\n... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class DisjointSet {\n vector<int> rank, parent, size;\npublic:\n DisjointSet(int n) {\n rank.resize(n + 1, 0);\n parent.resize(n + 1);\n size.resize(n + 1);\n for (int i = 0; i <= n; i++) {\n parent[i] = i;\n size[i] = 1;\n }\n }\n\n int ... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class Solution {\npublic:\n int find(vector<int> &uf, int node) {\n if (node == uf[node])\n return uf[node];\n uf[node] = find(uf, uf[node]);\n return uf[node];\n }\n int BuildMST(vector<int> &uf, vector<vector<int>> &edges, set<int> &mst) {\n int res = 0;\n ... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class Solution {\npublic:\n class DSU{\n public:\n vector<int> parent;\n vector<int> subtreeSize;\n\n void init(int n){\n parent.resize(n);\n subtreeSize.assign(n, 1);\n for(int i = 0; i < n; i++) parent[i] = i;\n }\n\n int findP... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "#define pii pair<int,int>\n#define vsi vector<set<pii>>\n#define idx first\n#define val second\n\nclass Solution {\n\n struct cmp {\n bool operator() (pii a, pii b) {\n return a.val > b.val;\n }\n };\n\n int findMST (const vsi& adjList, int n) {\n vector<int> dists(... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class Solution {\npublic:\n int find(vector<int>&parent, int x){\n //TC=log*n (log star n)\n if(parent[x]==x) return x;\n return parent[x]=find(parent, parent[x]);\n }\n void unionof(vector<int>&parent, vector<int>&rank, int a, int b){\n a=find(parent,a);\n b=fin... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class Solution {\npublic:\n int findpar(vector<int>& parent, int node) {\n if (node == parent[node]) {\n return node;\n }\n return parent[node] = findpar(parent, parent[node]);\n }\n void unionbysize(vector<int>& size, vector<int>& parent, int u, int v) {\n i... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "struct DSU{\n\tint n,level[105],pa[105];\n\tvoid init(int n){\n\t\tthis->n = n;\n\t\tfor(int i=0;i<n;i++){\n\t\t\tlevel[i]=0;\n\t\t\tpa[i]=i;\n\t\t}\n\t}\n\tint getpa(int x){\n\t\tif(x==pa[x])return x;\n\t\treturn pa[x]=getpa(pa[x]);\n\t}\n\tbool uni(int x,int y){\n\t\tx=getpa(x);\n\t\ty=getpa(y);\n\t\tif(... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class Solution {\npublic:\n int spanning(vector<pair<int,int>>adj[], vector<vector<int>>&edges, int n, int block, int comp){\n int start = 0;\n\n vector<int>vis(n,0);\n int wt = 0;\n priority_queue<pair<pair<int,int>,int>,vector<pair<pair<int,int>,int>>,\n ... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "#define ll int\nclass Disjoint\n{\n vector<int> rank, size, parent;\n\npublic:\n Disjoint(ll n)\n {\n rank.resize(n + 1, 0);\n parent.resize(n + 1, 0);\n for(int i= 0;i<n;i++) parent[i] = i;\n size.resize(n + 1, 1);\n }\n\n ll findUltimateparent(ll n)\n {\n ... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "\ntemplate <typename T>\nclass DisjointSet\n{\n unordered_map<T, T> parent;\n unordered_map<T, int> rank;\n public:\n int num_sets;\n DisjointSet(){\n num_sets=0;\n }\n void makeSet(vector<T> universe)\n {\n parent.clear();\n rank.clear();\n for (T i : un... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "bool cmp(vector<int>&a,vector<int>&b){\n cout<<a.size()<<\" \"<<b.size()<<endl;\n return a[2]<=b[2];\n}\nclass Solution {\npublic:\n int find_parent(int a,vector<int>&parent){\n if(a==parent[a]) return a;\n else return parent[a]=find_parent(parent[a],parent); \n }\n bool un... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "bool comp(vector<int>& a, vector<int>& b){\n return a[2]<b[2];\n}\n\nclass Solution {\npublic:\n vector<int> parent, ranks;\n void make_set(int v){\n parent[v]=v;\n ranks[v]=0;\n }\n int find_set(int v){\n if(v==parent[v]){\n return v;\n }\n else... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class Solution {\npublic:\nset<int> critical,pseudo;\nint mstweight=0;\nint curmst=0;\nint cc=0;\nset<vector<int>> edge;\nint n;\nvector<int> rank,par;\n\nint find(int a)\n{\n if(par[a]==a) return a;\n\n return par[a]=find(par[a]);\n}\n\nvoid unionn(int a ,int b)\n{\n a=find(a);\n b=find(b);\n\... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class UnionFind{\n public:\n vector<int> parent;\n UnionFind(int n){\n parent.resize(n);\n for(int i=0;i<n;i++){\n parent[i]=i;\n }\n }\n int find(int p){\n if(parent[p]==p)\n return p;\n return parent[p]=find(parent[p]);\n }\n b... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class UnionFind{\n public:\n vector<int> parent;\n UnionFind(int n){\n parent.resize(n);\n for(int i=0;i<n;i++){\n parent[i]=i;\n }\n }\n int find(int p){\n if(parent[p]==p)\n return p;\n return parent[p]=find(parent[p]);\n }\n b... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class UnionFind{\n public:\n vector<int> parent;\n UnionFind(int n){\n parent.resize(n);\n for(int i=0;i<n;i++){\n parent[i]=i;\n }\n }\n int find(int p){\n if(parent[p]==p)\n return p;\n return parent[p]=find(parent[p]);\n }\n b... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class DSU {\nprivate:\n vector<int> parent;\npublic:\n DSU(int n) {\n parent.resize(n,-1);\n }\n int find_leader(int x) {\n if (parent[x] < 0) return x;\n else return parent[x] = find_leader(parent[x]);\n }\n void merge(int x, int y) {\n x = find_leader(x);\n ... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class DSU {\nprivate:\n vector<int> parent;\npublic:\n DSU(int n) {\n parent.resize(n,-1);\n }\n int find_leader(int x) {\n if (parent[x] < 0) return x;\n else return parent[x] = find_leader(parent[x]);\n }\n void merge(int x, int y) {\n x = find_leader(x);\n ... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class Solution {\n class UnionFind {\n vector<int> parents;\n public:\n int count;\n UnionFind(int n) {\n parents.resize(n);\n for (int i = 0; i < n; i++) {\n parents[i] = i;\n }\n count = n;\n }\n\n int fin... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class UnionFind {\npublic: \n\n unordered_map<int,int> parent;\n unordered_map<int,int> rank;\n\n UnionFind(int n) {\n for(int i=0; i<n; i++) {\n parent[i] = i;\n rank[i] = 1;\n }\n }\n\n int find(int p) {\n while(p != parent[p]) {\n pare... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class Solution {\npublic:\n int size=0; \n static bool comp(vector<int>&v1,vector<int>&v2) //comperator function\n {\n return v1[2]<v2[2];\n }\n \n int findpar(vector<int>&par,int u) //find parent of a given nodes \n {\n if(u==par[u])return u;\n ... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class Solution {\npublic:\n int size=0; \n static bool comp(vector<int>&v1,vector<int>&v2) //comperator function\n {\n return v1[2]<v2[2];\n }\n \n int findpar(vector<int>&par,int u) //find parent of a given nodes \n {\n if(u==par[u])return u;\n ... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class UnionFind{\n vector<int> root, rank;\npublic:\n UnionFind(int n){\n rank.resize(n,1);\n root.resize(n);\n for(int i=0;i<n;i++) root[i] = i;\n }\n int find(int node){\n if(node==root[node]) return node;\n return root[node] = find(root[node]);\n }\n ... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class Solution {\npublic:\n class Disjoint {\n public:\n vector<int> parent, size;\n int comp, maxSize = 0;\n Disjoint(int n) {\n comp = n;\n parent.resize(n);\n size.resize(n);\n for (int i = 0; i < n; i++) {\n parent[i]... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class Solution {\npublic:\nint find(int x,vector<int>&parent){\n if(parent[x]==x)return x;\n\n return parent[x]=find(parent[x],parent);\n}\nvoid unionn(vector<int>&rank,vector<int>&parent,int &x,int &y){\n int x_parent=find(x,parent);\n int y_parent=find(y,parent);\n\n if(x_parent!=y_parent)... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class Solution {\npublic:\n // https://www.youtube.com/watch?v=83JnUxrLKJU\n\n int findParent(int a, vector<int>& parent) {\n if (parent[a] == -1) {\n return a;\n }\n return findParent(parent[a], parent);\n }\n\n bool doUnion(int a, int b, vector<int>& rank, vect... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class DisjointSet {\n public:\n vector<int> size, parent;\n DisjointSet(int n) {\n size.resize(n+1, 1);\n parent.resize(n+1);\n for (int i = 0; i <= n; i++) {\n parent[i] = i;\n }\n }\n \n int findUParent(int u) {\n if (parent[u] == u)\n ... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class disjoint{\n vector<int> parent;\n vector<int> size;\n public:\n disjoint(int n){\n parent.resize(n);\n size.resize(n);\n for(int i=0;i<n;i++){\n parent[i]=i;\n size[i]=1;\n }\n }\n\n int findUltParent(int node){\n if(node==par... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class DisjointSet {\n vector<int> rank, parent; \npublic: \n DisjointSet(int n) {\n rank.resize(n+1, 0); \n parent.resize(n+1);\n for(int i = 0;i<=n;i++) {\n parent[i] = i; \n }\n }\n\n int findUPar(int node) {\n if(node == parent[node])\n ... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class Solution {\npublic:\n int findparent(int x, vector<int> &parent) {\n if(parent[x] == -1) {\n return x;\n }\n return parent[x] = findparent(parent[x], parent);\n }\n\n void unionset(int x, int y, vector<int> &parent, vector<int>&rank) {\n int s1 = findpa... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class Solution {\npublic:\n int find_parent(int x,vector<int>&parent){\n\t if(x==parent[x])return x;\n\t return parent[x]=find_parent(parent[x],parent);\n\t}\n\tbool union_set(int a, int b,vector<int>&parent,vector<int>&rank){\n\t int pa=find_parent(a,parent);\n\t int pb=find_parent(b,parent... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class dsu\n{\n\tvector<int>rank,parent,size;\n\tpublic:\n\t\tdsu(int n){\n\t\t\trank.resize(n+1,0);\n\t\t\tparent.resize(n+1,0);\n\t\t\tsize.resize(n+1,1);\n\t\t\tfor(int i = 0; i <= n; i++){\n\t\t\t\tparent[i]=i;\n\t\t\t}\n\t\t}\n\t\tint findPar(int node){\n\t\t\tif(node==parent[node]){\n\t\t\t\treturn no... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class UnionFind{\n public:\n vector<int> parent;\n vector<int> size;\n int n;\n UnionFind(int a)\n {\n n = a;\n for(int i = 0; i < n; i++)\n {\n parent.push_back(i);\n size.push_back(1);\n }\n }\n int findRoot(int a)\n {\n ... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "bool comp(vector<int>& a, vector<int>& b){\n return a[2]<b[2];\n}\n\nclass Solution {\npublic:\n vector<int> parent, ranks;\n void make_set(int v){\n parent[v]=v;\n ranks[v]=0;\n }\n int find_set(int v){\n if(v==parent[v]){\n return v;\n }\n else... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class Solution {\npublic:\n int find(int u, vector<int>& parent) {\n if (parent[u] < 0) return u;\n return parent[u] = find(parent[u], parent); \n }\n\n vector<pair<int, pair<int, int>>> create_mst(vector<pair<int, pair<int, int>>>& v, int& dis, int s) {\n vector<pair<int, pa... |
1,613 | <p>Given a weighted undirected connected graph with <code>n</code> vertices numbered from <code>0</code> to <code>n - 1</code>, and an array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between nodes ... | 3 | {
"code": "class Solution {\npublic:\n vector<int>parent;\n void disjoint(int size){\n parent.resize(size+1);\n for(int i=0;i<=size;i++)\n parent[i]=(i);\n }\n int find(int u){\n if(parent[u]==u)return u;\n return parent[u] = find(parent[u]);\n }\n void merge(int u... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.