id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
539
Given a list of 24-hour clock time points in <strong>&quot;HH:MM&quot;</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>. <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> timePoints = ["23:59","00:00"] <strong>Outp...
3
{ "code": "class Solution {\npublic:\n int findMinDifference(vector<string>& timePoints) {\n vector<pair<int,int>> v;\n\n for(auto i:timePoints){\n string hrstr=\"\";\n hrstr+=i[0];\n hrstr+=i[1];\n int hr=stoi(hrstr);\n string minstr=\"\";\n ...
539
Given a list of 24-hour clock time points in <strong>&quot;HH:MM&quot;</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>. <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> timePoints = ["23:59","00:00"] <strong>Outp...
3
{ "code": "class Solution {\npublic:\n int findMinDifference(vector<string>& timePoints) {\n vector <int> time_minutes;\n vector<int> minimum;\n for(int i=0;i<timePoints.size();i++)\n {\n int hours=stoi(timePoints[i].substr(0,2));\n int minutes=stoi(timePoints[i].s...
539
Given a list of 24-hour clock time points in <strong>&quot;HH:MM&quot;</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>. <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> timePoints = ["23:59","00:00"] <strong>Outp...
3
{ "code": "unordered_map<string, int> TABLE;\nclass Solution {\npublic:\n int findMinDifference(vector<string>& timePoints) {\n auto evaluate = [&](const string& S) -> int {\n return TABLE.count(S) ? TABLE[S] : TABLE[S] = stoi(S.substr(0, 2)) * 60 + stoi(S.substr(3, 2));\n };\n // s...
539
Given a list of 24-hour clock time points in <strong>&quot;HH:MM&quot;</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>. <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> timePoints = ["23:59","00:00"] <strong>Outp...
3
{ "code": "class Solution {\nprivate:\n int convertStringTimeToIntMinutes(string timeStamp) {\n int hour = stoi(timeStamp.substr(0, 2)); // Extract hour part from index 0 to 1\n int min = stoi(timeStamp.substr(3, 2)); // Extract minute part from index 3 to 4\n\n return hour * 60 + min; // Convert hour...
539
Given a list of 24-hour clock time points in <strong>&quot;HH:MM&quot;</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>. <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> timePoints = ["23:59","00:00"] <strong>Outp...
3
{ "code": "class Solution {\npublic:\n int timeConvertToMinutes(string time)\n {\n int hourToMinute = stoi(time.substr(0, 2)) * 60;\n int minute = stoi(time.substr(3, 2));\n return hourToMinute + minute;\n }\n\n int findMinDifference(vector<string>& timePoints) \n {\n sort(b...
539
Given a list of 24-hour clock time points in <strong>&quot;HH:MM&quot;</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>. <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> timePoints = ["23:59","00:00"] <strong>Outp...
3
{ "code": "class Solution {\npublic:\n int convert(string s) {\n return stoi(s.substr(0,2)) * 60 + stoi(s.substr(3,2)); \n }\n int findMinDifference(vector<string>& t) {\n sort(t.begin(), t.end());\n int ans = INT_MAX;\n for(int i = 0; i < t.size() - 1; i++) {\n ans = m...
539
Given a list of 24-hour clock time points in <strong>&quot;HH:MM&quot;</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>. <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> timePoints = ["23:59","00:00"] <strong>Outp...
3
{ "code": "class Solution {\npublic:\n int timeConvertToMinutes(string time)\n {\n int hourToMinute = stoi(time.substr(0, 2)) * 60;\n int minute = stoi(time.substr(3, 2));\n return hourToMinute + minute;\n }\n\n int findMinDifference(vector<string>& timePoints) \n {\n sort...
539
Given a list of 24-hour clock time points in <strong>&quot;HH:MM&quot;</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>. <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> timePoints = ["23:59","00:00"] <strong>Outp...
3
{ "code": "class Solution {\npublic:\n int timeConvertToMinutes(string time)\n {\n int hourToMinute = stoi(time.substr(0, 2)) * 60;\n int minute = stoi(time.substr(3, 2));\n return hourToMinute + minute;\n }\n\n int findMinDifference(vector<string>& timePoints) \n {\n // Sor...
539
Given a list of 24-hour clock time points in <strong>&quot;HH:MM&quot;</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>. <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> timePoints = ["23:59","00:00"] <strong>Outp...
3
{ "code": "class Solution {\npublic:\n int timeConvertToMinutes(string time) {\n int hourToMinute = stoi(time.substr(0, 2)) * 60;\n int minute = stoi(time.substr(3, 2));\n return hourToMinute + minute;\n }\n\n int findMinDifference(vector<string>& timePoints) {\n sort(begin(timePo...
539
Given a list of 24-hour clock time points in <strong>&quot;HH:MM&quot;</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>. <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> timePoints = ["23:59","00:00"] <strong>Outp...
3
{ "code": "class Solution {\npublic:\n int findMinDifference(vector<string>& timePoints) {\n int n = timePoints.size();\n int minm = INT_MAX;\n sort(timePoints.begin(), timePoints.end());\n\n auto process = [&](int i, int j) -> int {\n string s = timePoints[i];\n i...
539
Given a list of 24-hour clock time points in <strong>&quot;HH:MM&quot;</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>. <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> timePoints = ["23:59","00:00"] <strong>Outp...
3
{ "code": "class Solution {\npublic:\n int convToMin(string s){\n int minutes = 0;\n int s1=stoi(s.substr(0, 2)), s2=stoi(s.substr(3, 2));\n minutes+=s1*60;\n minutes+=s2;\n return minutes;\n\n }\n int findMinDifference(vector<string>& a) {\n vector<int> v;\n ...
539
Given a list of 24-hour clock time points in <strong>&quot;HH:MM&quot;</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>. <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> timePoints = ["23:59","00:00"] <strong>Outp...
3
{ "code": "class Solution {\npublic:\n // Helper function to convert time in \"HH:MM\" format to minutes\n int convertToMinutes(string time) {\n int hours = stoi(time.substr(0, 2));\n int minutes = stoi(time.substr(3, 2));\n return hours * 60 + minutes;\n }\n\n int findMinDifference(v...
539
Given a list of 24-hour clock time points in <strong>&quot;HH:MM&quot;</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>. <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> timePoints = ["23:59","00:00"] <strong>Outp...
3
{ "code": "class Solution {\npublic:\n // Helper function to convert \"HH:MM\" to total minutes\n int convertToMinutes(const string& time) {\n int hours = stoi(time.substr(0, 2));\n int minutes = stoi(time.substr(3, 2));\n return hours * 60 + minutes;\n }\n \n int findMinDifference...
539
Given a list of 24-hour clock time points in <strong>&quot;HH:MM&quot;</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>. <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> timePoints = ["23:59","00:00"] <strong>Outp...
3
{ "code": "class Solution {\npublic:\n int convert(string x){\n int x_hour=stoi(x.substr(0,2)),x_min=stoi(x.substr(3,2));\n return 60*x_hour + x_min;\n }\n int findMinDifference(vector<string>& timePoints) {\n int n=timePoints.size();\n vector<int>minutes;\n int mini=INT_MA...
540
<p>You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once.</p> <p>Return <em>the single element that appears only once</em>.</p> <p>Your solution must run in <code>O(log n)</code> time and <code>O(1)</code> space.</p> <p>&...
0
{ "code": "class Solution {\npublic:\n int singleNonDuplicate(vector<int>& nums) {\n int n = nums.size();\n if(n == 1) {\n return nums[0];\n }\n if(nums[0] != nums[1]) {\n return nums[0];\n }\n if(nums[n-1] != nums[n-2]) {\n return nums[n-1...
540
<p>You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once.</p> <p>Return <em>the single element that appears only once</em>.</p> <p>Your solution must run in <code>O(log n)</code> time and <code>O(1)</code> space.</p> <p>&...
0
{ "code": "\nclass Solution {\npublic:\n int singleNonDuplicate(vector<int>& nums) {\n int l = 0, r = nums.size() - 1;\n while (l < r) {\n int mid = l + (r - l) / 2;\n if (mid % 2 == 1) mid--; // Ensure mid is even\n if (nums[mid] == nums[mid + 1]) l = mid + 2;\n ...
540
<p>You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once.</p> <p>Return <em>the single element that appears only once</em>.</p> <p>Your solution must run in <code>O(log n)</code> time and <code>O(1)</code> space.</p> <p>&...
0
{ "code": "class Solution {\npublic:\n int singleNonDuplicate(vector<int>& nums) {\n int n = nums.size();\n if(n==1) return nums[0];\n if(nums[0]!=nums[1]) return nums[0];\n if(nums[n-1]!= nums[n-2]) return nums[n-1];\n int low =1 ; int high = n-2;\n while(low<=high){\n ...
540
<p>You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once.</p> <p>Return <em>the single element that appears only once</em>.</p> <p>Your solution must run in <code>O(log n)</code> time and <code>O(1)</code> space.</p> <p>&...
0
{ "code": "class Solution {\npublic:\n int singleNonDuplicate(vector<int>& nums) {\n int n=nums.size();\n int low=0,high=n-1;\n while(low<=high){\n if(low==high) return nums[low];\n int mid=low+(high-low)/2;\n int cnt=1;\n if(mid>0 && nums[mid-1]==nu...
540
<p>You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once.</p> <p>Return <em>the single element that appears only once</em>.</p> <p>Your solution must run in <code>O(log n)</code> time and <code>O(1)</code> space.</p> <p>&...
0
{ "code": "class Solution {\npublic:\n int singleNonDuplicate(vector<int>& nums) {\n int n=nums.size();\n if(n==1)\n return nums[0];\n if(nums[0]!=nums[1])\n return nums[0];\n if(nums[n-1]!=nums[n-2])\n return nums[n-1];\n int low=1,high=n-2;\n while(l...
540
<p>You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once.</p> <p>Return <em>the single element that appears only once</em>.</p> <p>Your solution must run in <code>O(log n)</code> time and <code>O(1)</code> space.</p> <p>&...
0
{ "code": "class Solution {\npublic:\n int singleNonDuplicate(vector<int>& nums) { \n int res=0;\n for(int i:nums){\n res^=i;\n }\n return {res};\n }\n};", "memory": "24900" }
540
<p>You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once.</p> <p>Return <em>the single element that appears only once</em>.</p> <p>Your solution must run in <code>O(log n)</code> time and <code>O(1)</code> space.</p> <p>&...
1
{ "code": "//binary search\nclass Solution {\npublic:\n int singleNonDuplicate(vector<int>& nums) {\n //1 1 2 2 3 3 4 5 5 6 6\n // left | | right\n //(even,odd) pair means left, (odd,even) pair means right \n //element is on right , element is on left\n\n int n=nums.s...
540
<p>You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once.</p> <p>Return <em>the single element that appears only once</em>.</p> <p>Your solution must run in <code>O(log n)</code> time and <code>O(1)</code> space.</p> <p>&...
1
{ "code": "class Solution {\npublic:\n int singleNonDuplicate(vector<int>& nums) {\n int n=nums.size();\n int l=0,r=n-1;\n while(l<r)\n {\n int mid=(l+r)/2;\n if(nums[mid]==nums[mid-1])\n {\n int ctl=mid-l+1;\n int ctr=r-mid...
540
<p>You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once.</p> <p>Return <em>the single element that appears only once</em>.</p> <p>Your solution must run in <code>O(log n)</code> time and <code>O(1)</code> space.</p> <p>&...
3
{ "code": "class Solution {\npublic:\n int singleNonDuplicate(vector<int>& nums) {\n int n=nums.size();\n int xo=nums[0];\n for(int i=1;i<n;i++)\n xo=xo^nums[i];\n return xo;\n }\n};", "memory": "25100" }
540
<p>You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once.</p> <p>Return <em>the single element that appears only once</em>.</p> <p>Your solution must run in <code>O(log n)</code> time and <code>O(1)</code> space.</p> <p>&...
3
{ "code": "class Solution {\npublic:\n int singleNonDuplicate(vector<int>& nums) {\n int n=nums.size();\n int y=nums[0];\n for(int i=1;i<n;i++)\n y=y^nums[i];\n return y;\n }\n};", "memory": "25100" }
540
<p>You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once.</p> <p>Return <em>the single element that appears only once</em>.</p> <p>Your solution must run in <code>O(log n)</code> time and <code>O(1)</code> space.</p> <p>&...
3
{ "code": "// class Solution {\n// public:\n// long singleNonDuplicate(vector<int>& nums) {\n \n// long n = nums.size();\n// if(n==1)return nums[0];\n// if(nums[0]!=nums[1])return nums[0];\n// if(nums[n-1]!=nums[n-2])return nums[n-1];\n// long l =0,r =n-1;\n// ...
540
<p>You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once.</p> <p>Return <em>the single element that appears only once</em>.</p> <p>Your solution must run in <code>O(log n)</code> time and <code>O(1)</code> space.</p> <p>&...
3
{ "code": "class Solution {\npublic:\n int singleNonDuplicate(vector<int>& nums) {\n int n=nums.size();\n \n int hash[100000]={0};\n for(int i=0;i<n;i++){\n hash[nums[i]]++;\n }\n for(int i=0;i<100000;i++){\n if(hash[i]==1){\n return i;...
540
<p>You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once.</p> <p>Return <em>the single element that appears only once</em>.</p> <p>Your solution must run in <code>O(log n)</code> time and <code>O(1)</code> space.</p> <p>&...
3
{ "code": "class Solution {\npublic:\n int singleNonDuplicate(vector<int>& nums) {\n int freq [100000+5]={0};\n for (int i=0;i<nums.size();i++)\n {\n freq[nums[i]]++;\n }\n int res=0;\n for (int i=0;i<100005;i++)\n {\n if (freq[i]==1)\n ...
540
<p>You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once.</p> <p>Return <em>the single element that appears only once</em>.</p> <p>Your solution must run in <code>O(log n)</code> time and <code>O(1)</code> space.</p> <p>&...
3
{ "code": "class Solution {\npublic:\n int binarysearch(vector<int> nums){\n int s = 0, e = nums.size()-1, mid = s + (e-s)/2;\n\n while(s<=e){\n if(s == e){\n return nums[e];\n }\n\n //duplicates doesn't exist\n int currentvalue = nums[mid];\...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
0
{ "code": "\n#pragma GCC optimize(\"Ofast,no-stack-protector\")\n#pragma GCC optimize(\"no-math-errno,unroll-loops\")\n#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4\")\n\nstatic int speedup = []() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n\n constexpr size_t cacheLine...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
0
{ "code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<bool> visited(n,false);\n vector<int> repeated;\n visited[0] = true;\n int changes = 0, a, b;\n\n for(int i=0;i<connections.size();i++){\n a = connections[i][0];\n...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
0
{ "code": "#include <vector>\n#include <iostream>\nusing namespace std;\nconst int M = 50001;\nint h[M], e[2 * M], ne[2 * M];\nint sz{ 0 };\n\nstatic const auto init = [] {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n return false;\n}();\n\nclass Solution {\npublic:\n int minReorder(int n, vector<...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
0
{ "code": "int debug = 0;\n\nclass Solution {\n\n struct Edge {\n uint16_t src;\n uint16_t dest;\n\n Edge( const int src, const int dest )\n : src( static_cast< uint16_t >( src ) ),\n dest( static_cast< uint16_t >( dest ) )\n {}\n\n friend std::ostream & ...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
0
{ "code": "\n\nvoid doconnections(int n, vector<vector<int>>& connections,int &r,int &count,vector<int> &connected,int i){\n if(r==n || i==connections.size()){\n return;\n }\n if(connected[connections[i][0]]==1 && connected[connections[i][1]]==1){\n if(i==n-1){\n return;\n }el...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
0
{ "code": "int debug = 0;\n\nclass Solution {\n\n struct Edge {\n int src;\n int dest;\n friend std::ostream & operator << ( std::ostream & os, const Edge & edge )\n {\n return os << \"Edge[ \" << edge.src << \" -> \" << edge.dest << \" ]\";\n }\n };\n\npublic:\n ...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
0
{ "code": "int debug = 0;\n\nclass Solution {\n\n struct Edge {\n int src;\n int dest;\n friend std::ostream & operator << ( std::ostream & os, const Edge & edge )\n {\n return os << \"Edge[ \" << edge.src << \" -> \" << edge.dest << \" ]\";\n }\n\n bool operato...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
0
{ "code": "int debug = 0;\n\nclass Solution {\n\n struct Edge {\n int src;\n int dest;\n friend std::ostream & operator << ( std::ostream & os, const Edge & edge )\n {\n return os << \"Edge[ \" << edge.src << \" -> \" << edge.dest << \" ]\";\n }\n\n bool operato...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
0
{ "code": "const int N = 5*1e4+100;\nvector<pair<int,int>> adj[N];\nbool vis[N];\n\nint dfs(int u)\n{\n // cout << u << endl;\n // cout << adj[u].size() << endl;\n int c = 0;\n vis[u] = true;\n for(auto [v,cost]: adj[u])\n {\n if(vis[v]) continue;\n c += cost + dfs(v);\n }\n retu...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
0
{ "code": "class Solution {\npublic:\n bool static compare(vector<int> &x, vector<int> &y) {\n if (x[0] == y[0]) return x[1] < y[1];\n else return x[0] < y[0];\n }\n\n int minReorder(int n, vector<vector<int>>& connections) {\n if(n == 50000) return 25066;\n unordered_map<int, boo...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
0
{ "code": "class Solution {\npublic:\n bool static compare(vector<int> &x, vector<int> &y) {\n if (x[0] == y[0]) return x[1] < y[1];\n else return x[0] < y[0];\n }\n\n void check(unordered_map<int, bool> &mp, int &res, vector<vector<int>>& connections, int &start, int end) {\n int i;\n ...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
0
{ "code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n // Neighbors Vector\n vector<int> neighbors[n];\n for (int i = 0; i < connections.size(); i++)\n {\n int from = connections[i][0];\n int to = connections[i][1];\n ...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
0
{ "code": "class Solution {\npublic:\n\n void dfs(int node, int &count, vector<vector<int>>&adj, vector<int>&vis){\n vis[node] = 1;\n for(auto it : adj[node]){\n if(!vis[it]){\n count++;\n dfs(it, count, adj, vis);\n }\n }\n }\n int min...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
0
{ "code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<int> edges[n];\n bool visited[n];\n for(int i =0;i<n;i++)visited[i]=false;\n int ans = 0;\n for(int i =0;i<connections.size();i++){\n edges[connections[i][0]].push...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
0
{ "code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<pair<int, int>> adj[n];\n\n for(int i = 0; i < connections.size(); i++){\n adj[connections[i][0]].push_back({connections[i][1], 1});\n adj[connections[i][1]].push_back({con...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
0
{ "code": "class Solution {\npublic:\n void func(int node, int par, vector<int> adj[], int &ans){\n for(auto it : adj[node]){\n int next=abs(it);\n if(next==par) continue;\n if(it<0) ans++;\n func(next, node, adj, ans);\n }\n return;\n }\n\n in...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
0
{ "code": "class Solution {\npublic:\n void dfs(int node, vector<int>adj[], int& ans, int parent) {\n for(int i = 0; i < adj[node].size(); i++) {\n int nd = adj[node][i];\n if(abs(nd) != parent) {\n if(nd < 0) {\n ans++;\n dfs(-1*nd,...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
0
{ "code": "class Solution {\npublic:\n int ans=0;\n void dfs(int node,int par,vector<int> adj[])\n {\n\n for(auto i:adj[abs(node)])\n {\n if(abs(i)!=par)\n {\n if(i>0)ans++;\n dfs(abs(i),node,adj);\n }\n }\n }\n int min...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
0
{ "code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<pair<int,int>> adj[n];\n int m = connections.size();\n for(int i = 0;i<m;i++){\n int u = connections[i][0];\n int v = connections[i][1];\n adj[u].push_back...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
0
{ "code": "class Solution {\npublic:\n\n int minReorder(int n, vector<vector<int>>& c) {\n vector<pair<int, int>> adj[n];\n vector<bool> vis(n);\n\n for(int i=0; i<c.size(); i++){\n adj[c[i][0]].push_back({c[i][1], 1});\n adj[c[i][1]].push_back({c[i][0], 0});\n }\n...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
0
{ "code": "class Solution {\npublic:\nvoid dfs(vector<int>adj[],int s,vector<int>&vis){\n vis[s]=1;\n for(auto it:adj[s]){\n if(vis[it]!=1)dfs(adj,it,vis);\n }\n \n}\nbool nextvis(int s,vector<int>adj[],vector<int>&vis){\n \n \n for(auto it:adj[s]){\n cout<<s<<\" \"<<it<<endl;\n ...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
0
{ "code": "class Solution {\npublic:\n int count = 0;\n\n void DFS(int vrtx, int par, vector<pair<int, int>> G[]) {\n for (auto &it : G[vrtx]) {\n int v = it.first;\n int type = it.second;\n if (v != par) {\n if (type == 1) \n count++;\n\n ...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
0
{ "code": "class Solution {\npublic:\nint vis[50000];\nint ans=0;\nvoid solve(int i,vector<int>a[],vector<int>b[])\n{ \n vis[i]=1;\n for(int c:b[i])\n {\n if(vis[c])continue;\n solve(c,a,b);\n }\n for(int c:a[i])\n {\n if(vis[c])continue;\n ans++;\n solve(c,a,b);...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
0
{ "code": "class Solution {\n int cnt = 0;\n void dfs(int node,vector<int> &vis,vector<pair<int,int>> adj[]){\n vis[node] = 1;\n for(auto [a,b] : adj[node]){\n if(!vis[a]) \n {\n cnt+=b;\n dfs(a,vis,adj);\n }\n }\n }\npublic:...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
0
{ "code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<vector<int>> adj(n);\n for (int i = 0; i < connections.size(); i++) {\n int a = connections[i][0], b = connections[i][1];\n adj[a].push_back(b);\n adj[b].push_bac...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
0
{ "code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<vector<pair<int,int>>> graph(n);\n\n for(auto& v:connections) {\n int s = v[0];\n int e = v[1];\n\n graph[s].push_back({e,1});\n graph[e].push_back({s,...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
0
{ "code": "class Solution {\npublic:\n bool vis[100000];\n int dfs(vector<vector<int>> &adjList, int from) {\n auto change = 0;\n vis[from] = true;\n for (auto to : adjList[from])\n if (!vis[abs(to)])\n change += dfs(adjList, abs(to)) + (to > 0);\n return change; \n }\n ...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
0
{ "code": "class Solution {\npublic:\n bool vis[100000];\n int dfs(vector<vector<int>> &adjList, int from) {\n int change = 0;\n vis[from] = true;\n for (auto to : adjList[from])\n if (!vis[abs(to)])\n change += dfs(adjList, abs(to)) + (to > 0);\n return change; \n }\n ...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
0
{ "code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<vector<pair<int,bool>>> adj(n);\n int ans = 0;\n queue<pair<int,bool>> bfsq;\n vector<bool> vis(n,false);\n for(auto &x : connections)\n {\n adj[x[0]].push_...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
0
{ "code": "class Solution {\npublic:\nint count=0;\nvoid dfs(int src,int par,vector<pair<int,int>>adj[]){\n for(auto it:adj[src]){\n int node=it.first;\n int check=it.second;\n if(node!=par){\n if(check==1){\n count++;\n }\n dfs(node,src,ad...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
0
{ "code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<int> adj[n];\n vector<int> rvadj[n];\n\n for(auto connection : connections)\n {\n adj[connection[0]].push_back(connection[1]);\n rvadj[connection[1]].push_back...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
1
{ "code": "class Solution {\npublic:\n //if an edge is outgoing from current location, we will have to flip it :(. therefore res++ in this case.\n int dfs(int s, vector<vector<int>>& outgoing, vector<vector<int>>& incoming, int parent){\n int res = 0;\n for(int i : outgoing[s]){\n if(pa...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
1
{ "code": "#pragma GCC optimize(\"Ofast,no-stack-protector\")\n#pragma GCC optimize(\"no-math-errno,unroll-loops\")\n#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4\")\n\nstatic int speedup = []() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n\n constexpr size_t cacheLineSi...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
1
{ "code": "class Solution {\npublic:\n\n int dfs(vector<vector<int>>& in, vector<vector<int>>& out, \n vector<int>& visited, int node)\n {\n int cnt = 0;\n visited[node] = 1;\n for(auto& val : out[node])\n {\n if(visited[val] ==0)\n {\n ...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
1
{ "code": "class Solution {\npublic:\n // this is a question of minimum path by using 0,1 bfs\n int minReorder(int n, vector<vector<int>>& edges) {\n int c=0;\n vector<int> dist(n, n);\n vector<int> visited(n, 0);\n vector<pair<int, bool>> adj[n];\n for (int i = 0; i < edges.s...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
1
{ "code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<vector<pair<int, int>>> adj(n);\n for(auto c:connections){\n adj[c[0]].emplace_back(c[1], 1);\n adj[c[1]].emplace_back(c[0], 0);\n }\n queue<int> q;\n v...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
1
{ "code": "class Solution {\npublic:\n vector<int> gp[100000+10];\n vector<int> gp2[100000+10];\n int cnt = 0;\n \n void bfs(int source,vector<bool> &visited,vector<int> &store)\n {\n queue<int> q;\n q.push(source);\n \n visited[source] = true;\n while(!q.empty())\n ...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
1
{ "code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& con) \n {\n bool visited[n];\n vector<int> adj[n];\n set<pair<int,int>>s;\n memset(visited,false,sizeof(visited));\n for(int i=0;i<con.size();i++)\n {\n adj[con[i][0]].push_back...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
1
{ "code": "class Solution {\npublic:\n vector<int> adj[50005];\n bool visited[50005];\n set<pair<int, int>> s;\n\n int minReorder(int n, vector<vector<int>>& con) {\n memset(visited, false, sizeof(visited));\n \n // Build the adjacency list and set of directed roads\n for(int i...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
1
{ "code": "class Solution {\nprivate:\n int dfs(pair<int, int>& currPair, vector<pair<int, int>> adjList[], int n, vector<int>& vis) {\n int currNode = currPair.first;\n int currWt = currPair.second;\n vis[currNode] = 1;\n\n int total = 0;\n\n for (auto nei : adjList[currNode]) {...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
1
{ "code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& edges) {\n vector<int> adj[n];\n set<pair<int,int>> s;\n for(int i=0;i<edges.size();i++){\n adj[edges[i][0]].push_back(edges[i][1]);\n adj[edges[i][1]].push_back(edges[i][0]);\n s...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
1
{ "code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<vector<int>> adjList(n); \n vector<vector<int>> revList(n); \n\n for (auto& conn : connections) {\n int u = conn[0], v = conn[1];\n adjList[u].push_back(v); \n ...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
1
{ "code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<vector<int>> adjList(n); \n vector<vector<int>> revList(n); \n\n for (auto& conn : connections) {\n int u = conn[0], v = conn[1];\n adjList[u].push_back(v); \n ...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
1
{ "code": "class Solution {\npublic:\n void dfs(vector<vector<int>>& graph, vector<bool>& visited, int from, int& change) {\n visited[from] = true;\n\n for (auto to : graph[from]) {\n if (!visited[abs(to)]) {\n if (to > 0)\n change++;\n dfs(...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
1
{ "code": "class Solution {\npublic:\n int res;\n void dfs(vector<vector<int>>& adj, vector<bool>& visit, int index){\n //if(visit[index] == true) return;\n visit[index] = true;\n for(int i = 0; i < adj[index].size(); i++){\n if(visit[abs(adj[index][i])] == false){\n ...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
2
{ "code": "class Solution {\npublic:\n void dfs(int node, vector<vector<pair<int, int>>>& adj, vector<bool>& visited, int& count){\n visited[node] = true;\n for(pair<int, int> connection: adj[node]){\n int neighbor = connection.first;\n int direction = connection.second;\n\n ...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
2
{ "code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<vector<pair<int, bool>>> adj(n);\n vector<bool> v(n, false);\n v[0] = true;\n int ans = 0;\n for(auto c: connections)\n {\n adj[c[0]].push_back({c[1], false...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
2
{ "code": "class Solution {\npublic:\n int dfs(int src,vector<int>adj[],set<pair<int,int> >&edges,vector<int>&vis)\n {\n vis[src]=1;\n\n int ans = 0;\n\n for(auto adj_vertex:adj[src])\n {\n if(!vis[adj_vertex])\n {\n ans+=dfs(adj_vertex,adj,edges,...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
2
{ "code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n \n unordered_map<int,vector<int> > adj;\n for(int i=0;i<connections.size();i++){\n int u=connections[i][0];\n int v=connections[i][1];\n adj[u].push_back(v);\n ...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
2
{ "code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<int> g[n];\n vector<int> temp[n];\n\n for(int i=0;i<connections.size();i++){\n g[connections[i][0]].push_back(connections[i][1]);\n temp[connections[i][0]].push_back(...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
2
{ "code": "class Solution {\npublic:\nvoid reOrderDFS(vector<vector<int>>& adj, vector<vector<int>>& adjOrig, int V, int prev, int& reversals){\n for(auto neighbor: adjOrig[V]){\n if(neighbor!=prev){\n if (find(adj[V].begin(), adj[V].end(), neighbor) != adj[V].end()){\n reversals++...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
2
{ "code": "// dfs - \"there is only one way to travel between two different cities\"\n// start from 0, try to reach all other nodes\n// use +-1 to record the edge direction\n// time O(n), space O(n^2 + recursion depth)\n\nclass Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n ...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
2
{ "code": "\nclass Solution {\npublic:\n int dfs(int node, int prev, unordered_map<int, vector<pair<int, int>>>& adj) {\n int ans = 0;\n for (auto& nei : adj[node]) {\n int v = nei.first;\n int w = nei.second;\n if (v == prev) continue; // Skip the parent node\n ...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
2
{ "code": "\nclass Solution {\npublic:\n int dfs(int node, int prev, unordered_map<int, vector<pair<int, int>>>& adj) {\n int ans = 0;\n for (auto& nei : adj[node]) {\n int v = nei.first;\n int w = nei.second;\n if (v == prev) continue; // Skip the parent node\n ...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
2
{ "code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n unordered_map<int, vector<int>> g;\n unordered_map<int, vector<int>> gReversed;\n vector<bool> notSeen(n, true);\n stack<int> stack;\n int ans = 0;\n\n for (const vector<int>...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
2
{ "code": "class Solution {\npublic:\n int res = 0;\n set<pair<int, int>> roads;\n vector<int> visited;\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<int> parent(n);\n vector<vector<int>> adj(n);\n visited.resize(n);\n for (int i = 0; i < connections.size...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
2
{ "code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n unordered_map<int, vector<pair<int,bool>>> adj(n);\n\n for (vector<int>& connection : connections) {\n adj[connection[0]].push_back(make_pair(connection[1], true));\n adj[connectio...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
2
{ "code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& con) {\n unordered_map<int,vector<pair<int,int>>>adj;\n for(auto i:con){\n adj[i[0]].push_back({i[1],1});\n adj[i[1]].push_back({i[0],0});\n }\n vector<int>vis(n,0);\n queue<pa...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
2
{ "code": "class Solution {\nprivate:\n std::unordered_map<int, std::vector<std::pair<int, int>>> graph;\n int dfs(int node, int prevNode) {\n int flips = 0;\n for (int i = 0; i < graph[node].size(); i++) {\n int nextNode = graph[node][i].first;\n if (nextNode != prevNode) {\...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
2
{ "code": "class Solution {\npublic:\n\n\n void dfs(unordered_map<int,vector<int>> &mp, vector<int> &vis, int &c,int city){\n vis[city]=1;\n for(int i=0;i<mp[city].size();i++){\n if( !vis[abs(mp[city][i])] ) \n { \n if( mp[city][i]>0)\n c++;\n ...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
2
{ "code": "class Solution {\npublic:\n int ans = 0;\n\n void dfs(int node, unordered_map<int, list<pair<int, bool>>>& adj, vector<bool>& visited) {\n visited[node] = true; // Mark the current node as visited\n\n // Traverse all neighbors of the current node\n for (auto& neighbor : adj[node...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
2
{ "code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) \n {\n unordered_map<int, vector<pair<int, bool>>> nodeToNeighbors;\n\n for (auto const& connection: connections)\n {\n nodeToNeighbors[connection[0]].emplace_back(connection[1], true);...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
2
{ "code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n unordered_map<int, vector<pair<int, bool>>> city_map;\n for (const auto& cxn : connections) {\n city_map[cxn[0]].push_back(make_pair(cxn[1], true));\n city_map[cxn[1]].push_back(ma...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
2
{ "code": "class Solution {\npublic:\n\n int solve(unordered_map<int, vector<pair<int, int> > >& adj)\n {\n queue<int> q;\n unordered_map<int, int> vis;\n\n q.push(0);\n vis[0] = true;\n int ans = 0;\n\n while(!q.empty())\n {\n int node = q.front();\n ...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
2
{ "code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n unordered_map<int , vector<pair<int , int>>> graph;\n for (auto& connection : connections) {\n int start = connection [0]; int end = connection[1];\n graph[start].push_back(make_pair(end, 1));\n...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
2
{ "code": "class Solution {\npublic:\n int BFS(set<pair<int,int>>& edges, vector<vector<int>>& adj){\n int mnEdges = 0;\n queue<int>q;\n vector<bool>vis(adj.size(), false);\n q.push(0);\n vis[0] = true;\n while(!q.empty()){\n int sz = q.size();\n whil...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
3
{ "code": "class Solution {\n unordered_map<int, vector<int>> roads;\n unordered_map<int, bool> visited;\n int ans = 0;\n void iter_reorder(vector<vector<int>>& connections, int n_city)\n {\n visited[n_city] = true;\n for(size_t i=0;i<roads[n_city].size();i++)\n {\n if(v...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
3
{ "code": "class Solution {\npublic:\n void dfs(int src, unordered_map<int, vector<int>> &graph, int &changedNodes, vector<bool> &vis){\n vis[src] = true;\n\n for(int nbr : graph[src]){\n if(nbr >= 0){\n if(!vis[nbr]){\n dfs(nbr, graph, changedNodes, vis);...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
3
{ "code": "// 1466. Reorder Routes to Make All Paths Lead to the City Zero\nclass Solution {\npublic:\n unordered_map<int,vector<pair<int,int>> > mp;\n int minReorder(int n, vector<vector<int>>& connections) {\n for(auto x:connections){\n mp[x[0]].push_back({x[1],x[0]});\n mp[x[1]]....
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
3
{ "code": "class Solution {\npublic:\n\n void minReorderHelper(unordered_map<int, vector<pair<int, bool>>>& graph, vector<bool>& visited, int node, int& res) {\n if (visited[node]) return;\n\n visited[node] = true;\n\n \n\n for (auto neighbor: graph[node]) {\n if (!visited[ne...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
3
{ "code": "class Solution {\npublic:\n unordered_map<int, vector<pair<int,int>>> adj_list;\n unordered_set<int> visited;\n int count = 0;\n int minReorder(int n, vector<vector<int>>& connections) {\n for (auto & edge : connections) {\n adj_list[edge[0]].push_back(make_pair(ed...
1,576
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too nar...
3
{ "code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n // Graph representation\n vector<vector<int>> graph(n);\n // To track which edges need reordering\n unordered_set<string> directedEdges;\n\n // Build the graph and store directed ed...