id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "class Solution {\npublic:\n long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {\n long long n=nums1.size();\n vector<pair<long long,long long>> v;\n for(long long i=0;i<nums1.size();i++)\n {\n v.push_back({nums2[i],nums1[i]});\n }\n\n ... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "class Solution {\npublic:\n#define pll pair<long long, long long>\n long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {\n long long res = 0;\n long long sum = 0;\n vector<pll> v;\n int n = nums1.size();\n for (int i = 0; i < n; i++) {\n v.pus... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "class Solution {\npublic:\n long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {\n int n=nums1.size();\n\n vector<array<long long,2>>v;\n\n for(int i=0;i<n;i++){\n v.push_back({nums2[i],nums1[i]});\n }\n\n sort(v.rbegin(),v.rend());\n\n ... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "class Solution {\npublic:\n long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {\n int n = nums1.size();\n vector<pair<long long, long long>> nums(n);\n for(int i=0;i<n;i++){\n nums.push_back({-nums2[i], nums1[i]});\n }\n sort(nums.begin(), nu... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "class Solution {\npublic:\n long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {\n int n = nums1.size();\n vector<pair<long long, long long>> nums(n);\n for(int i=0;i<n;i++){\n nums.push_back({-nums2[i], nums1[i]});\n }\n sort(nums.begin(), nu... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "class Solution {\npublic:\n long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {\n int n = nums1.size();\n std::map<int, std::vector<int>> vals;\n for (int i = 0; i < n; i++)\n vals[nums2[i]].push_back(nums1[i]);\n \n int idx = 0;\n whi... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "#define ll long long\nclass Solution {\npublic:\n ll maxScore(vector<int>& nums1, vector<int>& nums2, int k) {\n ll ans = 0;\n vector<pair<ll, ll>>v;\n int n = nums1.size();\n for(int i=0; i<n; i++){\n v.push_back({nums1[i], nums2[i]});\n }\n sort(v.b... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "\nclass Solution {\npublic:\n long long maxScore(vector<int>& a, vector<int>& nums2, int k) {\n\n // priority_queue<pair<long long,long long>>pq;\n // long long n = nums1.size();\n // for(long long i=0;i<n;i++){\n // pq.push({nums2[i],nums1[i]});\n // }\n //... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "class Solution {\npublic:\n typedef long long ll;\n \n long long maxScore(vector<int>& v1, vector<int>& v2, int k) {\n vector<pair<int,int>> vec;\n\n for(int i=0;i<v1.size();i++){\n vec.push_back({v1[i],v2[i]});\n }\n sort(vec.rbegin(),vec.rend());\n\n ... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "class Solution {\npublic:\n\n long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {\n\n int n = nums1.size() ;\n\n vector<pair<int,int>> p1(n) ;\n for( int i=0 ; i<n ; i++ ){\n p1[i] = { nums2[i], nums1[i] } ;\n }\n sort( p1.rbegin() , p1.rend... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "using ll = long long;\nclass Solution {\npublic:\n long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {\n int N = nums1.size();\n vector<vector<int>> NUMS(N, vector<int>(2));\n for (int i = 0 ; i < N ; i++) {\n NUMS[i][0] = nums1[i];\n NUMS[i][1]... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "class Solution {\npublic:\n static bool cmp(const vector<int>& a, const vector<int>& b) {\n return a[0] > b[0];\n }\n long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {\n vector<vector<int>> n(nums1.size(),vector<int>(2,0));\n for(int i=0;i<nums1.size();i++) {... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "class Solution {\npublic:\n long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {\n long long ans = 0;\n\n int n = nums1.size();\n vector<vector<int>>temp(n, vector<int>(2));\n for (int i=0; i<n; i++)\n {\n temp[i][0] = nums1[i];\n t... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "class Solution {\npublic:\n\n long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {\n std::vector<std::vector<int>> nums(nums1.size(), std::vector<int>(2));\n for(int i = 0; i < nums.size(); i++){\n nums[i][0] = nums1[i];\n nums[i][1] = nums2[i];\n ... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "class MyObject {\npublic:\n long long first;\n long long second;\n long long id;\n};\n\nclass Solution {\npublic:\n long long maxi(long long a, long long b){\n if(a>b){\n return a;\n }\n return b;\n }\n long long maxScore(vector<int>& nums1, vector<int>& nu... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "class MyObject {\npublic:\n long long first;\n long long second;\n long long id;\n};\n\nclass Solution {\npublic:\n long long maxi(long long a, long long b){\n if(a>b){\n return a;\n }\n return b;\n }\n long long maxScore(vector<int>& nums1, vector<int>& nu... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "class Solution {\npublic:\n long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {\n int n = nums1.size();\n vector<pair<int, int>> a(n);\n\n for(int i=0;i<n;i++) {\n a[i]={nums2[i], nums1[i]};\n }\n\n sort(a.begin(), a.end());\n multiset... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "class Solution {\npublic:\n long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {\n vector<pair<int,int>> vp;\n int n = nums1.size();\n for(int i=0;i<n;i++){\n vp.push_back({nums1[i],nums2[i]});\n }\n\n sort(vp.begin(),vp.end(), [](auto &x , au... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "class Solution {\npublic:\n long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {\n // (nums2, nums1)\n multiset<pair<int, int>> pairs;\n for (int i = 0; i < nums1.size(); i++) {\n auto temp = pair<int, int>(-nums2[i], -nums1[i]);\n pairs.... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "class Solution {\npublic:\n long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {\n vector<vector<int>> joined(nums1.size());\n\n for(int i=0; i<nums1.size(); i++){\n joined[i] = {nums2[i], nums1[i]};\n }\n sort(joined.rbegin(), joined.rend());\n ... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "class Solution {\npublic:\n long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {\n // for pairs {n2, n1}, sort by n2\n // go over from biggest n2, sliding window calc sum\n // sum += new n1, sum -= smallest n1\n // multi * new n2, update result\n\n long... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "class Solution {\npublic:\n long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {\n int n = nums1.size();\n\n vector<vector<int>> p(n);\n\n for(int i=0; i<n; i++) {\n p[i]={nums2[i],nums1[i]};\n }\n sort(p.rbegin(),p.rend());\n\n priorit... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "class Solution {\npublic:\n long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {\n vector<pair<int, int>> storage; storage.reserve(nums1.size());\n for (int i = 0; i < nums1.size(); i++) {\n storage.emplace_back(nums1[i], nums2[i]);\n }\n\n sort(stor... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "class Solution {\npublic:\n long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {\n const int size = nums1.size();\n const int *d1 = nums1.data();\n const int *d2 = nums2.data();\n\n int last{k};\n int64_t curSum{0}, maxScore{0};\n auto pairGreater... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "class Solution {\npublic:\n long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {\n vector<pair<int, int>> v;\n for (int i = 0; i < nums1.size(); i++) {\n v.emplace_back(nums2[i], nums1[i]);\n }\n sort(v.begin(), v.end());\n multiset<int> s;\n ... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "class Solution {\npublic:\n long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {\n int n = nums1.size();\n vector<pair<int, int>> pairs(n);\n for (int i = 0; i < n; ++i) {\n pairs[i] = {nums2[i], nums1[i]};\n }\n sort(pairs.rbegin(), pairs.ren... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "class Solution {\n #define ll long long\npublic:\n long long maxScore(vector<int>& a, vector<int>& b, int k) {\n int n = a.size();\n vector<pair<ll,ll>>v(n);\n for(int i = 0;i<n;i++){\n v[i] = {b[i],a[i]};\n }\n\n sort(v.begin(),v.end(),greater<pair<ll,ll... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "class Solution {\npublic:\n long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {\n vector<pair<int,int>> vec;\n multiset<int> m;\n for(int i=0;i<nums1.size();i++) vec.push_back({nums2[i],nums1[i]});\n sort(vec.begin(),vec.end());\n for(auto it : nums1) m... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "class Solution {\npublic:\n long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {\n vector< pair< int, int > > a;\n int n = nums1.size();\n for (int i =0; i < n; i++){\n a.push_back({nums1[i], nums2[i]});\n }\n sort(a.begin(), a.end(), [](auto ... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "class Solution {\npublic:\n long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {\n int n = (int)nums1.size();\n // sort nums2 by number (keeping also position)\n std::vector<std::pair<int, int>> nums2min;\n for (int i = 0; i < n; ++i) {\n nums2min.pu... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "class Solution {\npublic:\n long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {\n long long ans = 0;\n vector<pair<int, int>> v1;\n multiset<int> s;\n int n = nums1.size();\n for(int i = 0; i < n; i++){\n v1.push_back({nums2[i], nums1[i]});\n... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "class Solution {\npublic:\n long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {\n vector<pair<long long,long long>> v;\n int sz = nums1.size();\n for(int i=0;i<sz;i++) v.push_back({nums1[i],nums2[i]});\n sort(begin(v),end(v),[&](pair<long long,long long>& p1, ... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "class Solution {\npublic:\n long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {\n int n = nums1.size();\n vector<pair<int,int>> v; //{nums2,nums1}\n for(int i{}; i<n; i++){\n v.push_back({nums2[i],nums1[i]});\n }\n\n sort(v.begin(),v.end());\... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "class Solution {\npublic:\n bool static cmp(vector<int> &a, vector<int> &b){\n if(a[0]==b[0]) return a[1]<b[1];\n return a[0]<b[0];\n }\n long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {\n vector<vector<int>> v;\n for(int i=0; i<nums1.size(); i++){\n ... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "class Solution {\npublic:\n bool static cmp(vector<int> &a,vector<int> &b){\n if(a[0]==b[0])return a[1]<b[1];\n return a[0]<b[0];\n }\n long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {\n vector<vector<int>> v;\n for(int i = 0; i < nums1.size(); i++){\... |
2,636 | <p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p>
<p>For chosen indices <code>i<su... | 3 | {
"code": "class Solution {\npublic:\n long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {\n vector<vector<int>> help;\n for(int i=0;i<nums1.size();i++){\n help.push_back({nums1[i],nums2[i]});\n }\n sort(help.begin(),help.end(),[](vector<int>& a,vector<int>& b... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 0 | {
"code": "class Solution {\npublic:\n int par[(int)1e5+10];\n int size[(int)1e5+10];\n void make(int v){\n par[v] = v;\n size[v] = 1;\n }\n\n int Find(int v){\n if(par[v]==v) return v;\n return par[v] = Find(par[v]);\n }\n\n void Union(int a, int b){\n a = Find... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 0 | {
"code": "// class UnionFind {\n// private:\n// vector<int> parent, rank;\n\n// public:\n// UnionFind(int size) {\n// parent.resize(size);\n// rank.resize(size, 0);\n// for (int i = 0; i < size; i++) {\n// parent[i] = i;\n// }\n// }\n// int find(int x) {\n/... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 0 | {
"code": "class Solution {\npublic:\n vector<int> parent; // yeh parent vector hai, har node ka parent store karega\n vector<int> rank; // yeh rank vector hai, tree ka height track karega (rank ka use hota hai union by rank mein)\n\n // Yeh find function hai, jo parent ko dhundta hai aur path compression ... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 0 | {
"code": "// class Solution {\n// public:\n\n// vector<int> ans;\n// int find(vector<int>& parent, int x){\n// return parent[x] = (parent[x] == -1)? -1: find(parent, parent[x]);\n// }\n\n// void DSU(vector<int>& parent, vector<int>& rank, int a, int b){\n// a = find(parent, a);\n// ... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 0 | {
"code": "class Solution {\npublic:\n int findParent(vector<int>& parent, int x) {\n if (parent[x] == x) {\n return x;\n }\n return parent[x] = findParent(parent, parent[x]);\n }\n\n void unionSets(int x, int z, vector<int>& parent) {\n int xParent = findParent(parent,... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 0 | {
"code": "class Solution {\npublic:\n\n vector<long long>parent;\n vector<long long>siz;\n\n\n void make_parent(vector<long long> & parent){\n for(int i = 0; i < parent.size() ; i++){\n parent[i] = i;\n }\n }\n\n int find_par(int v){\n \n if(v =... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 0 | {
"code": "#define ll long long\n#include <vector>\n#include <unordered_map>\n\nusing namespace std;\n\nclass DisjointSetUnion {\nprivate:\n vector<ll> parent, componentSize;\npublic:\n DisjointSetUnion(ll n) { // Changed parameter name to n\n parent.resize(n);\n componentSize.resize(n, 1); // In... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 0 | {
"code": "#define ll long long\n#include <vector>\n#include <unordered_map>\n\nusing namespace std;\n\nclass DisjointSetUnion {\nprivate:\n vector<ll> parent, componentSize;\npublic:\n DisjointSetUnion(ll n) { // Changed parameter name to n\n parent.resize(n);\n componentSize.resize(n, 1); // In... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 0 | {
"code": "class Solution {\npublic:\n // time: O(n)\n // space: O(n)\n long long countPairs(int n, vector<vector<int>>& edges) {\n vector<int> root(n);\n for (int i = 0; i < n; ++i)\n root[i] = i;\n\n for (const auto& e : edges) {\n int r1 = findRoot(root, e[0]);\n... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 0 | {
"code": "class Solution {\nprivate:\n int parent(vector<int>& unv, int a) {\n while(unv[a] != a) a = unv[a];\n return a;\n }\n\n void unionNode(vector<int>& unv, int a, int b) {\n int pa = parent(unv, a);\n int pb = parent(unv, b);\n\n if (pa != pb) unv[pb] = pa;\n }\n... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 0 | {
"code": "class Solution {\npublic:\n \n vector<int> parent;\n vector<int> rank;\n\n int find (int x) {\n if (x == parent[x]) \n return x;\n\n return parent[x] = find(parent[x]);\n }\n\n void Union (int x, int y) {\n int x_parent = find(x);\n int y_parent = fi... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 0 | {
"code": "class Solution {\npublic:\n\n vector<int>parent;\n vector<int>rank;\n\n int find(int x)\n {\n if(x == parent[x] )\n return x;\n return parent[x]=find(parent[x]);\n }\n\n void unionset(int x,int y)\n {\n int x_parent=find(x);\n int y_parent=find(y)... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 0 | {
"code": "class DSU {\n vector<int> parent;\n vector<int> size;\n public:\n DSU(int n) {\n parent.clear();\n size.clear();\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 ... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 0 | {
"code": "class DSU {\n vector<int> parent;\n vector<int> size;\n public:\n DSU(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 find_parent(int a) {\n if(a == paren... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 0 | {
"code": "class DSU {\npublic:\n vector<int> parent, rank, size;\n \n DSU(int n) {\n parent.resize(n);\n rank.resize(n, 0);\n size.resize(n, 1); \n for (int i = 0; i < n; ++i) parent[i] = i;\n }\n \n int find(int x) {\n if (parent[x] != x) {\n parent[x]... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 0 | {
"code": "class Solution {\npublic:\n long long findpar(long long node, vector<long long>& parent) {\n if (node == parent[node]) {\n return node;\n }\n return parent[node] = findpar(parent[node], parent);\n }\n\n void uni(long long u, long long v, vector<long long>& parent, v... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 1 | {
"code": "class Solution {\nclass Disjoint {\n public:\n vector<int> rank, parent, size;\n Disjoint(int n) {\n rank.resize(n + 1, 0);\n parent.resize(n + 1);\n size.resize(n + 1, 1);\n for(int i = 0; i <= n; i++){\n parent[i] = i;\n ... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 1 | {
"code": "class Solution {\n vector<int> par;\n\n int find(int x) {\n if (par[x] < 0)\n return x;\n\n return par[x] = find(par[x]);\n }\n\n void uni(int x, int y) {\n int p1 = find(x), p2 = find(y);\n if (p1 == p2)\n return;\n if (p2 < p1)\n ... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 1 | {
"code": "class DSU {\npublic:\n vector<int> parent, size;\n DSU(int n) {\n parent.resize(n);\n size.resize(n, 1);\n iota(parent.begin(), parent.end(), 0);\n }\n\n int find(int u) { return u==parent[u] ? u : parent[u]=find(parent[u]); }\n\n void merge(int u, int v) {\n int ... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 1 | {
"code": "class DSU {\npublic:\n vector<int> size, parent;\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 int find(int root) {\n if (root == parent[root]) return root;\n return parent[root] = find(parent[root])... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 1 | {
"code": "class DisjointSet{\npublic:\n vector<int> parent,size;\n DisjointSet(int n){\n parent.resize(n+1,0);\n size.resize(n+1,0);\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 return node... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 1 | {
"code": "class DisjointSet{\npublic:\n vector<int> parent,size;\n DisjointSet(int n){\n parent.resize(n+1,0);\n size.resize(n+1,0);\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 return node... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 1 | {
"code": "class Solution {\npublic:\n vector<pair<int, int>> parents;\n int parentCount;\n\n pair<int, int> findParent(int a) {\n if (parents[a].first == -1 || parents[a].first == a) return {a, parents[a].second};\n parents[a] = findParent(parents[a].first);\n return parents[a];\n }... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 1 | {
"code": "class Disjoint{\n public:\n vector<int> parent , size ; \n\n Disjoint(int n ){\n parent.resize(n+1,0); \n size.resize(n+1, 1); \n\n for(int i =0 ;i<=n ; i++){\n parent[i]=i; \n }\n }\n\n int findupar(int node){\n if(parent[node]==node){\n ... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 1 | {
"code": "class Dsu\n{\n public:\n vector<int>parent;\n vector<long long>size; \n Dsu(int n)\n {\n parent.resize(n);\n size.resize(n);\n\n for(int i=0;i<n;i++) {parent[i]=i; size[i]=1;}\n }\n int Upar(int node)\n {\n if(parent[node]==node)\n return node;\n ... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 1 | {
"code": "class DisjointSet {\npublic:\n vector<int> rank, parent, size;\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 ... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 1 | {
"code": "class Solution {\npublic:\n\nint findPar(int a, vector<int>& par) {\n if (par[a] == a) return a;\n return par[a] = findPar(par[a], par);\n}\n\nvoid unio(int a, int b, vector<int>& par, vector<int>& rank) {\n int aa = findPar(a, par);\n int bb = findPar(b, par);\n if (rank[aa] >= rank[bb]) {\... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 1 | {
"code": "class Solution {\npublic:\n vector<int>parent;\n vector<int>rank;\n\n int find (int i){\n if (i == parent[i]) return i;\n return find(parent[i]);\n }\n\n void unionfind(int i, int j){\n i = find (i); j = find (j);\n if (i==j) return;\n if (rank[i]>rank[j]){... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 1 | {
"code": "class Solution {\npublic:\n vector<int> parent;\n vector<int> rank;\n int find(int p){\n if(p == parent[p]){\n return p;\n }\n return parent[p] = find(parent[p]);\n }\n void Union(int x , int y){\n int x_p = find(x);\n int y_p = find(y);\n ... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 1 | {
"code": "class Solution {\npublic:\n//toh dekh muje jitne bhi edges derakhi \n//mai phle unko pair krke union karlunga\n//theeke aise mai poor agraph components mai bat jaayega\n//aur unko respective aprent bhi ban jaayenge\n\n//ab kux ni parent banane ke baad \n// i will create a map jisme mera har component ke no... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 1 | {
"code": "class Solution {\npublic:\n vector<int> parent;\n vector<int> rank;\n int find(int p){\n if(p == parent[p]){\n return p;\n }\n return parent[p] = find(parent[p]);\n }\n void Union(int x , int y){\n int x_p = find(x);\n int y_p = find(y);\n ... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 1 | {
"code": "class Solution {\npublic:\n //Solving Using Dsu\n vector<int>parent;\n vector<int>rank;\n int find(int a){\n if(parent[a] == a){\n return a;\n }\n return parent[a] = find(parent[a]);\n }\n void Union(int a,int b){\n int parentA = find(a);\n in... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 1 | {
"code": "class DisjointSet\n{\npublic:\n vector<int> rank, parent,size;\n DisjointSet(int n)\n {\n rank.resize(n + 1, 0);\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 int findPar(int node)\n {\n i... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 1 | {
"code": "class Solution {\npublic:\nvoid dfs(int node,int n,vector<int>adj[],long long & totalcnt,long long & cnt, vector<int>&vis){\n vis[node]=1;\n cnt++;\n totalcnt++;\n for(auto i: adj[node]){\n if(!vis[i]){\n dfs(i,n,adj,totalcnt,cnt,vis);\n }\n }\n return;\n\n}\n lon... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 1 | {
"code": "#include <vector>\n\nclass Solution {\npublic:\n void dfs(int node, vector<int> adj[], vector<int>& vis, long &cnt) {\n vis[node] = 1;\n cnt++;\n for (auto it : adj[node]) {\n if (!vis[it]) {\n dfs(it, adj, vis, cnt);\n }\n }\n }\n \... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 1 | {
"code": "class Solution {\npublic:\nvector<int>v;\n void solve(int node,vector<int>&vis,vector<int>adj[],long long &cnt){\n vis[node]=1;\n cnt++;\n for(auto it:adj[node]){\n if(!vis[it]) solve(it,vis,adj,cnt);\n }\n }\n long long countPairs(int n, vector<vector<int>>&... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 1 | {
"code": "class DSU\n{\n public:\n vector<int>parent,size;\n DSU(int n)\n {\n for(int i=0;i<n;++i)\n {\n parent.push_back(i);\n size.push_back(1);\n }\n }\n \n int findparent(int node)\n {\n if(parent[node]==node)\n return node;\n ... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 1 | {
"code": "class Solution {\npublic:\n vector<int> parent;\n vector<int> rank;\n map<int,int> compCount;\n int find(int x){\n if(x == parent[x]){\n return x;\n }\n return parent[x]=find(parent[x]);\n }\n void Union(int x,int y){\n int parent_x= find(x);\n ... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 1 | {
"code": "class Solution {\npublic:\n void dfs(int node, vector<vector<int>>& adj, vector<int>& vis, int cod) {\n vis[node] = cod;\n for (auto x : adj[node]) {\n if (!vis[x])\n dfs(x, adj, vis, cod);\n }\n }\n\n long long countPairs(int n, vector<vector<int>>& ... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 1 | {
"code": "class Solution {\npublic:\n void dfs(int node, vector<vector<int>>& adj, vector<int>& vis, int cod) {\n vis[node] = cod;\n for (auto x : adj[node]) {\n if (!vis[x]) {\n dfs(x, adj, vis, cod);\n }\n }\n }\n\n long long countPairs(int n, vect... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 1 | {
"code": "class Solution {\npublic:\n void dfs(vector<vector<int>> &adj,vector<int>&vis,int i,long long &x){\n if(vis[i]==1) return;\n vis[i]=1;\n for(auto it:adj[i]){\n if(vis[it]==0) dfs(adj,vis,it,x);\n }\n x+=1;\n }\n long long countPairs(int n, vector<vecto... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 1 | {
"code": "class Solution {\npublic:\n\n void dfs(vector<vector<int>> &graph, vector<int> &vi, long long int &cnt, int cur){\n if(vi[cur] == 1){ return; }\n vi[cur] = 1;\n cnt++;\n for(int i=0;i<graph[cur].size();i++){\n dfs(graph, vi, cnt, graph[cur][i]);\n }\n }\n... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 1 | {
"code": "class Solution {\npublic:\n long long findCount(vector<vector<int>> &graph, vector<bool> &visited, int start) {\n if (visited[start]) {\n return 0;\n }\n\n visited[start] = true;\n int count = 1;\n for (int i =0; i<graph[start].size(); i++) {\n co... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 1 | {
"code": "class Solution {\npublic:\n long countPairs(int n, vector<vector<int>>& edges) {\n createGraph(n, edges);\n vector<bool> visited(n, false);\n int numberOfVisitedNodes = 0;\n long numberOfUnreachablePairsOfNodes = 0;\n\n for (int node = 0; node < n; ++node) {\n ... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 1 | {
"code": "class Solution {\npublic:\n void dfs(int node ,vector<int>&vis,vector<int>adj[],int comp){\n vis[node]=comp;\n for(auto it: adj[node]){\n if(vis[it]==0){\n dfs(it,vis,adj,comp);\n }\n }\n }\n long long countPairs(int n, vector<vector<int>>&... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 1 | {
"code": "class Solution {\npublic:\n void dfs(int node ,vector<int>&vis,vector<int>adj[],int comp){\n vis[node]=comp;\n for(auto it: adj[node]){\n if(vis[it]==0){\n dfs(it,vis,adj,comp);\n }\n }\n }\n long long countPairs(int n, vector<vector<int>>&... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 2 | {
"code": "class Solution {\npublic:\n long long countPairs(int n, vector<vector<int>>& edges) {\n vector<bool>vis(n, false);\n int count;\n long long ans = 0;\n\n vector graph(n, vector<int>());\n for(auto&i : edges) {\n graph[i[0]].push_back(i[1]);\n graph... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 2 | {
"code": "class Solution {\npublic:\n void dfs(int cur, vector<vector<int>>&adjList, vector<bool>&visited, long long &s){\n visited[cur] = true;\n s++;\n for(auto k : adjList[cur]){\n if(!visited[k]) dfs(k, adjList, visited, s);\n }\n }\n long long countPairs(int n, v... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 2 | {
"code": "class Solution {\npublic:\n void dfs(int node, vector<vector<int>> &adj, vector<bool> &visited, long long &connected){\n visited[node] = true;\n connected++;\n\n for(auto itr = adj[node].begin(); itr != adj[node].end(); itr++){\n if(!visited[*itr]){\n dfs(*... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 2 | {
"code": "#define ll long long\nclass Solution {\npublic:\n \n void dfs(vector<vector<int>>& adj, int curr, int currParent, int *chainPtr ,bool *visited, int chainNumber) {\n\n if(visited[curr]){\n return;\n }\n visited[curr] = true;\n chainPtr[curr] = chainNumber;\n\n ... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 2 | {
"code": "class Solution {\n void dfs (std::vector<std::vector<int>>& graph, std::vector<bool>& visited, int src, int& count) {\n visited[src] = true;\n for (int i : graph[src]) {\n if (!visited[i]) {\n std::cout << i << ' ';\n dfs(graph, visited, i, count);\... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 2 | {
"code": "class Solution {\n void dfs (std::vector<std::vector<int>>& graph, std::vector<bool>& visited, int src, int& count) {\n visited[src] = true;\n for (int i : graph[src]) {\n if (!visited[i]) {\n dfs(graph, visited, i, count);\n ++count; ... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 2 | {
"code": "class Solution {\npublic:\n void dfs(vector<int>* graph, int &cnt, vector<int> &vis, int u){\n cnt++;\n vis[u] = 1;\n for(auto v : graph[u]){\n if(vis[v] == 1) continue;\n dfs(graph,cnt,vis,v);\n }\n }\n long long countPairs(int n, vector<vector<i... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 2 | {
"code": "#define ll long long\nclass Solution {\npublic:\n long long dfs(int node,vector<int> graph[],vector<ll> &vis){\n vis[node] = 1;\n ll size = 1;\n for(auto child:graph[node]){\n if(vis[child] == 1) continue;\n size += dfs(child,graph,vis);\n }\n ret... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 2 | {
"code": "class Solution {\npublic:\n long long countPairs(int n, vector<vector<int>>& edges) {\n \n map<int, vector<int>> m1;\n\n for(int i=0; i<edges.size(); i++)\n {\n int first = edges[i][0];\n int second = edges[i][1];\n\n m1[first].push_back(secon... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 2 | {
"code": "class Solution {\npublic:\n\n vector<vector<int>> graph;\n bool visited[100000];\n int dfs(int vertice)\n {\n int ans=1;\n visited[vertice]=1;\n for(auto it:graph[vertice])\n {\n if(!visited[it]) ans+=dfs(it);\n }\n return ans;\n }\n lo... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 2 | {
"code": "class Solution {\npublic:\n void dfs(int x, vector<vector<int>>& adj_list, vector<int>& visited, long long & count){\n visited[x] = 1;\n count++;\n\n for(auto adj: adj_list[x])\n {\n if(!visited[adj])\n dfs(adj, adj_list, visited, count); \n ... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 2 | {
"code": "class Solution {\npublic:\n\n int dfs(int u, vector<vector<int>> &g, vector<int> &visit){\n if(visit[u])\n return 0;\n visit[u] = 1;\n int cnt = 1;\n for(auto &x : g[u]){\n if(!visit[x]){\n cnt += dfs(x, g, visit);\n } \n ... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 2 | {
"code": "class Solution {\npublic:\n void dfs(vector<int>&vis,int i,vector<long long>adj[],int &count){\n vis[i]=1;\n count++;\n for(auto it:adj[i]){\n if(!vis[it]) dfs(vis,it,adj,count);\n }\n }\n long long countPairs(int n, vector<vector<int>>& edges) {\n\n v... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 2 | {
"code": "class Solution {\npublic:\n long long countPairs(int n, vector<vector<int>>& edges) {\n for(int i = 0; i < n; i++){\n parent[i] = i;\n }\n for(auto edge : edges){\n int u = edge[0];\n int v = edge[1];\n uni(u, v);\n }\n unord... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 3 | {
"code": "// simple problem of dsu, just create a dsu, and define different connected\n// components, you have to focus on the no of elements in a connected component\n\nclass Solution {\npublic:\n int find(unordered_map<int, int>& parent, int val) {\n if (parent[val] != val) {\n parent[val] = f... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 3 | {
"code": "// simple problem of dsu, just create a dsu, and define different connected\n// components, you have to focus on the no of elements in a connected component\n\nclass Solution {\npublic:\n int find(unordered_map<int, int>& parent, int val) {\n if (parent[val] != val) {\n parent[val] = f... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 3 | {
"code": "class Solution {\npublic:\n class DSU {\n public:\n vector<int> parent;\n vector<int> size;\n int n;\n DSU(int n) {\n this->n = n;\n parent.resize(n, -1);\n size.resize(n, 1);\n make_set();\n }\n void make_set() {\n... |
2,403 | <p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u... | 3 | {
"code": "class Solution {\npublic:\n typedef long long ll;\n void dfs(int node, unordered_map<int,vector<int>>& m, ll& cnt, vector<int>& vis){\n vis[node] = 1;\n cnt++;\n for(auto& i : m[node]){\n if(vis[i]==0) dfs(i, m, cnt, vis);\n }\n }\n \n long long cou... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.