id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\n \n int compute(int res, vector<vector<int>> &groups,vector<int> &req,int ones,int mc) {\n \n int i;\n for(i=0;i<groups.size();i++) {\n if(groups.size() == 1){\n continue;\n }\n int r = req[i];\n\n bool ...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n int minFlips(vector<vector<int>>& grid) {\n int m = grid.size(), n = grid[0].size();\n vector<vector<int>> arr(m, vector<int>(n));\n \n int ans = 0;\n \n int x = ceil(m/2.0), y = ceil(n/2.0);\n // int ones = 0;\n \n ...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "const int N = 200010;\nint f[N], g1[N][4], g2[N][4];\n\nclass Solution {\npublic:\n int minFlips(vector<vector<int>>& grid) {\n return solve(grid) + get(grid);\n }\n\n int solve(vector<vector<int>>& grid) {\n int m = grid.size(), n = grid[0].size();\n int l = 0, r = m - 1;\n ...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n\n vector<int> getSingle(vector<int> &aa,int n){\n int x=0;\n int y=0;\n int p=0,q=n-1;\n while(p<q){\n if(aa[p]==aa[q]){\n if(aa[p]==1)x+=2;\n }else{\n y++;\n }\n p++;\n ...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define ll long long\n#define ull unsigned long long\n#define For(i, a, n) for(ll i = a; i <= n; i++)\n#define rep0(i,n) for(ll i = 0; i < n; i++)\n#define rep1(i,n) for(ll i = 1; i <= n; i++)\n#define rrep0(i, n) for(ll i = n-1; i >= 0; i--)\n#define rrep1...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "\npair<int, int> operator+=(pair<int, int>& lhs, pair<int, int>& rhs) {\n lhs.first += rhs.first;\n lhs.second += rhs.second;\n\n return lhs;\n};\n\nclass Solution {\npublic:\n int minFlips(vector<vector<int>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n vect...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n int minFlips(vector<vector<int>>& grid) {\n int row = grid.size();\n int col = grid[0].size();\n int diff=0, rems=0;\n int ans =0;\n for(int i=0; i< ((row+1)/2); i++)\n {\n for(int j=0; j<((col+1)/2); j++)\n {\n ...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\n int dp[100005][4];\n int f(int id,int ct1,vector<vector<int>>&v){\n int syz = v.size();\n if(id >= syz){\n if(ct1 == 0)return 0;\n return 1e8;\n }\n\n if(dp[id][ct1] != -1)return dp[id][ct1];\n\n int m = v[id].size();\n\n ...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n int n, m;\n\n int solve(int i, int j, int rem, vector<vector<vector<int>>>& dp, vector<vector<int>>& grid) {\n if (i == (n + 1) / 2) {\n if (rem == 0) return 0;\n return 1e6; \n }\n if (dp[i][j][rem] != -1) return dp[i][j][rem];\n...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n int n, m;\n\n int solve(int i, int j, int rem, vector<vector<vector<int>>>& dp, vector<vector<int>>& grid) {\n if (i == (n + 1) / 2) {\n if (rem == 0) return 0;\n return 1e6; \n }\n if (dp[i][j][rem] != -1) return dp[i][j][rem];\n...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n int minchanges(vector<vector<int> > mat) \n{ \n // Rows in the matrix \n \n int N = mat.size(); \n \n // Columns in the matrix \n int M = mat[0].size(); \n \n int i, j, ans = 0, x; \n map<int, int> mp; \n // Traverse the given matrix \n for (i = 0...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n\n\nint minchanges(vector<vector<int> > mat) \n{ \n // Rows in the matrix \n int N = mat.size(); \n int o = 0,oc=0,k=0;\n // Columns in the matrix \n int M = mat[0].size(); \n \n int i, j, ans = 0, x; \n map<int, int> mp; \n \n for (i = 0; i < N / 2; i++...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n int M, N;\n int count1(vector<vector<int>>& grid){\n int cnt = 0;\n for(int i = 0; i < M; i++){\n for(int j = 0; j < N; j++){\n cnt += grid[i][j];\n }\n }\n return cnt;\n }\n int minFlips(vector<vector<...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n \n long long rec(int idx, vector<pair<int,int>> & vp, vector<vector<long long>> & dp, int fac)\n {\n if (idx==vp.size())\n {\n if (fac==0) return 0;\n return 1e7;\n }\n if (dp[idx][fac]!=-1) return dp[idx][fac];\n ...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n int minFlips(vector<vector<int>>& grid) {\n int res = 0;\n int m = grid.size(), n = grid[0].size();\n vector<vector<int>> grs;\n for (int i = 0; i <= m / 2; i++) {\n for (int j = 0; j <= n / 2; j++) {\n if (m % 2 == 0 && i...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "using vi = vector<int>;\nusing vvi = vector<vi>;\nclass Solution {\npublic:\n int minFlips(vector<vector<int>>& grid) {\n int m = (int)grid.size(), n = (int)grid[0].size();\n int ans = 0, cnt = 0, opt = 0;\n for (int i = 0;i<=(m - 1)/2;i++){\n for (int j = 0;j<=(n - 1)/2;...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n void walk(int i, int j, vector<vector<int>>& grid, vector<int>& cts)\n {\n int val = grid[i][j];\n cts[val]++;\n grid[i][j] = 2;\n int m = grid.size();\n int n = grid[0].size();\n int ii = m-1-i;\n int jj = n-1-j;\n i...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n int getans(int ind, int sum, vector<pair<int,int>>&vp, vector<vector<int>>&dp){\n if(ind >= vp.size()){\n if(sum == 0){\n return 0;\n }\n return 1e9;\n }\n\n if(dp[ind][sum] != -1){\n return dp[in...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n int minFlips(vector<vector<int>>& grid) {\n int n = grid.size(); \n double x = 3.14; \n string str = \"Hello\"; \n char ch = 'A'; \n float y = 2.718;\n \n int m = grid[0].size(); \n long l = 100000L; \n bool flag ...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n int minFlips(vector<vector<int>>& grid) {\n int ans=0;\n int n=grid.size(),m=grid[0].size();\n map<pair<int,int>,int> ma[2];\n for(int i = 0;i<grid.size();i++){\n for(int j = 0;j<grid[0].size();j++){\n int x=min(i,n-i-1),y...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n int minFlips(vector<vector<int>>& a) {\n int n=a.size();\n int m=a[0].size();\n if(n*m<=3){\n int cnt1=0;\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n if(a[i][j]==1)\n cnt1++;...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "\n class Disjoint_Set_Union\n {\n public:\n vector<int> size, rank, parent, edges , one;\n \n Disjoint_Set_Union(int n)\n {\n size.resize(n + 1), rank.resize(n + 1), parent.resize(n + 1), edges.resize(n + 1) , one.resize(n+1);\n for (int i = 0; i <= n; i++)\n size[i]...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n int dfs(vector<pair<int,int>>&v,int inx,int div,vector<vector<int>>&dp){\n if(inx==v.size()){\n if(div==0){\n return 0;\n }\n return 1e7;\n }\n if(dp[inx][div]!=-1){\n return dp[inx][div];\n ...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n #define ll long long\n ll fun(int id, vector<pair<ll, ll>> &vp, ll cnt,unordered_map<string, ll>& memo){\n if(id == vp.size()){\n return (cnt % 4 == 0)?0 : INT_MAX;\n }\n string key = to_string(id) + \",\" + to_string(cnt);\n if (memo...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution \n{\npublic:\n vector<vector<int>> f(vector<vector<int>>& matrix) \n {\n int n = matrix.size();\n int m = matrix[0].size();\n int flips = 0;\n vector<vector<int>> cells;\n for (int i = 0; i <= (n - 1) / 2; ++i) \n {\n for (int j = 0;...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "#ifndef ATCODER_DSU_HPP\n#define ATCODER_DSU_HPP 1\n\n#include <algorithm>\n#include <cassert>\n#include <vector>\n\nnamespace atcoder {\n\n// Implement (union by size) + (path compression)\n// Reference:\n// Zvi Galil and Giuseppe F. Italiano,\n// Data structures and algorithms for disjoint set union prob...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n int cnt=0;\n int cnt1=0;\n int n,m;\n void dfs(int x,int y,vector<vector<int>>& grid,vector<vector<int>>&vis)\n {\n vis[x][y]=1;\n cnt++;\n if(grid[x][y]==1)\n {\n cnt1++;\n }\n if(!vis[x][m-1-y])\n {\n ...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n int minFlips(vector<vector<int>>& grid) {\n using pii = pair<int,int>;\n int n=grid.size();\n int m=grid[0].size();\n function<vector<pii>(int,int)> pts;\n pts = [&] (int x,int y){\n vector<pii> res;\n res.push_back({x,...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n int minFlips(vector<vector<int>>& grid) {\n // grid[i][j]==1 --> grid[i][m-j-1]==1, grid[n-i-1][j], grid[n-i-1][m-j-1]]\n int flipCount = 0;\n int nonPairCount = 0;\n int pairCount = 0;\n\n int m = grid[0].size();\n int n = grid.size(...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n vector<pair<int,int>>vp;\n vector<vector<int>>dp;\n int fun(int i,int o){\n int len=vp.size();\n if(i==len){\n if(o==0){\n return 0;\n }\n else {\n return 1e9;\n }\n }\n ...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n\n vector<vector<int>> dp;\n\n int minFlips(vector<vector<int>>& grid) {\n vector<vector<int>> vis;\n int m = grid.size(),n = grid[0].size();\n for(int i = 0;i<m;i++){\n vis.push_back({});\n for(int j = 0;j<n;j++) vis[i].push_back(...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "const int N = 2e5 + 10;\n\nint dp[N][4];\n\nclass Solution {\n \n int f(vector<pair<int, int>> &arr, int pos, int ones)\n {\n if(pos == arr.size())\n {\n return ones == 0 ? 0 : 1e9;\n }\n \n \n int &ans = dp[pos][ones]; if(ans != -1) return ans;...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "const int N = 2e5 + 10;\n\nint dp[N][4];\n\nclass Solution {\n \n int f(vector<pair<int, int>> &arr, int pos, int ones)\n {\n if(pos == arr.size())\n {\n return ones == 0 ? 0 : 1e9;\n }\n \n \n int &ans = dp[pos][ones]; if(ans != -1) return ans;...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "\nvector<vector<pair<int,int>>> par;\nvoid make(pair<int,int> u){\n par[u.first][u.second]= u;\n}\npair<int,int> find(pair<int,int> u){\n if(par[u.first][u.second]== u){\n return u;\n }\n return par[u.first][u.second]= find(par[u.first][u.second]);\n}\nvoid unite(pair<int,int> u, pair<in...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n int minFlips(vector<vector<int>>& grid) {\n int rows = grid.size(), cols = grid[0].size();\n int flips = 0;\n \n if (rows % 2 == 1 && cols % 2 == 1) {\n if (grid[rows / 2][cols / 2] == 1) {\n flips += 1;\n }\n ...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n int minFlips(vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n int ans = 0;\n for(int i = 0; i < n / 2; i++) {\n for(int j = 0; j < m / 2; j++) {\n map<int, int> mpp;\n int coun...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n int minFlips(vector<vector<int>>& grid) {\n \n int ans = 0;\n int n = grid.size(), m = grid[0].size();\n \n for(int i=0; i<grid.size()/2; i++)\n {\n for(int j=0; j<grid[0].size()/2; j++)\n {\n set<...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n void dfs(int i, int j, int c, vector<vector<int>>& color) {\n int n = color.size();\n int m = color[0].size();\n if (i < 0 || i >= n || j < 0 || j >= m || color[i][j]) {\n return;\n }\n\n color[i][j] = c;\n dfs(i, m-1-j, c,...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n int minFlips(vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n int cnt = 0;\n for(int i = 0; i < n/2; i++){\n for(int j = 0; j < m/2; j++){\n set<pair<int,int>> st;\n st.insert(...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "#include <vector>\n#include <algorithm>\n\nusing namespace std;\n\nclass Solution {\npublic:\n int minFlips(vector<vector<int>>& grid) {\n map< pair<int,int> ,int > total,ones;\n int n=grid.size();\n int m=grid[0].size();\n for(int i=0;i<n;i++)\n {\n for(int...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n int fun(vector<vector<int>>&vec,int rem,int index,int n,vector<vector<int>>&dp){\n if(index == n){\n if(rem%4){\n return 1e9;\n }\n return 0;\n }\n if(dp[index][rem]!=-1){\n return dp[index][rem];...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n int minFlips(vector<vector<int>>& grid) {\n map< pair<int,int> ,int > total,ones;\n int n=grid.size();\n int m=grid[0].size();\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<m;j++)\n {\n int a=std::min(i,n-1-...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n int fun(vector<vector<int>>&vec,int rem,int index,int n,vector<vector<int>>&dp){\n if(index == n){\n if(rem%4){\n return 1e9;\n }\n return 0;\n }\n if(dp[index][rem]!=-1){\n return dp[index][rem];...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n\n int minFlips(vector<vector<int>>& grid) {\n int flips = 0;\n int n = grid.size(), m = grid[0].size();\n vector<vector<int>> newGrid(n, vector<int>(m, 0));\n\n for (int i = 0; i < n; i++)\n for (int j = 0; j < m; j++)\n i...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\n int m, n;\npublic:\n int to1D(int r, int c) {\n return (r * n + c);\n }\n\n int CountOnes(vector<vector<int>>& grid, int r, int c) {\n int ones = 1;\n int r1 = (m - 1 - r);\n int c1 = (n - 1 - c);\n if (c != c1 && grid[r][c1] == 1)\n ...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n int compare(vector<int>& row1,vector<int>& row2){\n if(row1.size()!=row2.size()){\n return -1;\n }\n int count=0;\n for(int i=0;i<row1.size();i++){\n if(row1[i]!=row2[i]){\n count++;\n }\n }\n ...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n int compare(vector<int>& row1,vector<int>& row2){\n if(row1.size()!=row2.size()){\n return -1;\n }\n int count=0;\n for(int i=0;i<row1.size();i++){\n if(row1[i]!=row2[i]){\n count++;\n }\n }\n ...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n int minFlips(vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n if(n*m < 4) {\n int ones = 0;\n for(int i=0;i<n;i++) {\n for(int j=0;j<m;j++) {\n ones += grid[i][j];\n ...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n int minFlips(vector<vector<int>>& a) {\n int n = a.size(), m = a[0].size();\n int ans = 0, twos = 0;\n\n for (int i = 0; i < n /2;i++) {\n for (int j = 0; j < m /2;j++) {\n vector<int> p(2, 0);\n set<pair<int, int>...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n int minFlips(vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n int i,j,k;\n int r1 = 0, r2 = 0;\n map<pair<int, int>, int> rows, cols;\n int cnt1 = 0;\n for(i=0;i<n;i++){\n for(j=0;j<m;j...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n int minFlips(vector<vector<int>>& a) \n {\n int n = a.size();\n int m = a[0].size();\n vector<vector<int>> groups4;\n vector<vector<int>> groups2,groups1;\n // vector<vector<int>> vis(n,vector<int>(m,0));\n for(int i = 0; i < n/2; ...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n \n vector<vector<int>> v,dp;\n int rec(int idx,int mod){\n if(idx==v.size())\n {\n if(mod==0)\n return 0;\n return 1e9;\n }\n if(dp[idx][mod]!=-1)\n return dp[idx][mod];\n int ans=rec(idx...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n \n int mod= 1e9+7;\n int func(int i, int sum, vector<vector<int>>& pairs, vector<vector<int>>& dp) \n {\n if (i >= pairs.size()) \n {\n if(sum==0)\n return 0;\n return mod;\n }\n if (dp[i][sum] != -1) \...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n \n const int INF = 2000001;\n\n int compute_min_flips(int idx, int mod_sum, vector<vector<int>>& cell_pairs, vector<vector<int>>& memo) {\n if (idx >= cell_pairs.size()) {\n return (mod_sum == 0) ? 0 : INF;\n }\n\n if (memo[idx][mod_sum] ...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n int calculate_min_flips(int pair_index, int current_modulo_sum, vector<vector<int>>& counts, vector<vector<int>>& dp) {\n if (pair_index >= counts.size()) {\n return (current_modulo_sum == 0) ? 0 : 1e9;\n }\n\n if (dp[pair_index][current_modulo_su...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n\n int ftt(int iidd, int mysum, vector<vector<int>>& vec_pair,\n vector<vector<int>>& memoo) {\n if (iidd >= vec_pair.size()) {\n if (mysum == 0) {\n return 0;\n }\n return 1e9;\n }\n\n if (memo...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "typedef long long int ll;\n\nclass DisjointSet{\n private:\n \n public:\n vector<ll> rank;vector<ll> parent;vector<ll> size;\n\n DisjointSet(ll n){\n //rank saari 0 se initialise hongi , parent sabke khud vhi honge\n rank.resize(n+1,0);\n parent.resize(n+1);\n\n //sabka s...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n int minFlips(vector<vector<int>>& grid) {\n vector<vector<int>>v;\n v=grid;\n int n= grid.size(),m=grid[0].size();\n\n \n set<pair<int,int>>st;\n int cnt=0,four=0;\n \n for(int i=0;i<n/2;i++){\n \n fo...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n int minFlips(vector<vector<int>> grid)\n {\n map< pair<int,int> ,int > total,ones;\n int n=grid.size();\n int m=grid[0].size();\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<m;j++)\n {\n int a=std::min(i,...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n int minFlips(vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n\n int flips = 0;\n int zero_zero = 0;\n int zero_one = 0;\n int one_one = 0;\n\n for (int i = 0; i <= n-1-i; ++i) {\n for (...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n int minFlips(vector<vector<int>>& grid) {\n \n int n=grid.size();\n int m=grid[0].size();\n int ans=0;\n int ones=0;\n bool f=false;\n for(int i=0;i<(n+1)/2;i++){\n \n for(int j=0;j<(...
3,524
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code...
3
{ "code": "class Solution {\npublic:\n int minFlips(vector<vector<int>>& grid) {\n int n = grid.size(), m = grid[0].size();\n int ans = 0, t_o = 0;\n int n2 = 10, n1 = 10, p2 = 10, p1 = 10;\n for(int i = 0; i < (n + 1) / 2; i++) {\n for(int j = 0; j < (m + 1) / 2; j++) {\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "int speedup = []{ios::sync_with_stdio(0); cin.tie(0); return 0;}();\nint nbs[100001], edg[200000], tm[100000][2];\nvector<int> res;\n\nint dfs(int n, int p) {\n int b0 = 0, b1 = 0;\n for (int i = nbs[n], j = nbs[n+1]; i != j; ++i) if (int nb = edg[i]; nb != p) {\n int x = dfs(nb, n) + 2 - (nb ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "int speedup = []{ios::sync_with_stdio(0); cin.tie(0); return 0;}();\nint nbs[100001], edg[200000], tm[100000][2];\nvector<int> res;\n\nint dfs(int n, int p) {\n int b0 = 0, b1 = 0;\n for (int i = nbs[n], j = nbs[n+1]; i != j; ++i) if (int nb = edg[i]; nb != p) {\n int x = dfs(nb, n) + 2 - (nb ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "constexpr int MAX = 100010;\n\nint dp[MAX][2];\nstd::vector<int> adj[MAX];\n\nvoid Update(int x, int& v1, int& v2) {\n if (v1 < x) {\n v2 = v1;\n v1 = x;\n } else {\n v2 = std::max(v2, x);\n }\n}\n\nvoid DFS1(int x, int p) {\n dp[x][0] = 0;\n dp[x][1] = 0;\n for (int y : adj[x]) {\n if (y...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "const int N = (int)1e5 + 5;\nint dwn[N], up[N];\n#define evee(x) (1+(!(x&1)))\n\nvector<int> g[N];\n\nvoid getdwn(int u, int par = -1) {\n dwn[u] = 0;\n for (auto &v : g[u]) if (v != par) {\n getdwn(v, u);\n dwn[u] = max(dwn[u], dwn[v] + evee(v));\n }\n}\nvoid getup(int u, int par = ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "constexpr int MAX = 100010;\n\nint dp[MAX][2];\nstd::vector<int> adj[MAX];\n\nvoid Update(int x, int& v1, int& v2) {\n if (v1 < x) {\n v2 = v1;\n v1 = x;\n } else {\n v2 = std::max(v2, x);\n }\n}\n\nvoid DFS1(int x, int p) {\n dp[x][0] = 0;\n dp[x][1] = 0;\n for (int y : adj[x]) {\n if (y...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "#pragma GCC optimize (\"Ofast\")\n#define _USE_MATH_DEFINES\n#include <bits/stdc++.h>\n#define FOR(i, a, b) for (auto i=(a); i<(b); i++)\n#define FORD(i, a, b) for (int i=(a); i>(b); i--)\n#define SZ(x) ((int)(x).size())\n#define ALL(x) (x).begin(), (x).end()\n#define PPC(x) __builtin_popcountll(x)\n#defin...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "class Solution {\npublic:\n int dfs(int curr,vector<int>adj[],vector<int>&far1,vector<int>&far2){\n for(int child:adj[curr]){\n int x=dfs(child,adj,far1,far2);\n x+=child&1?1:2;\n if(x>=far1[curr]){\n far2[curr]=far1[curr];\n far1[cur...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "class Solution {\npublic:\n int dfs(int curr,vector<int>adj[],vector<int>&far1,vector<int>&far2){\n for(int child:adj[curr]){\n int x=dfs(child,adj,far1,far2);\n x+=child&1?1:2;\n if(x>far1[curr]){\n far2[curr]=far1[curr];\n far1[curr...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "class Solution {\npublic:\n static const int MX = (int)1e5 + 1;\n int n;\n vector<int> adj[MX];\n int dp[MX], mxt[MX], mxt2[MX];\n vector<int> ans;\n void calc(int u, int p) {\n if (p != -1) {\n if (u % 2 == 0) {\n dp[u] = dp[p] + 2;\n } else {\...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "class Solution {\npublic:\n static const int MX = (int)1e5 + 1;\n int n;\n vector<int> adj[MX];\n int dp[MX], mxt[MX], mxt2[MX];\n vector<int> ans;\n void calc(int u, int p) {\n if (p != -1) {\n if (u % 2 == 0) {\n dp[u] = dp[p] + 2;\n } else {\...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "class Solution {\npublic:\n vector<int> timeTaken(vector<vector<int>>& edges) {\n static constexpr int N = 100005;\n static vector<int> t[N];\n int n = 0;\n for (auto& v : edges) n = max(n, max(v[0], v[1]) + 1);\n for (int i = 0; i < n; i++) {\n t[i].clear()...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "const int maxn = 1e5 + 5;\nvector<int> G[maxn];\nint D[maxn];\n\nclass Solution {\npublic:\n int dfsD(int cur, int p)\n{\n D[cur] = 0;\n for(int i = 0; i < G[cur].size(); i++)\n {\n int nxt = G[cur][i];\n if(nxt == p) continue;\n D[cur] = max(D[cur], dfsD(nxt, cur) + 2 - (n...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "class Solution {\n void buildFar(int curr, vector<vector<int>> &adj, vector<int> &far1, vector<int> &far2) {\n int maxChild1 = 0, maxChild2 = 0;\n for(int child: adj[curr]) {\n buildFar(child, adj, far1, far2);\n int dist = ((child & 1) ? 1 : 2) + far1[child];\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "int max(const vector<vector<int>>& edges) {\n auto result = edges[0][0];\n for (const auto& e : edges) {\n result = fmax(result, e[0]);\n result = fmax(result, e[1]);\n }\n return result;\n}\n\ninline int32_t fmax(int32_t a, int32_t b) {\n return a - ((a - b) & (a - b) >> 31);\n}\n\nvoid dfs_tre...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "int ans[100005],dp[100005][2];\nvector<int> adj[100005];\n\nvoid dfs(int root, int prev)\n{\n int curr;\n for(auto j:adj[root])\n {\n if(j!=prev)\n {\n dfs(j,root);\n curr=dp[j][0]+(j%2?1:2);\n if(curr>dp[root][0])\n dp[root][1]=dp[root...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "\nint ans[100005],f[100005][2];\nvector<int> g[100005];\n\nvoid dfs(int root, int prev)\n{\n int curr;\n for(auto nxt:g[root])\n {\n if(nxt!=prev)\n {\n dfs(nxt,root);\n curr=f[nxt][0]+(nxt%2?1:2);\n if(curr>f[root][0])\n f[root][1]=f[r...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "\nint ans[100005],f[100005][2];\nvector<int> g[100005];\n\nvoid solve(int root, int prev, int incoming)\n{\n ans[root]=max(f[root][0],incoming);\n int pa=(root%2?1:2),curr,x;\n for(auto j:g[root])\n {\n if(j!=prev)\n {\n curr=f[j][0]+(j%2?1:2);\n x=(curr==f[r...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "class Solution {\npublic:\n vector<int> g[100055];\n int dp[100055];\n \n void dfs(int v, int par = -1) {\n dp[v] = 0;\n \n for (auto u : g[v]) {\n if (u == par) continue;\n \n dfs(u, v);\n \n dp[v] = max(dp[v], dp[u] +...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "const int MAXN = 1e5;\nclass Solution {\nint ans1[MAXN], ans2[MAXN], up[MAXN];\nvector<int> adj[MAXN];\npublic:\n vector<int> timeTaken(vector<vector<int>>& edges) {\n \n int n = edges.size() + 1;\n \n for (auto &e: edges) {\n adj[e[0]].push_back(e[1]);\n ad...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "class Solution {\npublic:\n static const int MAX_N = 1e5 + 1;\n vector<int> adj[MAX_N];\n int far1[MAX_N];\n int far2[MAX_N];\n\n void dfs(int node, int p) {\n for(int to : adj[node]) {\n if(to == p) continue;\n\n dfs(to, node);\n int dist = (to & 1 ? ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "class Solution {\npublic:\n\nint dp[100001][2];\nint maxchild[100001];\n\n int solve(vector<int>adj[], int u, int p){\n int first =0, second = 0;\n // int dis =0;\n maxchild[u] = -1;\n for(auto it : adj[u]){\n if(it != p){\n int dis = solve(adj,it,u);\n dis += (it%...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "class Solution {\npublic:\n void 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]+2...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "class Solution {\npublic:\n int dp[100000][2];\n int solve(int i,int j,vector<int>a[])\n {\n int x=0,y=0;\n for(int c:a[i])\n {\n if(c==j)continue;\n int p=solve(c,i,a);\n if(c%2==0)p+=2;\n if(c%2!=0)p+=1;\n if(x<p)\n {\n y=x;x=p;\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "int speedup = []{ios::sync_with_stdio(0); cin.tie(0); return 0;}();\nclass Solution {\npublic:\n vector<int> timeTaken(vector<vector<int>>& edges) {\n int n = edges.size() + 1;\n vector<int> gp[n];\n\n for (const auto &it: edges) {\n gp[it[0]].emplace_back(it[1]);\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "class Solution {\npublic:\n int dfs1(vector<int>adj[],vector<pair<int,int> >&storemaxi,int node,int parnode){\n int maxi1=0;\n int maxi2=0;\n for(auto i:adj[node]){\n if(i!=parnode){\n \n int temp=dfs1(adj,storemaxi,i,node);\n if(...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "class Solution {\n int bfs(vector<int> adj[], vector<int> &dist, int src) {\n if(src & 1)\n dist[src] = 1;\n else\n dist[src] = 2;\n\n \n using pi = pair<int, int>;\n priority_queue<pi, vector<pi>, greater<pi>> pq;\n pq.push({dist[src], src...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "class Solution {\npublic:\n\n typedef long long ll;\n vector<ll>g[100001];\n\n ll dfs(int u, int p, vector<ll>&ch){\n\n ll mx = 0;\n\n for(int i=0; i<g[u].size(); i++){\n\n int v = g[u][i];\n\n if(v == p){\n continue;\n }\n\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "class Solution {\npublic:\n vector<int> timeTaken(vector<vector<int>>& edges) {\n int n = edges.size() + 1;\n vector<vector<int>> adj(n);\n for (auto &e : edges){\n adj[e[0]].push_back(e[1]);\n adj[e[1]].push_back(e[0]);\n }\n\n vector<int> res(n,...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "class Solution {\npublic:\n vector<int> timeTaken(vector<vector<int>>& edges) {\n int n = edges.size() + 1;\n vector<vector<int>> adj(n);\n for (auto &e : edges){\n adj[e[0]].push_back(e[1]);\n adj[e[1]].push_back(e[0]);\n }\n\n vector<int> res(n,...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "class Solution {\npublic:\n void dfs1 (vector<int> adj[],int node,vector<int> &vis, int & ans,int dis,int & end1){\n vis[node]=1;\n\n for(auto nd: adj[node]){\n if(!vis[nd]){\n if(nd%2==0){\n \n if(ans<dis+2){\n ans...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "int first[100002],sec[100002];\nvoid dfs1(int node,int parent,vector<vector<int>>& tree){\n for(auto i:tree[node]){\n if(i==parent) continue;\n dfs1(i,node,tree);\n int d = 1;\n if(i%2==0) d+=1;\n if(first[i]+d>first[node]){\n sec[node] = first[node];\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "class Solution {\npublic:\n int n;\n // vector<vector<int>> adj;\n // unordered_map<int, unordered_map<int, int>> child_time;\n vector<vector<pair<int, int>>> child_time;\n // unordered_map<long long, int> child_time;\n // map<pair<int, int>, int> child_time;\n vector<int> res;\n lo...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "class Solution {\npublic:\n int n;\n // vector<vector<int>> adj;\n // unordered_map<int, unordered_map<int, int>> child_time;\n vector<vector<pair<int, int>>> child_time;\n // unordered_map<long long, int> child_time;\n // map<pair<int, int>, int> child_time;\n vector<int> res;\n lo...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define loop(i, a, n) for (lli i = (a); i < (n); ++i)\n#define loopD(i, a, n) for (lli i = (a); i >= (n); --i)\n#define all(c) (c).begin(), (c).end()\n#define rall(c) (c).rbegin(), (c).rend()\n#define sz(a) ((int)a.size())\n#define YES cout << \"YES\" << en...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "class NodeInfo{\n public:\n int node,cost;\n NodeInfo(int node,int cost){\n this->node = node;\n this->cost = cost;\n }\n};\n\nclass Solution {\npublic:\n\n void updateNodeInfo(pair<NodeInfo,NodeInfo>&info,NodeInfo newInfo){\n if(newInfo.cost > info.first.cost){\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "#define ll long long int\n// if i is odd the node will get marked at time x if any of its adjacent node was marked as time x-1.\n// if i is even the node will get marked at time x if any of its adjacent node was marked at time x-2. \n// we have to solve it in 10^5.\nll dp[100005][2];\nclass Solution {\npub...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "void dfs(int p,int dis,vector<vector<int>>&v,vector<int>&vis,int &dep,int &node)\n{\n vis[p]=1;\n if(dis>dep)\n {\n dep=dis;\n node=p;\n }\n\n for(int i=0;i<v[p].size();i++)\n {\n if(vis[v[p][i]]==0)\n {\n if(v[p][i]%2)\n {\n ...
3,532
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>...
0
{ "code": "class Solution {\n int findLRmaxTime(int node, vector<vector<int>> &adj, vector<pair<int,int>> &a, vector<pair<int,int>> &b, int par) {\n int mx = 0, smx = 0, mc = -1, smc = -1;\n \n for (auto it : adj[node]) {\n if (it == par) continue;\n \n int p =...