id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
3,491 | You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.
<p>A <span data-keyword="subsequence-array">subsequence</span> <code>sub</code> of <code>nums</code> with length <code>x</code> is called <strong>valid</strong> if it satisfies:</p>
<ul>
<li><code>(sub[0] + sub[1]... | 3 | {
"code": "class Solution {\npublic:\n int solve(vector<int>& nums,vector<vector<int>>&dp,int c,int &k,int s)\n {\n \n if(dp[c][s]!=-1)\n return dp[c][s];\n int ans{};\n for(int i{c+1};i<nums.size();i++)\n {\n if((nums[c]+nums[i])%k==s)\n {\n ... |
3,491 | You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.
<p>A <span data-keyword="subsequence-array">subsequence</span> <code>sub</code> of <code>nums</code> with length <code>x</code> is called <strong>valid</strong> if it satisfies:</p>
<ul>
<li><code>(sub[0] + sub[1]... | 3 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n for(int& x : nums) {\n x = x % k;\n }\n int n = nums.size();\n \n vector<vector<int>> cache(n+2, vector<int>(n+2, -1));\n \n auto dp = [&](auto& dp, int i, int prev, in... |
3,491 | You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.
<p>A <span data-keyword="subsequence-array">subsequence</span> <code>sub</code> of <code>nums</code> with length <code>x</code> is called <strong>valid</strong> if it satisfies:</p>
<ul>
<li><code>(sub[0] + sub[1]... | 3 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n std::map<int, std::map<int, int>> mmap2;\n int sz1 = nums.size();\n int ans = 0;\n for (int i = 0; i < sz1; ++i) {\n int t2 = nums[i] % k;\n\n for (std::pair<const int, int>& p :... |
3,491 | You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.
<p>A <span data-keyword="subsequence-array">subsequence</span> <code>sub</code> of <code>nums</code> with length <code>x</code> is called <strong>valid</strong> if it satisfies:</p>
<ul>
<li><code>(sub[0] + sub[1]... | 3 | {
"code": "#include <bits/stdc++.h>\nusing namespace std;\n\nclass Solution {\n public:\n int maximumLength(vector<int> &nums, int k) {\n for (auto &n : nums) {\n n %= k;\n }\n int ret = std::numeric_limits<int>::min();\n auto table = prepareTable(nums, k);\n for (int... |
3,491 | You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.
<p>A <span data-keyword="subsequence-array">subsequence</span> <code>sub</code> of <code>nums</code> with length <code>x</code> is called <strong>valid</strong> if it satisfies:</p>
<ul>
<li><code>(sub[0] + sub[1]... | 3 | {
"code": "class Solution {\npublic:\n /*\n longest subsequence that sub[i] + sub[i+1] % k is all the same\n\n store each values % k value? then\n\n dp[k] = first_mod , length of subsequence\n\n build subseqs that sum == i, make i with mods j and (i-j) % k\n */\n int maximumLength(vector<int>& nu... |
3,491 | You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.
<p>A <span data-keyword="subsequence-array">subsequence</span> <code>sub</code> of <code>nums</code> with length <code>x</code> is called <strong>valid</strong> if it satisfies:</p>
<ul>
<li><code>(sub[0] + sub[1]... | 3 | {
"code": "class Solution {\npublic:\n /*\n longest subsequence that sub[i] + sub[i+1] % k is all the same\n\n store each values % k value? then\n\n dp[k] = first_mod , length of subsequence\n\n build subseqs that sum == i, make i with mods j and (i-j) % k\n */\n int maximumLength(vector<int>& nu... |
3,491 | You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.
<p>A <span data-keyword="subsequence-array">subsequence</span> <code>sub</code> of <code>nums</code> with length <code>x</code> is called <strong>valid</strong> if it satisfies:</p>
<ul>
<li><code>(sub[0] + sub[1]... | 3 | {
"code": "class Solution {\npublic:\n typedef long long ll;\n\n int maximumLength(vector<int>& nums, int k) {\n \n for(int i=0; i<nums.size(); i++){\n nums[i] = nums[i]%k;\n }\n\n vector<vector<ll>>v(k+1, vector<ll>(k+1, 0));\n ll n = nums.size();\n ll ans =... |
3,491 | You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.
<p>A <span data-keyword="subsequence-array">subsequence</span> <code>sub</code> of <code>nums</code> with length <code>x</code> is called <strong>valid</strong> if it satisfies:</p>
<ul>
<li><code>(sub[0] + sub[1]... | 3 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n=nums.size();\n\n int ans=1;\n\n for (int j=0;j<k;j++){\n vector<int> end(k,0);\n vector<int> cnt(k,0);\n for (int i=0;i<n;i++){\n int x=nums[i]%k;\n ... |
3,491 | You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.
<p>A <span data-keyword="subsequence-array">subsequence</span> <code>sub</code> of <code>nums</code> with length <code>x</code> is called <strong>valid</strong> if it satisfies:</p>
<ul>
<li><code>(sub[0] + sub[1]... | 3 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n // k*k matrix\n // same approach as before\n auto ss = vector(k, vector<int>(k, 0));\n for (int i = 0; i < k; i++) ss[i][nums[0] % k] = 1;\n int ans = 0;\n auto pre = ss;\n for (i... |
3,491 | You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.
<p>A <span data-keyword="subsequence-array">subsequence</span> <code>sub</code> of <code>nums</code> with length <code>x</code> is called <strong>valid</strong> if it satisfies:</p>
<ul>
<li><code>(sub[0] + sub[1]... | 3 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n vector<vector<int>> v(k, vector<int> (k,-1));\n vector<vector<int>> ans(k, vector<int> (k,0));\n int a=0;\n\n for(int i:nums){\n for(int j=0;j<k;j++){\n if(v[i%k][j] == -1){\... |
3,491 | You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.
<p>A <span data-keyword="subsequence-array">subsequence</span> <code>sub</code> of <code>nums</code> with length <code>x</code> is called <strong>valid</strong> if it satisfies:</p>
<ul>
<li><code>(sub[0] + sub[1]... | 3 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n=nums.size();\n vector<vector<int>>rem(k+1,vector<int>(k+1,0));\n vector<vector<int>>prev(k+1,vector<int>(k+1,-1));\n for(int i=0;i<n;i++){\n int a=(nums[i]%k);\n // cout<<a... |
3,491 | You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.
<p>A <span data-keyword="subsequence-array">subsequence</span> <code>sub</code> of <code>nums</code> with length <code>x</code> is called <strong>valid</strong> if it satisfies:</p>
<ul>
<li><code>(sub[0] + sub[1]... | 3 | {
"code": "#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp>\nusing namespace __gnu_pbds;\nusing namespace std;\nusing ll=long long;\ntypedef tree <pair<ll,ll>, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;\n/*\n order_of_key (k)\n find_by_order(k)\n*/\ntemplat... |
3,491 | You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.
<p>A <span data-keyword="subsequence-array">subsequence</span> <code>sub</code> of <code>nums</code> with length <code>x</code> is called <strong>valid</strong> if it satisfies:</p>
<ul>
<li><code>(sub[0] + sub[1]... | 3 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n vector<vector<int>> rem(n, vector<int>(n));\n vector<vector<int>> dp(n, vector<int>(k,-1));\n vector<vector<int>> len(n, vector<int>(k,-1));\n \n \n for(int... |
3,491 | You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.
<p>A <span data-keyword="subsequence-array">subsequence</span> <code>sub</code> of <code>nums</code> with length <code>x</code> is called <strong>valid</strong> if it satisfies:</p>
<ul>
<li><code>(sub[0] + sub[1]... | 3 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n for(auto &it:nums){\n it=it%k;\n }\n int n=nums.size();\n vector<vector<int>> dp(1e3+1,vector<int>(k+1,0));\n int ans=0;\n for(int i=0;i<n;i++){\n for(int j=0;j<=k;j++... |
3,491 | You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.
<p>A <span data-keyword="subsequence-array">subsequence</span> <code>sub</code> of <code>nums</code> with length <code>x</code> is called <strong>valid</strong> if it satisfies:</p>
<ul>
<li><code>(sub[0] + sub[1]... | 3 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n\n int ans = 0;\n for (int f = 0; f < k; f++) {\n int last = -1;\n vector<int> lst(k, -1);\n vector<int> res(k);\n vector<int> cnt(k);\n\n ... |
3,491 | You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.
<p>A <span data-keyword="subsequence-array">subsequence</span> <code>sub</code> of <code>nums</code> with length <code>x</code> is called <strong>valid</strong> if it satisfies:</p>
<ul>
<li><code>(sub[0] + sub[1]... | 3 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n vector<vector<int>> v(k, vector<int> (k,-1));\n vector<vector<int>> ans(k, vector<int> (k,0));\n\n for(int i:nums){\n for(int j=0;j<k;j++){\n if(v[i%k][j] == -1){\n ... |
3,491 | You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.
<p>A <span data-keyword="subsequence-array">subsequence</span> <code>sub</code> of <code>nums</code> with length <code>x</code> is called <strong>valid</strong> if it satisfies:</p>
<ul>
<li><code>(sub[0] + sub[1]... | 3 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n unordered_map<int, int> dp[n];\n\n int maxLen = 0;\n for (int endInd = 0; endInd < n; endInd++) {\n for (int prevInd = 0; prevInd < endInd; prevInd++) {\n ... |
3,491 | You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.
<p>A <span data-keyword="subsequence-array">subsequence</span> <code>sub</code> of <code>nums</code> with length <code>x</code> is called <strong>valid</strong> if it satisfies:</p>
<ul>
<li><code>(sub[0] + sub[1]... | 3 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n vector<unordered_map<int,int>> dp(n);\n\n for(int i=1; i<n ; i++){\n for(int j=i-1; j>=0; j--){\n int rem = (nums[i] + nums[j])%k;\n \n if(dp... |
3,491 | You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.
<p>A <span data-keyword="subsequence-array">subsequence</span> <code>sub</code> of <code>nums</code> with length <code>x</code> is called <strong>valid</strong> if it satisfies:</p>
<ul>
<li><code>(sub[0] + sub[1]... | 3 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n if (n <= 2)\n return n;\n\n int ans = 2;\n vector<unordered_map<int, int>> dp(n);\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < i; j++) {\n... |
3,491 | You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.
<p>A <span data-keyword="subsequence-array">subsequence</span> <code>sub</code> of <code>nums</code> with length <code>x</code> is called <strong>valid</strong> if it satisfies:</p>
<ul>
<li><code>(sub[0] + sub[1]... | 3 | {
"code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n int ans = 1;\n \n // dp[i] stores the length of the longest valid subsequence that ends at index i\n vector<unordered_map<int, int>> dp(n);\n \n for (int i ... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "// #pragma GCC optimize(\"Ofast,unroll-loops\")\n// #pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nconst int N = 1e5;\n\nint deg[N], P[N], mx[N];\n\nclass Solution {\n\n public:\n int minimumDiameterAfterMerge(vector<vector<int>... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "#pragma GCC optimize(\"unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nconst int N = 1e5;\n\nint deg[N], P[N], mx[N];\n\nclass Solution {\n public:\n int minimumDiameterAfterMerge(vector<vector<int>>& edges1, vec... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "#pragma GCC optimize(\"Ofast,unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nconst int N = 1e5;\n\nclass Solution {\n int deg[N], P[N], mx[N];\n\n public:\n int minimumDiameterAfterMerge(vector<vector<int>>& edg... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "#pragma GCC optimize(\"Ofast,unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nint speedup = []{ cin.tie(0)->sync_with_stdio(0); return 0; }();\n\nconst int N = 1e5;\n\nclass Solution {\n int deg[N], P[N], mx[N];\n... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "#pragma GCC optimize(\"O3,unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nint speedup = []{ cin.tie(0)->sync_with_stdio(0); return 0; }();\n\nconst int N = 1e5;\n\nclass Solution {\n int deg[N], P[N], mx[N];\n pu... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "#pragma GCC optimize(\"Ofast,unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n#include <bits/stdc++.h>\n#define endl '\\n'\nusing namespace std;\nusing ll = long long;\n\nint speedup = []{ cin.tie(0)->sync_with_stdio(0); return 0; }();\n\nconst int N = 1e5;\n\nclass Solution {\n int de... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "//I was very wrong in before's attempt, took my 30 minutes wasted\n// The question is asking minimum diameter\n//I fully wrote the code for maximum diamter by any two nodes\n//This is truly truly sad af !!!\n\n/*\nWe will burn leafs from bottom to up simulteanously all end leafs\neach burned leaf will fetc... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "\n\nclass Solution {\npublic:\n \n int dp[100005][2];\n vector<int> g[100005];\n \n int ans = 0;\n void dfs(int cur, int fa) {\n for (auto x : g[cur]) {\n if (x == fa) continue;\n dfs(x, cur);\n if (dp[x][0] + 1 >= dp[cur][0]) {\n dp[... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "using Pii = pair<int, int>;\n\nconstexpr int MAX = 100010;\nconstexpr int INF = 0x3F3F3F3F;\n\nstruct Tree {\n int v;\n vector<int> adj[MAX];\n int dis[MAX], que[MAX];\n \n void Reset(int n) {\n v = n;\n for (int i = 0; i < n; ++i) {\n vector<int>().swap(adj[i]);\n }\n }\n \n void Add... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "constexpr int MAX = 100010;\n\nstruct Tree {\n int v, dis[MAX], que[MAX];\n std::vector<int> adj[MAX];\n \n void Reset(int n) {\n v = n;\n for (int i = 0; i < n; ++i) {\n std::vector<int>().swap(adj[i]);\n }\n }\n \n void AddEdge(int a, int b) {\n adj[a].push_back(b);\n adj[b].pu... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n int dia;\n vector<vector<int>> g;\n vector<int> dep;\n\n void dfs(int node, int par) {\n dep[node] = 0;\n int mx1 = 0, mx2 = 0;\n for (auto child : g[node]) {\n if (child == par) continue;\n dfs(child, node);\n in... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n int minimumDiameterAfterMerge(vector<vector<int>>& edges1, vector<vector<int>>& edges2) {\n int max_len_in_1 = treeDiameter(edges1);\n int max_len_in_2 = treeDiameter(edges2);\n int max_len = 1 + (max_len_in_1+1)/2 + (max_len_in_2+1)/2;\n return ma... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n int minimumDiameterAfterMerge(vector<vector<int>>& edges1, vector<vector<int>>& edges2) {\n int max_len_in_1 = treeDiameter(edges1);\n int max_len_in_2 = treeDiameter(edges2);\n int max_len = 1 + (max_len_in_1+1)/2 + (max_len_in_2+1)/2;\n return ma... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n int minimumDiameterAfterMerge(vector<vector<int>>& edges1, vector<vector<int>>& edges2) {\n int max_len_in_1 = treeDiameter(edges1);\n int max_len_in_2 = treeDiameter(edges2);\n int max_len = 1 + (max_len_in_1+1)/2 + (max_len_in_2+1)/2;\n return ma... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n int minimumDiameterAfterMerge(vector<vector<int>>& edges1, vector<vector<int>>& edges2) {\n int d1 = treeDiameter(edges1);\n int d2 = treeDiameter(edges2);\n return max({d1, d2, (d1 + 1) / 2 + (d2 + 1) / 2 + 1});\n }\n\n int treeDiameter(vector<vect... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n\nint bfs(vector<int> adj[], int n) {\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;\n q.push({0, 0});\n vector<int> ans(n, 1e8);\n ans[0] = 0;\n\n while (!q.empty()) {\n auto it = q.top();\n int wt = it.first;\n ... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n int bfs(vector<int> adj[],int n)\n {\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;\n q.push({0,0});\n vector<int> ans(n,1e8);\n ans[0]=0;\n while(!q.empty())\n {\n auto it=q.top();\n ... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "// diameter = sum of the two of the largest height siblings - 1\n// res = (diameter1 + 1) / 2 + (diameter2 + 1) / 2 + 1;\n\nclass Solution {\npublic:\n // Similar to diameter of binary tree\n int dfs(int u, int par, vector<int> adj[], int& res) {\n int largest = 0, secondLargest = 0;\n ... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n int dfs(int node, int parent,vector<int>adj[],int &maxi){\n int height1=0,height2=0;\n for(auto &neighbour:adj[node]){\n if(neighbour!=parent){\n int height=dfs(neighbour,node,adj,maxi);\n if(height>=height1){\n ... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n\n // int dfs(int i, int parent, vector<int>adj[]){\n // int ans = 0;\n // for(int x : adj[i]){\n // if(x!=parent){\n // 1+dfs(x,i,adj));\n // }\n // }\n // return ans;\n // }\n\n // Function to perform... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\nint visited[100009];\npublic:\n int minimumDiameterAfterMerge(vector<vector<int>>& edges1, vector<vector<int>>& edges2) {\n int n = edges1.size() + 1;\n int m = edges2.size() + 1;\n vector<int> tree1[n];\n vector<int> tree2[m];\n for(int i = 0; i < ed... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "using Pii = pair<int, int>;\n\nconstexpr int MAX = 100010;\nconstexpr int INF = 0x3F3F3F3F;\n\nstruct Tree {\n vector<int> adj[MAX];\n \n void Reset(int n) {\n for (int i = 0; i < n; ++i) {\n vector<int>().swap(adj[i]);\n }\n }\n \n void AddEdge(int a, int b) {\n adj[a].push_back(b);\n ... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n pair<int, int> dfs(int node, int parent, vector<int> adj[], int &maxDist) {\n ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);\n pair<int, int> farthest = {0, node}; // {distance, node}\n\n for (int neighbor : adj[node]) {\n if (n... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n\n pair<int,int>findExtremeNode(int i, int parent, vector<int>adj[]){\n pair<int,int>node_dist = {i,0};\n\n for(int x : adj[i]){\n if(x!=parent){\n pair<int,int>curr = findExtremeNode(x,i,adj);\n curr.second++;\n ... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n int f[100005][2];\n vector<int>e1[100005], e2[100005];\n\n int max_dep = 0;\n int node = 1;\n void DFS1(int root, int par, vector<int> e[], int dep) {\n if(dep > max_dep) {\n max_dep = dep;\n node = root;\n }\n for(auto i... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n vector<int> adj1[100005], adj2[100005];\n int d1 = 0, d2 = 0;\n int maxD(int cur, int par, vector<int> adj[], int &dx){\n // cout<<cur<<endl;\n int x = 0, y = 0;\n for(int i = 0; i<adj[cur].size(); i++){\n if(adj[cur][i] != par){\n ... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n map <int ,int > l;\n int best;\n int node;\n int dfs(int v, bool* visited, vector <int> graph[], int c = 0){\n visited[v] = true;\n int ans = 0;\n for(int i = 0; i < graph[v].size(); i++){\n if(!visited[graph[v][i]])ans = max(ans,dfs(g... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n map <int ,int > l;\n int best;\n int node;\n int dfs(int v, bool* visited, vector <int> graph[], int c = 0){\n visited[v] = true;\n int ans = 0;\n for(int i = 0; i < graph[v].size(); i++){\n if(!visited[graph[v][i]])ans = max(ans,dfs(g... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\n vector<int> go[100005];\n int dis[100005];\n int calc(vector<vector<int>>& edges){\n int n = edges.size()+1;\n for(int i=0;i<n;i++) go[i].clear();\n for(auto e:edges){\n go[e[0]].push_back(e[1]);\n go[e[1]].push_back(e[0]);\n }\n... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n\n void dfs(int node, int par, int distVal, vector<int>& dist, vector<int> adj[]) {\n dist[node] = distVal;\n\n for(auto child: adj[node]) {\n if(child != par) {\n dfs(child, node, distVal+1, dist, adj);\n }\n }\n }\... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n void dfs(int root, int parent, vector<int> &level, vector<int> adj[]){\n \n for(auto it : adj[root]){\n if(it==parent) continue;\n\n level[it]=level[root]+1;\n dfs(it, root, level, adj);\n }\n }\n\n int minimumDiamet... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n pair<int,int>bfs(int src,vector<int>adj[],int n) {\n queue<int>q;\n q.push(src);\n vector<int>distance(n,1e9);\n distance[src]=0;\n while(!q.empty()) {\n auto node=q.front();\n q.pop();\n\n for(auto &child:ad... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n int dist(int node, vector<int> list[], int n){\n queue<int> q;\n q.push(node);\n int d = 0;\n vector<int> vis(n);\n vis[node] = 1;\n while(q.size()>0){\n int x = q.size();\n d++;\n for(int i = 0;i<x;i+... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\nvoid dfs(int node,int par,int n,int mx[n][2],vector<int> adj[]){\n for(auto x:adj[node]){\n int a=node;\n int b=x;\n if(a==node){\n if(b!=par){\n dfs(b,a,n,mx,adj);\n int c=mx[b][0]+1;\n ... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\n int dfs(vector<int> adj[] , int node , vector<int>& vis){\n vis[node] = 1;\n int path = 0;\n for(auto it : adj[node]){\n if(!vis[it]){\n path = max(path , dfs(adj , it , vis));\n }\n }\n return 1 + path;\n }\n ... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n vector<int> bfs(int i,int n, vector<int> adj[])\n {\n vector<int> vis(n,0);\n int ret= 0;\n int dis =0;\n queue<int> q;\n q.push(i);\n vis[i]=1;\n while(!q.empty())\n {\n int sz = q.size();\n ret... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\nprivate:\n vector<vector<int>> adjList;\n vector<int> visited, distances;\n\n void dfs(int node, int dist) {\n if (visited[node]) return;\n\n distances[node] = dist;\n visited[node] = 1;\n for (int neighbor : adjList[node]) {\n dfs(neighbor,... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n int minimumDiameterAfterMerge(vector<vector<int>>& edges1, vector<vector<int>>& edges2) {\n const auto get_min_height = [](vector<vector<int>>& edges) -> pair<int, int> {\n int n = edges.size() + 1;\n if (n == 1) {\n return {0, 0};\... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n\n void solve(vector<int>adj[],vector<int>&visited,int &node,int dist,int &res,int i){\n visited[i]=true;\n\n\n for(auto it:adj[i]){\n if(!visited[it]){\n solve(adj,visited,node,dist+1,res,it);\n }\n }\n\n if(res... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n pair<int,int> bfs(int node, vector<int>& vis, vector<int> adj[]) {\n queue<pair<int,int>> q;\n q.push({node, 0});\n vis[node] = 1;\n int maxi = -1;\n int ans = 0;\n \n while (!q.empty()) {\n auto it = q.front();\n ... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n vector<int>g1[100001];\n vector<int>g2[100001];\n typedef long long ll;\n\n ll dfs1(vector<int> g[], int u, int p, vector<ll>&dis){\n \n ll ans = -1;\n\n for(int i=0; i<g[u].size(); i++){\n int v = g[u][i];\n\n if(v == p){\n... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n int minimumDiameterAfterMerge(vector<vector<int>>& edges1, vector<vector<int>>& edges2) {\n int diameter1 = getDiameter(edges1);\n int diameter2 = getDiameter(edges2);\n return std::max(\n std::max(diameter1, diameter2), \n (diameter... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n int maxDia=0;\n \n // topological sort to find center of graph\n int topo(vector<vector<int>>&edges){\n int n=edges.size(),dia=0;\n vector<int>deg(n+1,0);\n vector<vector<int>>graph(n+1);\n \n for(int i=0;i<n;i++){\n deg[... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\n int calDiameter(vector<int> adj_list[],int n){\n queue<pair<int,int>> q;\n q.push({0,-1});\n int lastNode = 0;\n while(!q.empty()){\n int node = q.front().first;\n int par = q.front().second;\n lastNode = node;\n ... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n void get(vector<vector<int>> &g, int &mi,int &ma,vector<int> °){\n \n int n = deg.size();\n if(n==1){\n mi = 0;\n ma = 0;\n return;\n }\n queue<int> q;\n for(int i=0;i<n;i++){\n if(deg[i]... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> adj1;\n vector<vector<int>> adj2;\n int d1=0;\n int d2=0;\n int solve1(int node,int par){\n\n int mx = 0;\n int mx1 = 0;\n for(auto& e:adj1[node]){\n if(e==par)continue;\n int t = solve1(e,node);\n ... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> tree1;\n vector<vector<int>> tree2;\n int maxdist;\n int farthest;\n\n void dfs(vector<vector<int>>& adj, int node, int parent, int dist) {\n if (dist > maxdist) {\n maxdist = dist;\n farthest = node;\n }\n ... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "#pragma GCC optimize (\"O3\")\n#pragma GCC optimize (\"unroll-loops\")\n#pragma GCC optimize (\"no-stack-protector\")\n#pragma GCC optimize (\"unswitch-loops\")\n#pragma GCC target (\"avx2,popcnt\")\nclass Solution {\npublic:\n vector<vector<int>> tree1;\n vector<vector<int>> tree2;\n int maxdist;... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "// https://leetcode.cn/problems/find-minimum-diameter-after-merging-two-trees/solutions/2826597/shu-dp-or-bfs-by-tsreaper-kkp1/\nclass Solution {\npublic:\n int minimumDiameterAfterMerge(std::vector<std::vector<int>>& edges1, std::vector<std::vector<int>>& edges2) {\n // 建图\n auto build = ... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n int diameter(vector<vector<int>> &edge){\n vector<vector<int>> g(edge.size()+1);\n for(auto&e :edge){\n int x=e[0],y=e[1];\n g[x].push_back(y);\n g[y].push_back(x);\n }\n int res=0;\n auto dfs=[&](auto&& dfs,... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n int farthest, d;\n void dfs(int c, int p, int dist, vector<vector<int>> &r) {\n if (r.size() == 1) return;\n if (dist > d) d = dist, farthest = c;\n for (const auto &adj: r[c]) if (adj - p) dfs(adj, c, dist + 1, r);\n }\n int diameter(vector<vect... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n int farthest, d;\n void dfs(int c, int p, int dist, vector<vector<int>> &r) {\n if (r.size() == 1) return;\n if (dist > d) d = dist, farthest = c;\n for (const auto &adj: r[c]) if (adj - p) dfs(adj, c, dist + 1, r);\n }\n int diameter(vector<vect... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n int maxNode(vector<vector<int>>& edges){\n int maxNode = 0;\n for(auto& edge: edges){\n maxNode = max(max(edge[0], edge[1]), maxNode);\n }\n return maxNode;\n }\n\n void buildTree(vector<vector<int>> &edges, vector<vector<int>> &tr... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n pair<int,int> ending(int start,int n,vector<vector<int>>&adj){\n vector<bool>vis(n,0);\n queue<int>q;\n q.push(start);\n vis[start]=1;\n int level=-1;\n pair<int,int>ans;\n ans.first=start;\n ans.second=level;\n w... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution\n{\npublic:\n pair<int,int>get_far_point_dist(vector<vector<int>>&adj,int st)\n {\n int n=adj.size();\n vector<bool>vis(n);\n int dist=-1,pt=st;\n queue<int>q;\n q.push(st);\n vis[st]=true;\n while(!q.empty())\n {\n int... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n\n int dfs(int node, int parent, vector<vector<int>>& graph, int& diameter){\n // longestSubtreePath1 and 2 store the top 2 longest paths from all possible subtrees\n int longestSubtreePath1 = 0, longestSubtreePath2 = 0;\n // now we traverse all the subtre... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n\n int dfs(int node, int parent, vector<vector<int>>& graph, int& diameter){\n // longestSubtreePath1 and 2 store the top 2 longest paths from all possible subtrees\n int longestSubtreePath1 = 0, longestSubtreePath2 = 0;\n // now we traverse all the subtre... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\nprivate:\n int DFS(int u , int pu , int &maxi , vector<vector<int>> &Graph){\n int H1 = 0 , H2 = 0;\n for(int v : Graph[u]){\n if(v != pu){\n int h = DFS(v , u , maxi , Graph);\n if(h >= H1) H2 = H1 ... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 0 | {
"code": "class Solution {\npublic:\n int dfs(int node,int parent,int& maxi,vector<vector<int>>&graph)\n {\n int h1=0,h2=0;\n for(auto it:graph[node])\n {\n if(it==parent)\n continue;\n int h=dfs(it,node,maxi,graph);\n if(h>=h1)h2=h1,h1=h;\n ... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 1 | {
"code": "class Solution {\npublic:\n void make_graph(vector<vector<int>>&graph, vector<vector<int>>&edges){\n for(auto& e: edges){\n graph[e[0]].push_back(e[1]);\n graph[e[1]].push_back(e[0]);\n }\n }\n\n int get_furthest_node(vector<vector<int>>&graph){\n queue<i... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 1 | {
"code": "class Solution {\n void dfs(int u, int fa, vector<int>& dp, vector<vector<int>>& G, int& d) {\n for (auto& v : G[u]) {\n if (v == fa) {\n continue;\n }\n dfs(v, u, dp, G, d);\n d = max(d, dp[u] + 1 + dp[v]);\n dp[u] = max(dp[u]... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 1 | {
"code": "class Solution {\npublic:\n/*\n find farthest node distance for all nodes in the trees.\n For merge, take minimum of above value in both trees, resultant distance is min(tree1) + min(tree2) + 1\n answer = max(diameter of tree 1, diameter of tree 2, min(tree1) + min(tree2) + 1)\n\n optimisation... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 1 | {
"code": "// const int N = 1e5+5;\n// vector<int> vis(N);\n// int dis = 0;\n// int ind = 0;\n\npair<int,int> md(-1,-1);\n\nvector<vector<int>> g;\n\nvoid dfs(int x,int f= -1, int d = 0){\n md = max(md,make_pair(d,x));\n for(auto v: g[x]){\n if(v!=f){\n dfs(v,x,d+1);\n }\n }\n}\n\nin... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 1 | {
"code": "class Solution {\npublic:\n int minimumDiameterAfterMerge(vector<vector<int>>& edges1, vector<vector<int>>& edges2) {\n auto diameter = [] (vector<vector<int>>& edges) -> int {\n int n = edges.size();\n vector<vector<int>> nexts(1 + n);\n for (auto& edge : edges) ... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 1 | {
"code": "class Solution {\npublic:\n \n int func(int node,int par,int &maximum,vector<int>adj[]){\n int dia=0;\n int maxi=0, secMaxi=0;\n for(auto it:adj[node]){\n if(it==par) continue;\n dia= func(it,node,maximum,adj);\n if(dia>=maxi){\n secMaxi=maxi;\n ... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 1 | {
"code": "class Solution {\npublic:\n void makeGraph(vector<vector<int>> &edges, vector<int> adj[]){\n for(auto it : edges){\n int u = it[0];\n int v = it[1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n }\n\n int getFurNode(vector<int> adj[],... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 1 | {
"code": "class Solution {\npublic:\n int n;\n int diameterFind(int n,int node,vector<int>adj[],int &ans){\n\n queue<int>q;\n q.push(node);\n \n vector<int>vis(n+1,0);\n vis[node]=1;\n int d=0,far=node;\n\n while(!q.empty()){\n int sz=q.size();\n\n ... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 2 | {
"code": "class Solution {\nprivate:\nint extreme_node;\nint max_depth=INT_MIN;\nvoid dfs(int i,int p, vector<vector<int>>&adj, int depth){\n if(max_depth<depth){\n extreme_node=i;\n max_depth=depth;\n }\n for(auto a: adj[i]){\n if(a==p)continue;\n dfs(a,i,adj,depth+1);\n }\n}... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 2 | {
"code": "class Solution {\npublic:\n\n void dfs(vector<vector<int>>& adj, vector<bool>& v, int x, vector<int>& d){\n v[x] = true;\n for(auto y: adj[x]){\n if(!v[y]){\n d[y] = d[x]+1;\n dfs(adj, v, y, d);\n }\n }\n }\n\n int findDiamet... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 2 | {
"code": "class Solution {\npublic:\n int find_center(vector<vector<int>>&adj,vector<int>&indeg ){\n int n = indeg.size();\n vector<bool>vis(n,0);\n \n\n queue<int>q;\n for(int i=0;i<n;i++){\n if(indeg[i]<=1){q.push(i);vis[i]=1;}\n }\n\n int last=-1;\n ... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 2 | {
"code": "class Solution {\npublic:\n void dfs(int u, vector<vector<int>>& adl, vector<int>& dis) {\n for(auto v: adl[u]) {\n if(dis[v] == -1) {\n dis[v] = dis[u] + 1;\n dfs(v,adl,dis);\n }\n }\n }\n \n int diameter(vector<vector<int>>& ad... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 2 | {
"code": "class Solution {\npublic:\n void fnc(int node, int par, vector<vector<int>> &adj, vector<int> &height, vector<int> &depth){\n for(auto child: adj[node]){\n if(child==par) continue;\n fnc(child, node, adj, height, depth);\n // cout<<node<<\" \"<<height[node]<<\" \"... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 2 | {
"code": "class Solution {\npublic:\n int findDiameter(int vertex, int par, vector<int> &diameter, vector<int> graph[], vector<vector<int>> &branch){\n for(auto &child:graph[vertex]){\n if(child == par)\n continue;\n\n int dia = findDiameter(child, vertex, diameter, gra... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 2 | {
"code": "int CeilDivision(int n, int d) {\n return (n + d - 1) / d;\n}\n\nint FindRadius(vector<vector<int>>& g) {\n vector<int> in(g.size(), 0);\n vector<vector<int>> longest(g.size(), vector<int>(2));\n vector<bool> visited(g.size(), 0);\n queue<int> q;\n for (int i = 0; i < g.size(); ++i) {\n auto& edge... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 3 | {
"code": "class Solution {\npublic:\n int dfs(int u,vector<vector<int>>& adj,vector<bool>& vis){\n int tempans=0;\n vis[u]=true;\n for(auto v:adj[u]){\n if(!vis[v]){\n vis[v]=true;\n tempans=max(tempans,dfs(v,adj,vis));\n }\n }\... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 3 | {
"code": "class Solution {\n struct Node {\n int depth;\n int curr;\n };\n vector<set<int>> v1,v2;\n\n void findFurthestNode(int x,vector<set<int>>& v, int depth, bitset<100000>& vis, Node& node){\n if(vis[x])return;\n vis[x]=true;\n for(auto&i:v[x]){\n findF... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 3 | {
"code": "class Solution {\npublic:\n int diameter(vector<vector<int>>& edges) {\n int n = edges.size() + 1;\n vector<set<int>> graph(n);\n for (const auto& edge : edges) {\n graph[edge[0]].insert(edge[1]);\n graph[edge[1]].insert(edge[0]);\n }\n vector<int... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 3 | {
"code": "const int MAXN = 1e5 + 100;\nstruct Cal {\n set<int> G[MAXN];\n int d[MAXN];\n int c;\n int diameter;\n\n int calDiameter() {\n c = 0;\n dfs(0, -1);\n d[c] = 0;\n dfs(c, -1);\n return d[c];\n }\n\n void dfs(int u, int fa) {\n for (int v : G[u]) {\n if (v == fa) continue;\n ... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> makegrp(vector<vector<int>> edges){\n int n=edges.size();\n vector<vector<int>> adj(n+1);\n for(auto x:edges){\n adj[x[0]].push_back(x[1]);\n adj[x[1]].push_back(x[0]);\n }\n return adj;\n }\n ... |
3,439 | <p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>... | 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> createGraph(vector<vector<int>> edges)\n {\n vector<vector<int>> adjList(edges.size()+1);\n\n for(auto edge:edges)\n {\n adjList[edge[0]].push_back(edge[1]);\n adjList[edge[1]].push_back(edge[0]);\n }\n\... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.