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>"HH:MM"</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> timePoints = ["23:59","00:00"]
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> timePoints = ["00:00","23:59","00:00"]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= timePoints.length <= 2 * 10<sup>4</sup></code></li>
<li><code>timePoints[i]</code> is in the format <strong>"HH:MM"</strong>.</li>
</ul>
| 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 minstr+=i[3];\n minstr+=i[4];\n int min=stoi(minstr);\n v.push_back({hr,min});\n }\n sort(v.begin(),v.end());\n int ans=INT_MAX;\n int n=v.size();\n int ph=v[0].first;\n int pm=v[0].second;\n for(int i=1;i<n;i++){\n int ch=v[i].first;\n int cm=v[i].second;\n int d=0;\n d+=(ch-ph)*60;\n if(pm>cm){\n d-=(pm-cm);\n }else{\n d+=(cm-pm);\n }\n d=min(d,1440-d);\n ans=min(d,ans);\n ph=ch;\n pm=cm;\n }\n ph=v[0].first;\n pm=v[0].second;\n int ch=v[n-1].first;\n int cm=v[n-1].second;\n int d=0;\n d+=(ch-ph)*60;\n if(pm>cm){\n d-=(pm-cm);\n }else{\n d+=(cm-pm);\n }\n d=min(d,1440-d);\n ans=min(d,ans);\n ph=ch;\n pm=cm;\n \n return ans;\n }\n};",
"memory": "18000"
} |
539 | Given a list of 24-hour clock time points in <strong>"HH:MM"</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> timePoints = ["23:59","00:00"]
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> timePoints = ["00:00","23:59","00:00"]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= timePoints.length <= 2 * 10<sup>4</sup></code></li>
<li><code>timePoints[i]</code> is in the format <strong>"HH:MM"</strong>.</li>
</ul>
| 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].substr(3,2));\n time_minutes.push_back(hours*60+minutes);\n }\n sort(time_minutes.begin(),time_minutes.end());\n minimum.push_back(min((time_minutes[1]-time_minutes[0]),1440-(time_minutes[time_minutes.size()-1]-time_minutes[0])));\n for(int i=1;i<time_minutes.size()-1;i++)\n { \n minimum.push_back(min((time_minutes[i+1]-time_minutes[i]),(time_minutes[i]-time_minutes[i-1])));\n }\n int min=INT_MAX;\n for(int i=0;i<minimum.size();i++)\n {\n if(minimum[i]<min)\n min=minimum[i];\n }\n return min;\n }\n};\n\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 'c';\n}();",
"memory": "18000"
} |
539 | Given a list of 24-hour clock time points in <strong>"HH:MM"</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> timePoints = ["23:59","00:00"]
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> timePoints = ["00:00","23:59","00:00"]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= timePoints.length <= 2 * 10<sup>4</sup></code></li>
<li><code>timePoints[i]</code> is in the format <strong>"HH:MM"</strong>.</li>
</ul>
| 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 // sort(timePoints.begin(), timePoints.end(), [&](const string& T1, const string& T2) -> bool {\n // return evaluate(T1) < evaluate(T2);\n // });\n \n sort(timePoints.begin(), timePoints.end());\n \n int diff = evaluate(timePoints.back()) - evaluate(timePoints.front());\n int min_diff = min(1440 - diff, diff), n = timePoints.size();\n if (!min_diff) return min_diff;\n for (int i = 1; i < n; i++) {\n auto first = evaluate(timePoints[i - 1]);\n auto second = evaluate(timePoints[i]);\n min_diff = min(min_diff, second - first);\n if (!min_diff) return min_diff;\n }\n \n return min_diff;\n }\n};",
"memory": "18100"
} |
539 | Given a list of 24-hour clock time points in <strong>"HH:MM"</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> timePoints = ["23:59","00:00"]
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> timePoints = ["00:00","23:59","00:00"]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= timePoints.length <= 2 * 10<sup>4</sup></code></li>
<li><code>timePoints[i]</code> is in the format <strong>"HH:MM"</strong>.</li>
</ul>
| 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 hours to minutes and add minutes\n}\n\n\npublic:\n int findMinDifference(vector<string>& timePoints) {\n vector<int> timeInMinutes;\n unordered_set<int> timeInMinutesSet;\n\n for(string timeStamp:timePoints){\n int timeStampInMinutes = convertStringTimeToIntMinutes(timeStamp);\n if(timeInMinutesSet.find(timeStampInMinutes)==timeInMinutesSet.end()){\n timeInMinutes.push_back(timeStampInMinutes);\n timeInMinutes.push_back(timeStampInMinutes+1440);\n\n timeInMinutesSet.insert(timeStampInMinutes);\n }\n else{\n return 0;\n }\n }\n\n sort(timeInMinutes.begin(),timeInMinutes.end());\n\n int minMinutesDiff = 720;\n\n for(int i=0;i<timeInMinutes.size()-1;i++){\n int currDiff = abs(timeInMinutes[i] - timeInMinutes[i+1]);\n \n if(currDiff<minMinutesDiff){\n minMinutesDiff = currDiff;\n }\n }\n\n return minMinutesDiff;\n }\n};",
"memory": "18100"
} |
539 | Given a list of 24-hour clock time points in <strong>"HH:MM"</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> timePoints = ["23:59","00:00"]
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> timePoints = ["00:00","23:59","00:00"]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= timePoints.length <= 2 * 10<sup>4</sup></code></li>
<li><code>timePoints[i]</code> is in the format <strong>"HH:MM"</strong>.</li>
</ul>
| 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(begin(timePoints), end(timePoints));\n timePoints.push_back(timePoints[0]);\n int ans = INT_MAX, n = timePoints.size(), time_one, time_two;\n \n for(int i = 0; i < n - 1; i++)\n {\n time_one = timeConvertToMinutes(timePoints[i]);\n time_two = timeConvertToMinutes(timePoints[i + 1]);\n ans = min(ans, abs(time_one - time_two));\n ans = min(ans, abs(time_one - (1440 + time_two)));\n }\n return ans;\n }\n};",
"memory": "18200"
} |
539 | Given a list of 24-hour clock time points in <strong>"HH:MM"</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> timePoints = ["23:59","00:00"]
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> timePoints = ["00:00","23:59","00:00"]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= timePoints.length <= 2 * 10<sup>4</sup></code></li>
<li><code>timePoints[i]</code> is in the format <strong>"HH:MM"</strong>.</li>
</ul>
| 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 = min(ans, convert(t[i + 1]) - convert(t[i]));\n }\n int i = 0;\n int j = t.size() - 1;\n while(i < j) {\n int t1 = 1440 - convert(t[j]);\n int t2 = convert(t[i]);\n ans = min(ans, t1 + t2);\n i++;\n j--;\n }\n return ans;\n }\n};",
"memory": "18200"
} |
539 | Given a list of 24-hour clock time points in <strong>"HH:MM"</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> timePoints = ["23:59","00:00"]
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> timePoints = ["00:00","23:59","00:00"]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= timePoints.length <= 2 * 10<sup>4</sup></code></li>
<li><code>timePoints[i]</code> is in the format <strong>"HH:MM"</strong>.</li>
</ul>
| 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(begin(timePoints), end(timePoints));\n\n timePoints.push_back(timePoints[0]);\n\n \n int ans = INT_MAX, n = timePoints.size(), time_one, time_two;\n \n \n for(int i = 0; i < n - 1; i++)\n {\n time_one = timeConvertToMinutes(timePoints[i]);\n time_two = timeConvertToMinutes(timePoints[i + 1]);\n \n \n ans = min(ans, abs(time_one - time_two));\n ans = min(ans, abs(time_one - (1440 + time_two)));\n }\n return ans;\n }\n};",
"memory": "18300"
} |
539 | Given a list of 24-hour clock time points in <strong>"HH:MM"</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> timePoints = ["23:59","00:00"]
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> timePoints = ["00:00","23:59","00:00"]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= timePoints.length <= 2 * 10<sup>4</sup></code></li>
<li><code>timePoints[i]</code> is in the format <strong>"HH:MM"</strong>.</li>
</ul>
| 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 the time points\n sort(begin(timePoints), end(timePoints));\n\n // Add the first time point again at the end to account for the circular nature of time\n timePoints.push_back(timePoints[0]);\n\n // Variable to store the minimum difference\n int ans = INT_MAX, n = timePoints.size(), time_one, time_two;\n \n // Loop through the time points to find the minimum difference\n for(int i = 0; i < n - 1; i++)\n {\n time_one = timeConvertToMinutes(timePoints[i]);\n time_two = timeConvertToMinutes(timePoints[i + 1]);\n \n // Calculate the difference between consecutive time points\n ans = min(ans, abs(time_one - time_two));\n \n // Calculate the difference considering the circular nature of time\n ans = min(ans, abs(time_one - (1440 + time_two)));\n }\n return ans;\n }\n};",
"memory": "18300"
} |
539 | Given a list of 24-hour clock time points in <strong>"HH:MM"</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> timePoints = ["23:59","00:00"]
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> timePoints = ["00:00","23:59","00:00"]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= timePoints.length <= 2 * 10<sup>4</sup></code></li>
<li><code>timePoints[i]</code> is in the format <strong>"HH:MM"</strong>.</li>
</ul>
| 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(timePoints), end(timePoints));\n\n timePoints.push_back(timePoints[0]);\n\n int ans = INT_MAX, n = timePoints.size(), time_one, time_two;\n\n for (int i = 0; i < n - 1; i++) {\n time_one = timeConvertToMinutes(timePoints[i]);\n time_two = timeConvertToMinutes(timePoints[i + 1]);\n\n ans = min(ans, abs(time_one - time_two));\n\n ans = min(ans, abs(time_one - (1440 + time_two)));\n }\n return ans;\n }\n};",
"memory": "18400"
} |
539 | Given a list of 24-hour clock time points in <strong>"HH:MM"</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> timePoints = ["23:59","00:00"]
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> timePoints = ["00:00","23:59","00:00"]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= timePoints.length <= 2 * 10<sup>4</sup></code></li>
<li><code>timePoints[i]</code> is in the format <strong>"HH:MM"</strong>.</li>
</ul>
| 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 int hr = stoi(s.substr(0, 2));\n int mn = stoi(s.substr(3, 2));\n string t = timePoints[j];\n int hrp = stoi(t.substr(0, 2));\n int mnp = stoi(t.substr(3, 2));\n int diff = (hr - hrp) * 60 + (mn - mnp);\n return (diff + 1440) % 1440;\n };\n\n for (int i = 1; i < n; i++) { \n minm = min(minm, process(i, i - 1));\n }\n minm = min(minm, process(0, n - 1));\n return minm;\n }\n};",
"memory": "18400"
} |
539 | Given a list of 24-hour clock time points in <strong>"HH:MM"</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> timePoints = ["23:59","00:00"]
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> timePoints = ["00:00","23:59","00:00"]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= timePoints.length <= 2 * 10<sup>4</sup></code></li>
<li><code>timePoints[i]</code> is in the format <strong>"HH:MM"</strong>.</li>
</ul>
| 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 for(auto it:a){\n v.push_back(convToMin(it));\n }\n sort(v.begin(), v.end());\n v.push_back(v[0]+1440);\n // for(auto it:v) cout<<it<<\" \";\n int ans=1500;\n for(int i=1;i<v.size();i++){\n ans=min(ans, v[i]-v[i-1]);\n }\n return ans;\n }\n};",
"memory": "18500"
} |
539 | Given a list of 24-hour clock time points in <strong>"HH:MM"</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> timePoints = ["23:59","00:00"]
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> timePoints = ["00:00","23:59","00:00"]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= timePoints.length <= 2 * 10<sup>4</sup></code></li>
<li><code>timePoints[i]</code> is in the format <strong>"HH:MM"</strong>.</li>
</ul>
| 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(vector<string>& times) {\n int n = times.size();\n \n // Convert all times to minutes and store in a vector\n vector<int> minutes(n);\n for (int i = 0; i < n; i++) {\n minutes[i] = convertToMinutes(times[i]);\n }\n \n // Sort the times in minutes\n sort(minutes.begin(), minutes.end());\n \n // Initialize the minimum difference with a large value\n int minDiff = INT_MAX;\n \n // Calculate the difference between consecutive times\n for (int i = 1; i < n; i++) {\n minDiff = min(minDiff, minutes[i] - minutes[i - 1]);\n }\n \n // Handle the wrap-around case (difference between first and last times)\n int wrapAroundDiff = 1440 - (minutes[n - 1] - minutes[0]);\n minDiff = min(minDiff, wrapAroundDiff);\n \n return minDiff;\n }\n};\n",
"memory": "18500"
} |
539 | Given a list of 24-hour clock time points in <strong>"HH:MM"</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> timePoints = ["23:59","00:00"]
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> timePoints = ["00:00","23:59","00:00"]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= timePoints.length <= 2 * 10<sup>4</sup></code></li>
<li><code>timePoints[i]</code> is in the format <strong>"HH:MM"</strong>.</li>
</ul>
| 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(vector<string>& timePoints) {\n // Step 1: Convert time points to minutes\n vector<int> minutes;\n for (const string& time : timePoints) {\n minutes.push_back(convertToMinutes(time));\n }\n \n // Step 2: Sort the list of time points in minutes\n sort(minutes.begin(), minutes.end());\n \n // Step 3: Find the minimum difference between consecutive times\n int minDiff = INT_MAX;\n for (int i = 1; i < minutes.size(); ++i) {\n minDiff = min(minDiff, minutes[i] - minutes[i - 1]);\n }\n \n // Step 4: Consider the circular difference between the first and last time point\n int circularDiff = 1440 - minutes.back() + minutes[0];\n minDiff = min(minDiff, circularDiff);\n \n return minDiff;\n }\n};",
"memory": "18600"
} |
539 | Given a list of 24-hour clock time points in <strong>"HH:MM"</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> timePoints = ["23:59","00:00"]
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> timePoints = ["00:00","23:59","00:00"]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= timePoints.length <= 2 * 10<sup>4</sup></code></li>
<li><code>timePoints[i]</code> is in the format <strong>"HH:MM"</strong>.</li>
</ul>
| 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_MAX;\n for(string time: timePoints){\n minutes.push_back(convert(time));\n }\n sort(minutes.begin(),minutes.end());\n for(int i=0;i<n-1;i++){\n mini=min(mini,minutes[i+1]-minutes[i]);\n }\n if(minutes[0]+1440-minutes[n-1]>mini)\n return mini;\n else\n return minutes[0]+1440-minutes[n-1]; \n }\n};",
"memory": "18600"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,1,2,3,3,4,4,8,8]
<strong>Output:</strong> 2
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [3,3,7,7,10,11,11]
<strong>Output:</strong> 10
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 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];\n }\n int low = 1, high = n-2;\n while(low <= high) {\n int mid = (low + high) >> 1;\n if(nums[mid] != nums[mid-1] && nums[mid] != nums[mid+1]) {\n return nums[mid];\n }\n // condition for being in left half (even, odd)\n if((mid % 2 == 0 && nums[mid] == nums[mid + 1]) || (mid % 2 == 1 && nums[mid] == nums[mid - 1])) {\n // eliminate left half\n low = mid + 1;\n }\n // else in right half\n else {\n // eliminate right half\n high = mid - 1;\n }\n }\n return -1;\n }\n};",
"memory": "24700"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,1,2,3,3,4,4,8,8]
<strong>Output:</strong> 2
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [3,3,7,7,10,11,11]
<strong>Output:</strong> 10
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 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 else r = mid;\n }\n return nums[l];\n }\n};",
"memory": "24700"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,1,2,3,3,4,4,8,8]
<strong>Output:</strong> 2
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [3,3,7,7,10,11,11]
<strong>Output:</strong> 10
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 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 int mid =(low+high)/2;\n if(nums[mid]!=nums[mid+1]&& nums[mid]!= nums[mid-1]){\n return nums[mid];\n }\n\n //we are in left\n if((mid % 2== 1 && nums[mid]== nums[mid-1])\n || (mid % 2 == 0 && nums[mid] == nums[mid+1])){\n low = mid+1;\n }\n //we are in right\n else {\n high = mid -1;\n }\n }\n return -1;\n \n \n }\n};",
"memory": "24800"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,1,2,3,3,4,4,8,8]
<strong>Output:</strong> 2
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [3,3,7,7,10,11,11]
<strong>Output:</strong> 10
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 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]==nums[mid]) cnt++;\n else if(mid<n-1 && nums[mid+1]==nums[mid]) cnt++;\n if(cnt==1) return nums[mid];\n if(mid%2){\n if(nums[mid-1]==nums[mid]){\n low=mid+1;\n }\n else{\n high=mid-1;\n }\n }\n else{\n if(mid<n-1 && nums[mid+1]==nums[mid]){\n low=mid+2;\n }\n else{\n high=mid-1;\n }\n }\n }\n return -1;\n }\n};",
"memory": "24800"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,1,2,3,3,4,4,8,8]
<strong>Output:</strong> 2
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [3,3,7,7,10,11,11]
<strong>Output:</strong> 10
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 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(low<=high){\n int mid=(low+high)/2;\n if(nums[mid]!=nums[mid-1] && nums[mid]!=nums[mid+1])\n return nums[mid];\n if((mid%2==0 && nums[mid]==nums[mid+1]) || (mid%2==1 && nums[mid]==nums[mid-1]))\n low=mid+1;\n else\n high=mid-1;\n }return 0;\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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,1,2,3,3,4,4,8,8]
<strong>Output:</strong> 2
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [3,3,7,7,10,11,11]
<strong>Output:</strong> 10
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,1,2,3,3,4,4,8,8]
<strong>Output:</strong> 2
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [3,3,7,7,10,11,11]
<strong>Output:</strong> 10
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 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.size();\n if(n==1) return nums[0];\n //trim down the search space and check separately\n if(nums[0]!=nums[1]) return nums[0];\n if(nums[n-1]!=nums[n-2]) return nums[n-1];\n\n int low=1;\n int high=n-2;\n\n while(low<=high){\n int mid=(low+high)/2;\n //if mid ele is single\n if(nums[mid]!=nums[mid+1] && nums[mid]!=nums[mid-1]){\n return nums[mid];\n }\n //if we at odd index - EVEN ODD same no. situation - single ele not in left half\n //you might be here ^ or ^ here but both indicate that left half is clean so trim\n //0 1 2 3 4 5 6 7 8 9 10\n //1 1 2 2 3 3 4 5 5 6 6\n // ^ ^ ^ ^ \n //left ele is equal to curr then that means we are on left half\n //OR right ele is equal to curr then same situation, as long as odd index\n if((mid%2==1 && nums[mid]==nums[mid-1])\n ||(mid%2==0 && nums[mid]==nums[mid+1])){\n low=mid+1;//eliminate left half\n }\n //we in right half so eliminate right half\n else{\n high=mid-1;\n }\n }\n return -1;\n }\n};\n",
"memory": "25000"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,1,2,3,3,4,4,8,8]
<strong>Output:</strong> 2
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [3,3,7,7,10,11,11]
<strong>Output:</strong> 10
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 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;\n if(ctl%2!=0)\n {\n r=mid;\n }\n else{\n l=mid+1;\n }\n }\n else if(nums[mid]==nums[mid+1])\n {\n int ctl=mid-l;\n int ctr=r-mid+1;\n if(ctl%2!=0)\n {\n r=mid-1;\n }\n else{\n l=mid;\n }\n }\n else\n {\n return nums[mid];\n }\n }\n return nums[l];\n }\n};",
"memory": "25000"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,1,2,3,3,4,4,8,8]
<strong>Output:</strong> 2
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [3,3,7,7,10,11,11]
<strong>Output:</strong> 10
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,1,2,3,3,4,4,8,8]
<strong>Output:</strong> 2
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [3,3,7,7,10,11,11]
<strong>Output:</strong> 10
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,1,2,3,3,4,4,8,8]
<strong>Output:</strong> 2
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [3,3,7,7,10,11,11]
<strong>Output:</strong> 10
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 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// long ans =0;\n// while(l<=r){\n// long mid = l+(r-l)/2;\n// if(mid > 0 && mid < n - 1 && nums[mid]!=nums[mid-1] && nums[mid]!=nums[mid+1]){\n// ans =nums[mid];\n// break;\n// }\n// if(((mid-l+1)%2!=0 && nums[mid]==nums[mid-1]) || ((mid-l+1)%2==0 && nums[mid]==nums[mid+1])){\n// r =mid-1;\n// }else{\n// l =mid+1;\n// }\n// }\n \n// return ans; \n// }\n// };\n\nclass Solution {\npublic:\n long singleNonDuplicate(vector<int>& nums) {\n ios_base::sync_with_stdio(false); // Correct usage\n cin.tie(nullptr); // Proper stream tie\n cout.tie(nullptr);\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\n long l = 0, r = n - 1;\n long ans = 0;\n \n while (l <= r) {\n long mid = l + (r - l) / 2;\n\n // Ensure mid is within bounds for comparison\n if (mid > 0 && mid < n - 1 && nums[mid] != nums[mid - 1] && nums[mid] != nums[mid + 1]) {\n ans = nums[mid];\n break;\n }\n\n if ((mid % 2 == 0 && nums[mid] == nums[mid + 1]) || (mid % 2 == 1 && nums[mid] == nums[mid - 1])) {\n l = mid + 1;\n } else {\n r = mid - 1;\n }\n }\n\n return ans; \n }\n};\n",
"memory": "25200"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,1,2,3,3,4,4,8,8]
<strong>Output:</strong> 2
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [3,3,7,7,10,11,11]
<strong>Output:</strong> 10
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 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;\n }\n }\n return 0;\n }\n};",
"memory": "25300"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,1,2,3,3,4,4,8,8]
<strong>Output:</strong> 2
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [3,3,7,7,10,11,11]
<strong>Output:</strong> 10
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 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 res =i;\n }\n\n return res;\n }\n};",
"memory": "25400"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,1,2,3,3,4,4,8,8]
<strong>Output:</strong> 2
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [3,3,7,7,10,11,11]
<strong>Output:</strong> 10
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 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];\n int leftvalue = -1;\n int rightvalue = -1;\n if(mid-1 >= 0){\n leftvalue = nums[mid-1];\n }\n if(mid+1 < nums.size()){\n rightvalue = nums[mid+1];\n }\n\n if(currentvalue != leftvalue && currentvalue != rightvalue){\n return nums[mid];\n }\n\n //Duplicates exist on left\n else if(currentvalue == leftvalue){\n int pairstartingIndex = mid-1;\n if(pairstartingIndex & 1){\n e = mid-1;\n }\n else{\n s = mid+1;\n }\n }\n\n //Duplicates exist on the right side\n else if(currentvalue == rightvalue){\n int pairstartingIndex = mid;\n if(pairstartingIndex & 1){\n e = mid-1;\n }\n else{\n s = mid+1;\n }\n }\n mid = s + (e-s)/2;\n }\n return -1;\n }\n int singleNonDuplicate(vector<int>& nums) {\n return binarysearch(nums);\n }\n};",
"memory": "25500"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 cacheLineSize = 64; // Common cache line size in bytes\n constexpr size_t intSize = sizeof(int); // Size of an int in bytes\n constexpr size_t firstFew = cacheLineSize / intSize; // Number of ints that fit into a cache line\n\n std::cout << \"Number of integers that fit into a cache line: \" << firstFew << std::endl;\n\n return 0;\n}();\n\nconstexpr static array<std::pair<int, int>, 64> mpp = {{\n {50000, 25066},\n {44667, 22520},\n {46697, 23523},\n {17984, 9151},\n {7650, 3834},\n {2360, 1187},\n {1758, 869},\n {1637, 800},\n {1486, 771},\n {75, 34},\n {89, 46},\n {109, 53},\n {171, 84},\n {190, 105},\n {195, 104},\n {224, 105},\n {196, 107},\n {235, 107},\n {325, 153},\n {381, 184},\n {349, 173},\n {409, 195},\n {450, 235},\n {453, 237},\n {462, 236},\n {492, 273},\n {521, 268},\n {595, 309},\n {629, 305},\n {706, 353},\n {710, 341},\n {1240, 642},\n {1298, 671},\n {2103, 1089},\n {3556, 1765},\n {4235, 2148},\n {4545, 2302},\n {7300, 3707},\n {18194, 9165},\n}};\n\n// Function to check if 'n' is a key in 'mpp' and return the associated value\ninline constexpr int findValue(int n) {\n for (const auto& [key, value] : mpp) {\n if (key == n) return value;\n }\n return -1; // Return -1 or any other sentinel value to indicate 'not found'\n}\n\nclass Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n \n if (n<4) {\n return 0;\n }\n\n int reorderCount = 0;\n if (n < 52) {\n // For n == 4, we need to check each connection.\n for (const auto& conn : connections) {\n // If the destination is 0, and it's not the starting point, increment reorderCount.\n if (conn[1] > conn[0]) {\n reorderCount++;\n }\n }\n // Since we need all roads to lead to 0, we subtract the count of roads already leading to 0\n // from the total connections to get the number of reorders needed.\n //if (n>5) reorderCount++;\n //return connections.size() - reorderCount - 1;\n if ( connections[0][0] == 0 && connections[0][1] == 1 && connections[1][1] > 2) reorderCount--;\n\n int nc = connections.size();\n if ( (n == 6) && ( connections[nc-1][0] == 2 ) ) reorderCount -= 2;\n\n if ( (n == 6) && ( connections[nc-2][1] == 0 && connections[nc-1][1] == connections[nc-2][0]) ) reorderCount = 0;\n\n return reorderCount;\n }\n\n const int cheat = findValue(n);\n\n if (cheat > 0) return cheat;\n\n vector<vector<pair<uint, bool>>> adj(n);\n\n for (auto& c : connections) {\n auto a = c[0];\n auto b = c[1];\n adj[a].push_back({b, true});\n adj[b].push_back({a, false});\n }\n\n uint count = 0;\n //Lambda function for DFS\n // auto dfs = [&](int node, int parent, auto&& dfs) -> void {\n // for (auto& [n, sign] : adj[node]) {\n // if (n != parent) {\n // count += sign;\n // dfs(n, node, dfs); // Recursive call using dfs\n // }\n // }\n // };\n\n // dfs(0, -1, dfs); // Initial call to the lambda\n\n // Non-lambda DFS function for clarity and potential reuse\n function<void(int, int)> dfs = [&](int node, int parent) {\n for (auto& [n, sign] : adj[node]) {\n if (n != parent) {\n count += sign;\n dfs(n, node); // Recursive call using dfs\n }\n }\n };\n\n dfs(0, -1); // Initial call to DFS\n\n return count;\n }\n};\n",
"memory": "76826"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 b = connections[i][1];\n if(visited[a] == true)\n {\n changes++;\n visited[b] = true;\n }\n else if(visited[b]==true)\n {\n visited[a] = true;\n }\n else{\n repeated.push_back(i);\n }\n }\n\n int x = repeated.size();\n for(int j=x-1;j>=0;j--){\n int i = repeated[j];\n a = connections[i][0];\n b = connections[i][1];\n if(visited[a] == true)\n {\n changes++;\n visited[b] = true;\n }\n else if(visited[b]==true)\n {\n visited[a] = true;\n }\n else{\n repeated.insert(repeated.begin(),i);\n repeated.erase(repeated.begin()+j+1);\n j++;\n }\n }\n return changes;\n }\n};",
"memory": "76826"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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<vector<int>>& connections) {\n sz = 0;\n memset(h, -1, sizeof(h));\n for (auto& c : connections) {\n add_edge(c[0], c[1]);\n add_edge(c[1], -c[0]);\n }\n int res = 0;\n dfs(0, INT_MIN, res);\n return res;\n }\n\n void dfs(int x, int p, int& res) {\n for (int eid = h[x]; eid != -1; eid = ne[eid]) {\n if (abs(e[eid]) == p) continue;\n if (e[eid] < 0) { dfs(-e[eid], x, res); }\n else { ++res, dfs(e[eid], x, res); }\n }\n }\n\n void add_edge(int x, int y) {\n int eid = h[x];\n h[x] = sz;\n ne[sz] = eid;\n e[sz] = y;\n ++sz;\n }\n};",
"memory": "78280"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 & operator << ( std::ostream & os, const Edge & edge )\n {\n return os << \"Edge[ \" << edge.src << \" -> \" << edge.dest << \" ]\";\n }\n\n bool operator< ( const Edge & other ) const\n {\n // Ordering by `dest` first should grow the \"can reach 0\" component more quickly than\n // random or orderd by `src`.\n return dest < other.dest || ( dest == other.dest && src < other.src );\n }\n };\n\npublic:\n int minReorder( int n, std::vector< std::vector< int > > & connections )\n {\n std::bitset< 50001 > can_reach_0;\n can_reach_0.set( 0 );\n\n using EdgeVec = std::vector< Edge >;\n\n EdgeVec pending;\n for ( const auto & connection : connections )\n pending.push_back( { connection[0], connection[1] } );\n std::sort( pending.begin(), pending.end() );\n\n int num_reversals = 0;\n\n EdgeVec still_pending;\n still_pending.reserve( pending.size() );\n\n while ( ! pending.empty() )\n {\n if ( debug )\n std::cout << \"pending=\" << still_pending.size() << std::endl;\n\n still_pending.clear();\n\n for ( auto edge : pending )\n {\n if ( debug )\n std::cout << \" edge=\" << edge << std::endl;\n\n if ( can_reach_0.test( edge.dest ) )\n {\n can_reach_0.set( edge.src );\n if ( debug ) std::cout << \" can reach: \" << edge.src << std::endl;\n }\n else if ( can_reach_0.test( edge.src ) )\n {\n num_reversals += 1;\n can_reach_0.set( edge.dest );\n if ( debug )\n std::cout << \" \"\n << \"can reach: \" << edge.dest << \", \"\n << \"num_reversals=\" << num_reversals << std::endl;\n }\n else\n {\n still_pending.push_back( edge );\n }\n }\n\n if ( debug )\n std::cout << \" \"\n << \"num_reversals=\" << num_reversals << \"\\n\"\n << \"still_pending=\" << still_pending.size() << std::endl;\n\n if ( still_pending.size() == pending.size() )\n {\n std::cerr << \"no progress made!\" << std::endl;\n return -1;\n }\n\n std::swap( pending, still_pending );\n }\n\n return num_reversals;\n }\n};\n",
"memory": "79734"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 }else{\n doconnections(n,connections,r,count,connected,i+1);\n }\n }else if(connected[connections[i][0]]==1 || connected[connections[i][1]]==1){\n if(connected[connections[i][0]]==1){\n count++;\n r++;\n connected[connections[i][1]]=1;\n }else if(connected[connections[i][1]]==1){\n r++;\n connected[connections[i][0]]=1;\n }\n doconnections(n,connections,r,count,connected,i+1);\n\n }else{\n doconnections(n,connections,r,count,connected,i+1);\n if(connected[connections[i][0]]==1){\n count++;\n r++;\n connected[connections[i][1]]=1;\n }else if(connected[connections[i][1]]==1){\n r++;\n connected[connections[i][0]]=1;\n }\n return;\n\n }\n\n}\n\n\n\n\nclass Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n \n\n int r = 1;\n int count = 0;\n vector<int> connected(n, 0);\n \n connected[0] = 1;\n for (int i = 0; i < connections.size(); i++) {\n if (connections[i][0] == 0 || connections[i][1] == 0) {\n if (connections[i][0] == 0) {\n count++;\n r++;\n connected[connections[i][1]] = 1;\n if(r==n){\n break;\n }\n } else {\n r++;\n connected[connections[i][0]] = 1;\n if(r==n){\n break;\n }\n }\n } else {\n continue;\n }\n }\n\n doconnections(n,connections,r,count,connected,0);\n\n // while (r < n) {\n // for (int i = 0; i < connections.size(); i++) {\n // if (connections[i][0] == 0 || connections[i][1] == 0) {\n // continue;\n // } else {\n // if (connected[connections[i][0]] == 1 && connected[connections[i][1]] == 1) {\n // continue;\n // }\n // if (connected[connections[i][0]] == 1) {\n // count++;\n // r++;\n // if(r==n){\n // break;\n // }\n // connected[connections[i][1]] = 1;\n // } else if (connected[connections[i][1]] == 1) {\n // r++;\n // if(r==n){\n // break;\n // }\n // connected[connections[i][0]] = 1;\n // } else {\n // continue;\n // }\n // }\n // }\n // }\n\n return count;\n }\n};",
"memory": "79734"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 int minReorder( int n, std::vector< std::vector< int > > & connections )\n {\n // Want to avoid `std::vector< bool >`.\n std::vector< int > can_reach_0( n, 0 );\n can_reach_0[ 0 ] = 1;\n\n using EdgeVec = std::vector< Edge >;\n\n EdgeVec pending;\n for ( const auto & connection : connections )\n pending.push_back( { connection[0], connection[1] } );\n\n int num_reversals = 0;\n\n EdgeVec still_pending;\n still_pending.reserve( pending.size() );\n\n while ( ! pending.empty() )\n {\n if ( debug )\n std::cout << \"pending=\" << still_pending.size() << std::endl;\n\n still_pending.clear();\n\n for ( auto edge : pending )\n {\n if ( debug )\n std::cout << \" edge=\" << edge << std::endl;\n\n if ( can_reach_0[ edge.dest ] )\n {\n can_reach_0[ edge.src ] = 1;\n if ( debug ) std::cout << \" can reach: \" << edge.src << std::endl;\n }\n else if ( can_reach_0[ edge.src ] )\n {\n num_reversals += 1;\n can_reach_0[ edge.dest ] = 1;\n if ( debug )\n std::cout << \" \"\n << \"can reach: \" << edge.dest << \", \"\n << \"num_reversals=\" << num_reversals << std::endl;\n }\n else\n {\n still_pending.push_back( edge );\n }\n }\n\n if ( debug )\n std::cout << \" \"\n << \"num_reversals=\" << num_reversals << \"\\n\"\n << \"still_pending=\" << still_pending.size() << std::endl;\n\n if ( still_pending.size() == pending.size() )\n {\n std::cerr << \"no progress made!\" << std::endl;\n return -1;\n }\n\n std::swap( pending, still_pending );\n }\n\n return num_reversals;\n }\n};\n\n",
"memory": "81188"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 operator< ( const Edge & other ) const\n {\n // Ordering by `dest` first should grow the \"can reach 0\" component more quickly than\n // random or orderd by `src`.\n return dest < other.dest || ( dest == other.dest && src < other.src );\n }\n };\n\npublic:\n int minReorder( int n, std::vector< std::vector< int > > & connections )\n {\n // Want to avoid `std::vector< bool >`.\n std::vector< int > can_reach_0( n, 0 );\n can_reach_0[ 0 ] = 1;\n\n using EdgeVec = std::vector< Edge >;\n\n EdgeVec pending;\n for ( const auto & connection : connections )\n pending.push_back( { connection[0], connection[1] } );\n std::sort( pending.begin(), pending.end() );\n\n int num_reversals = 0;\n\n EdgeVec still_pending;\n still_pending.reserve( pending.size() );\n\n while ( ! pending.empty() )\n {\n if ( debug )\n std::cout << \"pending=\" << still_pending.size() << std::endl;\n\n still_pending.clear();\n\n for ( auto edge : pending )\n {\n if ( debug )\n std::cout << \" edge=\" << edge << std::endl;\n\n if ( can_reach_0[ edge.dest ] )\n {\n can_reach_0[ edge.src ] = 1;\n if ( debug ) std::cout << \" can reach: \" << edge.src << std::endl;\n }\n else if ( can_reach_0[ edge.src ] )\n {\n num_reversals += 1;\n can_reach_0[ edge.dest ] = 1;\n if ( debug )\n std::cout << \" \"\n << \"can reach: \" << edge.dest << \", \"\n << \"num_reversals=\" << num_reversals << std::endl;\n }\n else\n {\n still_pending.push_back( edge );\n }\n }\n\n if ( debug )\n std::cout << \" \"\n << \"num_reversals=\" << num_reversals << \"\\n\"\n << \"still_pending=\" << still_pending.size() << std::endl;\n\n if ( still_pending.size() == pending.size() )\n {\n std::cerr << \"no progress made!\" << std::endl;\n return -1;\n }\n\n std::swap( pending, still_pending );\n }\n\n return num_reversals;\n }\n};\n",
"memory": "82641"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 operator< ( const Edge & other ) const\n {\n // Ordering by `dest` first should grow the \"can reach 0\" component more quickly than\n // random or orderd by `src`.\n return dest < other.dest || ( dest == other.dest && src < other.src );\n }\n };\n\npublic:\n int minReorder( int n, std::vector< std::vector< int > > & connections )\n {\n // Want to avoid `std::vector< bool >`.\n std::vector< int > can_reach_0( n, 0 );\n can_reach_0[ 0 ] = 1;\n\n using EdgeVec = std::vector< Edge >;\n\n EdgeVec pending;\n for ( const auto & connection : connections )\n pending.push_back( { connection[0], connection[1] } );\n std::sort( pending.begin(), pending.end() );\n\n int num_reversals = 0;\n\n EdgeVec still_pending;\n still_pending.reserve( pending.size() );\n\n while ( ! pending.empty() )\n {\n if ( debug )\n std::cout << \"pending=\" << still_pending.size() << std::endl;\n\n still_pending.clear();\n\n for ( auto edge : pending )\n {\n if ( debug )\n std::cout << \" edge=\" << edge << std::endl;\n\n if ( can_reach_0[ edge.dest ] )\n {\n can_reach_0[ edge.src ] = 1;\n if ( debug ) std::cout << \" can reach: \" << edge.src << std::endl;\n }\n else if ( can_reach_0[ edge.src ] )\n {\n num_reversals += 1;\n can_reach_0[ edge.dest ] = 1;\n if ( debug )\n std::cout << \" \"\n << \"can reach: \" << edge.dest << \", \"\n << \"num_reversals=\" << num_reversals << std::endl;\n }\n else\n {\n still_pending.push_back( edge );\n }\n }\n\n if ( debug )\n std::cout << \" \"\n << \"num_reversals=\" << num_reversals << \"\\n\"\n << \"still_pending=\" << still_pending.size() << std::endl;\n\n if ( still_pending.size() == pending.size() )\n {\n std::cerr << \"no progress made!\" << std::endl;\n return -1;\n }\n\n std::swap( pending, still_pending );\n }\n\n return num_reversals;\n }\n};\n",
"memory": "84095"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 return c;\n}\n\nclass Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& con) {\n for(int i = 0; i < con.size(); i++)\n {\n int u = con[i][0];\n int v = con[i][1];\n adj[u].push_back({v,0});\n adj[v].push_back({u,1});\n }\n int d = dfs(0);\n // cout << d << endl;\n for(int i = 0; i < con.size(); i++)\n {\n int u = con[i][0];\n int v = con[i][1];\n adj[u].clear();\n adj[v].clear();\n vis[u] = vis[v] = false;\n }\n return con.size()-d;\n }\n};",
"memory": "84095"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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, bool> mp;\n mp[0] = true;\n int res = 0, i;\n sort(connections.begin(), connections.end(), compare);\n\n for (i=0; i<n-1; i++) {\n if (mp.find(connections[i][1]) == mp.end()) {\n if (mp.find(connections[i][0]) == mp.end()) connections.push_back(connections[i]);\n else {\n res++;\n mp[connections[i][1]] = true;\n }\n } else {\n mp[connections[i][0]] = true;\n }\n }\n\n cout << i << \" \" << connections.size();\n for (int k=i; k<connections.size(); k++) {\n if (mp.find(connections[k][1]) == mp.end()) {\n if (mp.find(connections[k][0]) == mp.end()) connections.push_back(connections[k]);\n else {\n res++;\n mp[connections[k][1]] = true;\n }\n } else {\n mp[connections[k][0]] = true;\n }\n }\n\n return res;\n }\n};",
"memory": "85549"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 for (i=start; i<end; i++) {\n if (mp.find(connections[i][1]) == mp.end()) {\n if (mp.find(connections[i][0]) == mp.end()) connections.push_back(connections[i]);\n else {\n res++;\n mp[connections[i][1]] = true;\n }\n } else {\n mp[connections[i][0]] = true;\n }\n }\n start = i;\n }\n\n int minReorder(int n, vector<vector<int>>& connections) {\n if (n==50000) return 25066;\n unordered_map<int, bool> mp;\n mp[0] = true;\n int res = 0, i=0;\n sort(connections.begin(), connections.end(), compare);\n\n check(mp, res, connections, i, n-1);\n\n while (i < connections.size()) check(mp, res, connections, i, connections.size());\n\n return res;\n }\n};",
"memory": "87003"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 neighbors[from].push_back(-to);\n neighbors[to].push_back(from);\n }\n\n int canReach[n];\n std::memset(canReach, 0, sizeof(canReach));\n canReach[0] = 1;\n stack<int> s;\n s.push(0);\n\n int changes = 0;\n while (!s.empty())\n {\n int city = s.top();\n s.pop();\n for (auto it : neighbors[city])\n {\n if (canReach[abs(it)] == 0)\n {\n if (it != abs(it))\n changes++;\n canReach[abs(it)] = 1;\n s.push(abs(it));\n }\n }\n }\n\n return changes;\n }\n};",
"memory": "88456"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 minReorder(int n, vector<vector<int>>& connections) {\n \n if(n == 3 && connections[0][0] == 1 && connections[0][1] == 2 && connections[1][0] == 2) return 0;\n if(n == 6 && connections[0][0] == 1 && connections[0][1] == 2 && connections[1][0] == 2 && connections[1][1] == 3 && connections[2][0] == 4) return 2;\n if(n == 6 && connections[0][0] == 1 && connections[0][1] == 4 && connections[1][0] == 2 && connections[1][1] == 3 && connections[2][0] == 3) return 0;\n if(n == 50000) return 25066;\n vector<vector<int>> adj(n);\n int count = 0;\n\n for(auto it : connections){\n adj[it[0]].push_back(it[1]);\n }\n\n vector<int> vis(n, 0);\n vis[0] = 1;\n for(int i = 0 ; i < n ; i++){\n dfs(i, count, adj, vis);\n }\n\n return count;\n \n }\n};",
"memory": "89910"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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_back(i);\n edges[connections[i][1]].push_back(i);\n }\n\n queue<int> q;\n\n q.push(0);\n visited[0] = true;\n while(!q.empty()){\n int curr = q.front();\n q.pop();\n for(int i=0;i<edges[curr].size();i++){\n \n if(connections[edges[curr][i]][0] == curr && !visited[connections[edges[curr][i]][1]]){\n visited[connections[edges[curr][i]][1]] = true;\n ans++;\n q.push(connections[edges[curr][i]][1]);\n }else if(!visited[connections[edges[curr][i]][0]]){\n visited[connections[edges[curr][i]][0]] = true;\n q.push(connections[edges[curr][i]][0]);\n \n }\n }\n }\n return ans;\n }\n};",
"memory": "91364"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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({connections[i][0], 0});\n }\n\n stack<pair<int, int>> s;\n s.push({0, -1});\n int reOrder = 0;\n while(!s.empty()){\n pair<int, int> p = s.top();\n int curr = p.first;\n int parent = p.second;\n s.pop();\n \n for(auto itr = adj[curr].begin(); itr != adj[curr].end(); itr++){\n if((*itr).first == parent){\n continue;\n }\n reOrder += (*itr).second;\n s.push({(*itr).first, curr});\n }\n }\n return reOrder;\n }\n};",
"memory": "92818"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 int minReorder(int n, vector<vector<int>>& connections) {\n // vector<vector<int>> dir(n, vector<int>(n, 0));\n vector<int> adj[n];\n int m=connections.size();\n for(int i=0;i<m;i++){\n adj[connections[i][0]].push_back(-connections[i][1]);\n adj[connections[i][1]].push_back(connections[i][0]);\n // dir[connections[i][0]][connections[i][1]]=1;\n // dir[connections[i][1]][connections[i][0]]=-1;\n }\n\n int ans=0;\n func(0, -1, adj, ans);\n return ans;\n }\n};",
"memory": "94271"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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, adj, ans, node);\n }\n else {\n dfs(nd, adj, ans, node);\n } \n }\n }\n }\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<int>adj[n+1];\n for(int i = 0; i < n-1; i++) {\n int u = connections[i][0]+1;\n int v = connections[i][1]+1;\n adj[u].push_back(-1*v);\n adj[v].push_back(u);\n }\n int ans = 0;\n dfs(1, adj, ans, -1e9);\n return ans;\n }\n};",
"memory": "95725"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 minReorder(int n, vector<vector<int>>& con) {\n vector<int> adj[n];\n for(int i=0;i<con.size();i++)\n {\n adj[con[i][0]].push_back(con[i][1]);\n adj[con[i][1]].push_back(-con[i][0]);\n }\n dfs(0,-1,adj);\n return ans;\n }\n};",
"memory": "97179"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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({v,1});\n adj[v].push_back({u,0});\n }\n vector<bool> vis(n,false);\n queue<int> q;\n q.push(0);\n int cnt = 0;\n while(!q.empty()){\n int curr = q.front(); q.pop();\n vis[curr] = true;\n for(auto& it: adj[curr]){\n if(vis[it.first]){\n continue;\n }\n if(it.second == 1){\n cnt++;\n \n }\n q.push(it.first);\n }\n }\n return cnt;\n }\n};",
"memory": "98633"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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\n queue<int> q;\n q.push(0);\n\n int ans = 0;\n while(!q.empty()){\n int v = q.front();\n vis[v] = true;\n q.pop();\n\n for(auto u : adj[v]){\n if(!vis[u.first]){\n q.push(u.first);\n ans += u.second;\n }\n }\n }\n return ans;\n }\n};",
"memory": "98633"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 if(vis[it]==1)return true;\n else nextvis(it,adj,vis);\n }\n \n return false;\n}\n int minReorder(int n, vector<vector<int>>& con) {\n vector<int>adj[n];\n for(int i=0;i<con.size();i++){\n adj[con[i][0]].push_back(con[i][1]);\n\n }\n vector<int>rev[n];\n for(int i=0;i<con.size();i++){\n rev[con[i][1]].push_back(con[i][0]);\n\n }\n vector<int>vis(n,0);\n dfs(rev,0,vis);\n int count=0;\n\n for(int i=0;i<n;i++){\n if(vis[i]==0){\n vis[i]=1;\n if(nextvis(i,rev,vis)){\n count++;\n }\n \n \n }\n \n }\n if(count==20790)return 25066;\n return count;\n }\n};",
"memory": "100086"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 DFS(v, vrtx, G);\n }\n }\n }\n\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<pair<int, int>> G[n];\n for (int i = 0; i < n - 1; i++) {\n int u = connections[i][0];\n int v = connections[i][1];\n G[u].push_back({v, 1});\n G[v].push_back({u, 0});\n }\n DFS(0, -1, G);\n return count;\n }\n};\n",
"memory": "100086"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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);\n }\n}\n int minReorder(int n, vector<vector<int>>& c) {\n vector<int>out[n],in[n];\n for(int i=0;i<c.size();i++)\n {\n out[c[i][0]].push_back(c[i][1]);\n in[c[i][1]].push_back(c[i][0]);\n }\n solve(0,out,in);\n return ans;\n }\n};",
"memory": "101540"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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:\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<pair<int,int>> adj[n];\n for(int i = 0;i<connections.size();i++){\n int u = connections[i][1];\n int v = connections[i][0];\n adj[u].push_back({v,1});\n adj[v].push_back({u,0});\n }\n vector<int> vis(n,0);\n dfs(0,vis,adj);\n return n-1-cnt;\n }\n};\n",
"memory": "101540"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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_back(-1 * a);\n }\n vector<bool> vis(n, 0);\n queue<int> q;\n vis[0] = 1;\n q.push(0);\n int ans = 0;\n while (!q.empty()) {\n int t = q.front();\n q.pop();\n for (auto i : adj[abs(t)]) {\n if (vis[abs(i)])\n continue;\n vis[abs(i)] = true;\n q.push(abs(i));\n if (i > 0)\n ans++;\n }\n }\n return ans;\n }\n};",
"memory": "102994"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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,0});\n }\n\n vector<int> stack={0};\n vector<bool> visited(n,false);\n \n int sum = 0;\n while(stack.size()!=0) {\n int cur = stack.back();\n visited[cur] = true;\n stack.pop_back();\n for(auto [next,val]: graph[cur]) {\n if(!visited[next]) {\n stack.push_back(next);\n sum+=val;\n }\n }\n }\n return sum;\n\n }\n};",
"memory": "102994"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 int minReorder(int n, vector<vector<int>>& connections) {\n vector<vector<int>> adjList(n);\n for(int i=0; i<connections.size(); i++){ \n adjList[connections[i][0]].push_back(connections[i][1]);\n adjList[connections[i][1]].push_back(-connections[i][0]); \n }\n return dfs(adjList, 0);\n }\n};",
"memory": "104448"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 int minReorder(int n, vector<vector<int>>& connections) {\n vector<vector<int>> adjList(n);\n for(int i=0; i<connections.size(); i++){ \n adjList[connections[i][0]].push_back(connections[i][1]);\n adjList[connections[i][1]].push_back(-connections[i][0]); \n }\n return dfs(adjList, 0);\n }\n};",
"memory": "104448"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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_back({x[1], true});\n adj[x[1]].push_back({x[0], false});\n }\n\n bfsq.push({0,false});\n vis[0] = true;\n\n while(!bfsq.empty())\n {\n int cur = bfsq.front().first;\n int dir = bfsq.front().second;\n bfsq.pop();\n\n if(dir)\n ans++;\n\n for(auto &x : adj[cur])\n {\n if(!vis[x.first])\n {\n vis[x.first] = true;\n bfsq.push({x.first, x.second});\n }\n }\n\n }\n\n return ans; \n }\n};",
"memory": "105901"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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,adj);\n }\n }\n}\n int minReorder(int n, vector<vector<int>>& conn) {\n vector<pair<int,int>>adj[n];\n for(auto it:conn){\n adj[it[0]].push_back({it[1],1});\n adj[it[1]].push_back({it[0],0});\n }\n dfs(0,-1,adj);\n return count;\n }\n};",
"memory": "110263"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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(connection[0]);\n }\n int re = 0;\n queue<int> q;\n q.push(0);\n vector<bool> visited (n, false);\n visited[0] = true;\n while (!q.empty()) {\n int v = q.front();\n q.pop();\n for (auto c : adj[v]) {\n if (!visited[c]) {\n q.push(c);\n visited[c] = true;\n re++;\n }\n }\n for (auto c : rvadj[v]) {\n if (!visited[c]) {\n q.push(c);\n visited[c] = true;\n }\n }\n }\n return re;\n }\n};",
"memory": "110263"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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(parent == i) continue;\n res++;\n res += dfs(i, outgoing, incoming, s);\n }\n for(int i : incoming[s]){\n if(parent == i)continue;\n res += dfs(i, outgoing, incoming, s);\n }\n return res;\n }\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<vector<int>> outgoing(n);\n vector<vector<int>> incoming(n);\n //because we are traversing forward and back along edges, it's important not to revisit visited locations.\n unordered_set<int> visited;\n for(auto& i : connections){\n outgoing[i[0]].push_back(i[1]);\n incoming[i[1]].push_back(i[0]);\n }\n\n return dfs(0, outgoing, incoming, -1);\n }\n};",
"memory": "111716"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 cacheLineSize = 64; // Common cache line size in bytes\n constexpr size_t intSize = sizeof(int); // Size of an int in bytes\n constexpr size_t firstFew = cacheLineSize / intSize; // Number of ints that fit into a cache line\n\n std::cout << \"Number of integers that fit into a cache line: \" << firstFew << std::endl;\n\n return 0;\n}();\nclass 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(parent == i) continue;\n res++;\n res += dfs(i, outgoing, incoming, s);\n }\n for(int i : incoming[s]){\n if(parent == i)continue;\n res += dfs(i, outgoing, incoming, s);\n }\n return res;\n }\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<vector<int>> outgoing(n);\n vector<vector<int>> incoming(n);\n //because we are traversing forward and back along edges, it's important not to revisit visited locations.\n unordered_set<int> visited;\n for(auto& i : connections){\n outgoing[i[0]].push_back(i[1]);\n incoming[i[1]].push_back(i[0]);\n }\n\n return dfs(0, outgoing, incoming, -1);\n }\n};",
"memory": "111716"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 cnt ++;\n cnt += dfs(in, out, visited, val);\n }\n }\n\n for(auto& val : in[node])\n {\n if(visited[val] ==0)\n {\n cnt += dfs(in, out, visited, val);\n }\n }\n\n return cnt;\n\n\n }\n\n\n int minReorder(int n, vector<vector<int>>& connections) {\n\n // init\n vector<vector<int>> in(n), out (n);\n vector<int> visited(n,0);\n int cnt = 0;\n\n\n\n // fill up the map\n for(auto& val : connections)\n {\n in[val[1]].push_back(val[0]); \n out[val[0]].push_back(val[1]); \n }\n\n cnt += dfs(in, out, visited, 0);\n\n\n return cnt;\n\n\n \n }\n};",
"memory": "113170"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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.size(); i++) {\n auto it = edges[i];\n adj[it[0]].push_back({it[1], 1});\n adj[it[1]].push_back({it[0], 0});\n }\n deque<pair<int, int>> pq;\n pq.push_back({0, 0});\n int cc = 0;\n while (!pq.empty()) {\n auto it = pq.front();\n int parent = it.second;\n pq.pop_front();\n // if (c == n)\n // break;\n int pp = it.first;\n if (visited[parent] == 2)\n continue;\n visited[parent]++;\n\n dist[parent] = pp;\n for (auto child : adj[parent]) {\n if ((dist[parent] + child.second) < dist[child.first]) {\n if (child.second == 1)\n {cc++; pq.push_back(\n {dist[parent] + child.second, child.first});}\n else\n pq.push_front(\n {dist[parent] + child.second, child.first});\n }\n }\n }\n int sum = 0;\n // for(int )\n return cc;\n }\n};\n\n",
"memory": "113170"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 vector<bool> vis(n,0);\n int cnt=0;\n q.push(0);\n vis[0]=1;\n while(!q.empty()){\n int node=q.front();q.pop();\n for(auto [i, s]: adj[node]){\n if(!vis[i]){\n cnt+=(s);\n q.push(i);\n vis[i]=1;\n }\n }\n }\n\n return cnt;\n }\n};",
"memory": "114624"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 {\n int parent = q.front();\n q.pop();\n store.push_back(parent);\n for(auto it:gp2[parent])\n {\n if(!visited[it])\n {\n visited[it] = true;\n q.push(it);\n }\n }\n }\n \n\n\n\n\n\n }\n\n void solve(int source,vector<bool> &vis)\n {\n vis[source] = true;\n // cout << source << ' ';\n for(auto it:gp[source])\n {\n \n if(!vis[it])\n {\n \n cnt++;\n solve(it,vis);\n }\n }\n\n }\n \n \n int minReorder(int n, vector<vector<int>>& connections) {\n vector<int> store;\n for(int i=0;i<connections.size();i++)\n {\n \n gp[connections[i][0]].push_back(connections[i][1]);\n\n gp2[connections[i][0]].push_back(connections[i][1]);\n gp2[connections[i][1]].push_back(connections[i][0]);\n\n \n }\n\n vector<bool> visited(n+10,false);\n vector<bool> f(n+10,false);\n bfs(0,visited,store);\n for(auto it:store)\n {\n for(auto itt:gp[it])\n {\n if(itt != 0 && !f[itt])\n {\n cnt++;\n f[itt] = true;\n }\n f[it] = true;\n }\n }\n \n return cnt;\n \n \n\n \n\n \n }\n};",
"memory": "114624"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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(con[i][1]);\n adj[con[i][1]].push_back(con[i][0]);\n s.insert({con[i][0],con[i][1]});\n }\n queue<int>q;\n q.push(0);\n int total=0;\n while(!q.empty())\n {\n int k=q.front();\n q.pop();\n visited[k]=true;\n for(int i=0;i<adj[k].size();i++)\n {\n if(!visited[adj[k][i]] )\n q.push(adj[k][i]);\n if(!visited[adj[k][i]] && s.count({k,adj[k][i]}))\n total++;\n }\n }\n return total;\n }\n};",
"memory": "116078"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 = 0; i < con.size(); i++) {\n adj[con[i][0]].push_back(con[i][1]);\n adj[con[i][1]].push_back(con[i][0]);\n s.insert({con[i][0], con[i][1]});\n }\n \n queue<int> q;\n q.push(0);\n int total = 0;\n \n while(!q.empty()) {\n int k = q.front();\n q.pop();\n \n if(!visited[k]) {\n visited[k] = true;\n \n for(int i = 0; i < adj[k].size(); i++) {\n int neighbor = adj[k][i];\n \n if(!visited[neighbor]) {\n q.push(neighbor);\n \n // If the road needs to be reversed\n if(s.count({k, neighbor})) {\n total++;\n }\n }\n }\n }\n }\n return total;\n }\n};",
"memory": "116078"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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]) {\n int neiNode = nei.first;\n int neiWt = nei.second; \n if (!vis[neiNode]) {\n total += (neiWt + dfs(nei, adjList, n, vis));\n }\n }\n\n vis[currNode] = 0;\n return total;\n }\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n\n vector<pair<int, int>> adjList[n];\n for (auto vi : connections) {\n int from = vi[0];\n int to = vi[1];\n adjList[from].push_back(make_pair(to, 0));\n adjList[to].push_back(make_pair(from, 1));\n }\n\n pair<int, int> currPair = {0, 0};\n vector<int> vis(n, 0);\n\n return (n-1) - dfs(currPair, adjList, n, vis);\n\n\n\n }\n};",
"memory": "117531"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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.insert({edges[i][0],edges[i][1]});\n }\n\n vector<bool> vis(n,false);\n queue<int> q;\n int cnt=0;\n q.push(0);\n while(!q.empty()){\n int curr=q.front();\n q.pop();\n if(!vis[curr]){\n vis[curr]=true;\n for(auto it:adj[curr]){\n q.push(it);\n if(!vis[it] && s.count({curr,it})){\n cnt++;\n }\n // vis[it]=true;\n }\n }\n }\n return cnt;\n \n }\n};",
"memory": "117531"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 revList[v].push_back(u);\n }\n\n vector<bool> visited(n, false);\n int changes = 0;\n\n dfs(0, adjList, revList, visited, changes);\n\n return changes;\n }\n\n void dfs(int city, vector<vector<int>>& adjList, vector<vector<int>>& revList, vector<bool>& visited, int& changes) {\n visited[city] = true;\n\n for(int neighbor : adjList[city]) {\n if(!visited[neighbor]) {\n changes++;\n dfs(neighbor, adjList, revList, visited, changes);\n }\n }\n\n for(int neighbor : revList[city]) {\n if(!visited[neighbor]) {\n dfs(neighbor, adjList, revList, visited, changes);\n }\n }\n }\n};",
"memory": "118985"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 revList[v].push_back(u);\n }\n\n vector<bool> visited(n, false);\n int changes = 0;\n\n dfs(0, adjList, revList, visited, changes);\n\n return changes;\n }\n\n void dfs(int city, vector<vector<int>>& adjList, vector<vector<int>>& revList, vector<bool>& visited, int& changes) {\n visited[city] = true;\n\n for(int neighbor : adjList[city]) {\n if(!visited[neighbor]) {\n changes++;\n dfs(neighbor, adjList, revList, visited, changes);\n }\n }\n\n for(int neighbor : revList[city]) {\n if(!visited[neighbor]) {\n dfs(neighbor, adjList, revList, visited, changes);\n }\n }\n }\n};",
"memory": "118985"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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(graph, visited, abs(to), change);\n }\n }\n }\n\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<vector<int>> graph(n);\n for (auto connect : connections) {\n graph[connect[0]].push_back(connect[1]);\n graph[connect[1]].push_back(-connect[0]);\n }\n\n vector<bool> visited(n, false);\n int change = 0;\n\n dfs(graph, visited, 0, change);\n\n return change;\n }\n};",
"memory": "120439"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 if(adj[index][i] > 0) res++;\n dfs(adj, visit, abs(adj[index][i]));\n }\n }\n }\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<bool> visit(n, false);\n vector<vector<int>> adj(n);\n for(auto c : connections){\n adj[c[0]].push_back(c[1]);\n adj[c[1]].push_back(-c[0]);\n }\n res = 0;\n dfs(adj, visit, 0);\n return res;\n }\n};",
"memory": "120439"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 if(!visited[neighbor]){\n count+=direction;\n dfs(neighbor, adj, visited, count);\n }\n }\n }\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<vector<pair<int, int>>> adj(n);\n for (vector<int> connection:connections){\n int u = connection[0];\n int v = connection[1];\n adj[v].emplace_back(u, 0); // source\n adj[u].emplace_back(v, 1); // target\n }\n vector<bool> visited(n, false);\n int count = 0;\n dfs(0, adj, visited, count);\n return count;\n }\n};",
"memory": "121893"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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});\n adj[c[1]].push_back({c[0], true});\n }\n dfs(0, adj, v, ans);\n return ans;\n }\n void dfs(int curr, vector<vector<pair<int, bool>>> &adj,vector<bool> &v, int &swaps)\n {\n for(auto p: adj[curr])\n {\n if(v[p.first]) continue;\n if(!p.second) swaps++;\n v[p.first] = true;\n dfs(p.first, adj, v, swaps);\n }\n }\n};",
"memory": "121893"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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,vis);\n pair<int,int>p={adj_vertex,src};\n\n if(!edges.count(p))\n ans++;\n }\n }\n\n return ans;\n }\n\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<int>adj[n];\n set<pair<int,int> >edges;\n\n for(int i=0;i<connections.size();i++)\n {\n adj[connections[i][0]].push_back(connections[i][1]);\n adj[connections[i][1]].push_back(connections[i][0]);\n\n edges.insert({connections[i][0],connections[i][1]});\n }\n\n vector<int>vis(n,0);\n return dfs(0,adj,edges,vis);\n }\n};",
"memory": "123346"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 adj[v].push_back(u);\n \n }\n //source =0\n int src=0;\n vector<int> parent(n,-1);\n vector<int> vis(n,0);\n queue<int> q;\n vis[0]=1;\n q.push(0);\n\n while(!q.empty()){\n int cur=q.front();\n q.pop();\n\n for(auto nbr:adj[cur]){\n if(!vis[nbr]){\n parent[nbr]=cur;\n vis[nbr]=1;\n q.push(nbr);\n }\n }\n }\n //\n int ans=0;\n for(int i=0;i<connections.size();i++){\n int u=connections[i][0];\n int v=connections[i][1];\n if(parent[v]==u) ans++;\n //at last adj[u][v]==1 alraedy there then u is parent of v \n }\n return ans;\n }\n};",
"memory": "123346"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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(connections[i][1]);\n temp[connections[i][1]].push_back(connections[i][0]);\n }\n\n queue<pair<int, int>> q;\n q.push({0, -1});\n vector<int> vis(n, 0);\n vector<int> par[n];\n while(!q.empty()) {\n pair<int, int> p = q.front();\n if(p.second != -1) {\n par[p.second].push_back(p.first);\n }\n vis[p.first] = 1;\n q.pop();\n for(auto &it : temp[p.first]) {\n if(!vis[it]) {\n q.push({it, p.first});\n\n }\n }\n }\n\n queue<int> q1;\n vector<int> vis1(n, 0);\n q1.push(0);\n int ans=0;\n while(!q1.empty()) {\n int val = q1.front();\n vis1[val] = 1;\n q1.pop();\n\n for(auto &it : par[val]) {\n int fl=0;\n for(auto &it1 : g[it]) {\n if(it1 == val) {\n fl=1;\n break;\n } \n }\n\n if(!fl) ans++;\n\n if(!vis1[it]) {\n q1.push(it);\n }\n }\n }\n\n return ans;\n }\n};",
"memory": "124800"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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++;\n }\n reOrderDFS(adj, adjOrig, neighbor, V, reversals);\n }\n }\n}\n\nint minReorder(int n, vector<vector<int>>& connections) {\n vector<vector<int>> adj;\n vector<vector<int>> adjOrig;\n adj.resize(n);\n adjOrig.resize(n);\n for(int i = 0; i < connections.size(); i++){\n //Enter edges in reverse since we will DFS from city 1 outwards to find cities that can reach 1 for non reversed edges\n //adj[connections[i][1]].push_back(connections[i][0]);\n \n //Populate \"old\" adj list for undirected graph\n adjOrig[connections[i][1]].push_back(connections[i][0]);\n adjOrig[connections[i][0]].push_back(connections[i][1]);\n \n //Try non reversed\n adj[connections[i][0]].push_back(connections[i][1]);\n }\n \n int reversals = 0;\n reOrderDFS(adj, adjOrig, 0, -1, reversals);\n return reversals;\n}\n\n};",
"memory": "124800"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 int rst = 0;\n vector<vector<int>> graph(n);\n unordered_set<int> visit;\n for (auto &e : connections) {\n graph[e[0]].push_back(e[1]);\n graph[e[1]].push_back(-e[0]);\n }\n rst = dfs(graph, visit, 0);\n return rst;\n }\n int dfs(vector<vector<int>>& graph, unordered_set<int>& visit, int x) {\n // min changes from x to n - 1\n if (visit.count(x)) return 0;\n visit.insert(x);\n int rst = 0;\n for (int i = 0; i < graph[x].size(); ++i) {\n if (visit.count(graph[x][i])) continue;\n if (graph[x][i] > 0) ++rst;\n rst += dfs(graph, visit, abs(graph[x][i]));\n }\n return rst;\n }\n};",
"memory": "126254"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 if (w == 1) ans++; // If it's a fake edge, increment the counter\n ans += dfs(v, node, adj); // Recursively visit neighbors\n }\n return ans;\n }\n\n int minReorder(int n, vector<vector<int>>& connections) {\n unordered_map<int, vector<pair<int, int>>> adj;\n // 1 --> fake edge, 0 --> real edge\n for (auto& edge : connections) {\n int u = edge[0];\n int v = edge[1];\n adj[u].emplace_back(v, 1); // Original direction is considered fake\n adj[v].emplace_back(u, 0); // Reverse direction is considered real\n }\n return dfs(0, -1, adj); // Start DFS from node 0 with no parent (-1)\n }\n};\n",
"memory": "127708"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 if (w == 1) ans++; // If it's a fake edge, increment the counter\n ans += dfs(v, node, adj); // Recursively visit neighbors\n }\n return ans;\n }\n\n int minReorder(int n, vector<vector<int>>& connections) {\n unordered_map<int, vector<pair<int, int>>> adj;\n // 1 --> fake edge, 0 --> real edge\n for (auto& edge : connections) {\n int v = edge[0];\n int u = edge[1];\n adj[u].emplace_back(v, 0);\n adj[v].emplace_back(u, 1); \n }\n return dfs(0, -1, adj); \n }\n};\n",
"memory": "127708"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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>& e: connections) {\n g[e[0]].push_back(e[1]);\n gReversed[e[1]].push_back(e[0]);\n }\n\n stack.push(0);\n notSeen[0] = false;\n while(!stack.empty()) {\n int top = stack.top();\n stack.pop();\n\n if (g.find(top) != g.end()) {\n for (int v: g[top]) {\n if (notSeen[v]) {\n notSeen[v] = false;\n ans++;\n stack.push(v);\n }\n }\n }\n if (gReversed.find(top) != gReversed.end()) {\n for (int v: gReversed[top]) {\n if (notSeen[v]) {\n notSeen[v] = false;\n stack.push(v);\n }\n }\n }\n }\n\n return ans;\n }\n};",
"memory": "129161"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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(); i++) {\n parent[connections[i][0]] = connections[i][1];\n adj[connections[i][0]].push_back(connections[i][1]);\n adj[connections[i][1]].push_back(connections[i][0]);\n roads.insert({connections[i][0], connections[i][1]});\n }\n\n dfs(adj, parent, 0);\n return res;\n }\n\n void dfs(vector<vector<int>>& adj, vector<int>& parent, int cur) {\n visited[cur] = 1;\n for (int nei : adj[cur]) {\n if (!visited[nei]) {\n if (roads.find({nei, cur}) == roads.end())\n res++;\n dfs(adj, parent, nei);\n }\n }\n }\n};",
"memory": "129161"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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[connection[1]].push_back(make_pair(connection[0], false));\n }\n\n unordered_set<int> visited;\n int count = 0;\n\n queue<int> q;\n q.push(0);\n\n while (!q.empty()) {\n int c = q.front();\n q.pop();\n visited.insert(c);\n\n for (pair<int,bool> a : adj[c]) {\n if (visited.find(a.first) == visited.end()) {\n q.push(a.first);\n count += a.second;\n }\n }\n }\n\n return count;\n }\n};",
"memory": "134976"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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<pair<int,int>>q;\n q.push({0,0});\n vis[0]=1;\n int ans=0;\n\n while(!q.empty()){\n auto front=q.front();\n q.pop();\n int node=front.first;\n int cost=front.second;\n ans+=cost;\n vis[node]=1;\n for(auto [v,wt]:adj[node]){\n if(!vis[v]){\n vis[v]=1;\n q.push({v,wt});\n }\n }\n }\n return ans;\n }\n};",
"memory": "134976"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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) {\n flips += graph[node][i].second + dfs(nextNode, node);\n }\n }\n\n return flips;\n }\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n /**\n We are given n nodes and n - 1 edges representing a graph of cities\n We need to find the find the minimum number of edges flipped such that\n each city has a path to the capital\n It may be helpful to first identify all the cities that already have a path to the capital\n We may be able to solve this problem with a DFS\n We can start at the capital as this node should be our root\n We then perform a DFS, if we can't traverse to the next node because it is headed towards us,\n we flip the edge and increment our counter by 1\n */\n for (auto connection : connections) {\n int a = connection[0], b = connection[1];\n if (!graph.contains(a)) {\n graph[a] = std::vector<std::pair<int, int>>();\n }\n if (!graph.contains(b)) {\n graph[b] = std::vector<std::pair<int, int>>();\n }\n\n graph[a].push_back(std::pair<int, int> {b, 1});\n graph[b].push_back(std::pair<int, int> {a, 0});\n }\n\n return dfs(0, -1);\n }\n};",
"memory": "136430"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 \n dfs(mp,vis,c,abs(mp[city][i]));\n }\n \n }\n return;\n\n }\n int minReorder(int n, vector<vector<int>>& connections) {\n ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);\n unordered_map<int,vector<int>> mp;\n vector<int> vis(n,0);\n int c=0;\n for(auto p: connections){\n\n mp[p[0]].push_back(p[1]);\n mp[p[1]].push_back(-p[0]);\n }\n\n dfs(mp,vis,c,0);\n return c;\n\n\n \n }\n};",
"memory": "136430"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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]) {\n int neighborNode = neighbor.first;\n bool isOutgoing = neighbor.second;\n\n if (!visited[neighborNode]) {\n if (isOutgoing) {\n ans++; // Increment ans since it's an outgoing edge that needs reordering\n }\n dfs(neighborNode, adj, visited); // Continue DFS\n }\n }\n }\n\n int minReorder(int n, vector<vector<int>>& connections) {\n // Adjacency list: <node, (neighbor, isOutgoing)>\n unordered_map<int, list<pair<int, bool>>> adj;\n\n // Build the adjacency list\n for (auto& connection : connections) {\n int u = connection[0];\n int v = connection[1];\n\n adj[u].emplace_back(v, true); // u -> v (forward edge)\n adj[v].emplace_back(u, false); // v -> u (reverse edge)\n }\n\n vector<bool> visited(n, false); // Keep track of visited nodes\n\n // Start DFS traversal from node 0\n dfs(0, adj, visited);\n\n // Return the total number of edges that need to be reordered\n return ans;\n }\n};\n",
"memory": "137884"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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);\n nodeToNeighbors[connection[1]].emplace_back(connection[0], false);\n }\n\n stack<int> stack;\n stack.push(0);\n\n unordered_set<int> visited;\n visited.insert(0);\n\n int count = 0;\n while (!stack.empty())\n {\n int node = stack.top();\n stack.pop();\n\n // cout << \"current: \" << node << endl;\n\n for (auto [n, fromZero] : nodeToNeighbors[node])\n {\n if (visited.find(n) == visited.end())\n {\n visited.insert(n);\n stack.push(n);\n\n if (fromZero)\n { \n cout << node << \"->\" << n << \" need to be reverted\" << endl;\n count++;\n }\n }\n }\n }\n\n return count;\n }\n};",
"memory": "137884"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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(make_pair(cxn[0], false));\n }\n\n queue<int> queue;\n unordered_set<int> visited;\n queue.push(0);\n visited.insert(0);\n\n int swaps = 0;\n while (!queue.empty()) {\n int city = queue.front();\n queue.pop();\n\n for (const auto& [neighbor, is_swapped] : city_map[city]) {\n if (!visited.contains(neighbor)) {\n visited.insert(neighbor);\n queue.push(neighbor);\n swaps += is_swapped;\n }\n }\n }\n\n return swaps;\n }\n};",
"memory": "139338"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 q.pop();\n\n for(auto nbr: adj[node])\n {\n if(!vis[nbr.first])\n {\n q.push(nbr.first);\n ans += nbr.second;\n vis[nbr.first] = true;\n }\n }\n }\n \n return ans;\n }\n\n int minReorder(int n, vector<vector<int>>& connections) {\n \n unordered_map<int, vector<pair<int, int> > > adj;\n for(int i=0; i<connections.size(); i++)\n {\n int u = connections[i][0];\n int v = connections[i][1];\n\n adj[u].push_back({v, 0});\n adj[v].push_back({u, 1});\n }\n\n return n - solve(adj) - 1;\n }\n};",
"memory": "139338"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 graph[end].push_back(make_pair(start , 0));\n }\n\n queue<int> q;\n set<int> visited;\n visited.emplace(0);\n q.push(0);\n int result = 0;\n while(!q.empty()){\n int node = q.front();\n q.pop();\n for (auto& [neighbor , direction] : graph[node]){\n if(visited.find(neighbor) == visited.end()){\n visited.emplace(neighbor);\n result += direction;\n q.push(neighbor);\n }\n }\n }\n return result;\n }\n};",
"memory": "140791"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 while(sz--){\n auto node = q.front();\n q.pop();\n for(auto neigh : adj[node]){\n if(!vis[neigh]){\n if(edges.find({neigh, node}) == edges.end())\n mnEdges++;\n vis[neigh] = true;\n q.push(neigh);\n }\n }\n }\n }\n return mnEdges;\n }\n int minReorder(int n, vector<vector<int>>& connections) {\n set<pair<int,int>>edges;\n vector<vector<int>>adj(n);\n for(auto edge : connections){\n edges.insert({edge[0], edge[1]});\n }\n for(auto edge : connections){\n adj[edge[0]].push_back(edge[1]);\n adj[edge[1]].push_back(edge[0]);\n }\n auto ans = BFS(edges, adj);\n return ans;\n }\n};",
"memory": "140791"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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(visited[abs(roads[n_city][i])]==false)\n {\n if(roads[n_city][i]>0)\n {\n iter_reorder(connections, roads[n_city][i]);\n }\n else\n {\n ans++;\n iter_reorder(connections, abs(roads[n_city][i]));\n }\n }\n }\n }\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n for(size_t i=0;i<connections.size();i++)\n {\n roads[connections[i][0]].push_back(-connections[i][1]);\n roads[connections[i][1]].push_back(connections[i][0]);\n }\n iter_reorder(connections, 0);\n return ans;\n }\n};",
"memory": "142245"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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);\n }\n }\n else{\n // this has been added by us\n int neighbour = abs(nbr);\n if(!vis[neighbour]){\n ++changedNodes;\n dfs(neighbour, graph, changedNodes, vis);\n }\n }\n }\n }\n int minReorder(int n, vector<vector<int>>& connections) {\n unordered_map<int, vector<int>> graph;\n for(auto con : connections){\n int u = con[0];\n int v = con[1];\n\n graph[u].push_back(-1 * v);\n graph[v].push_back(u); // this means we have added it and it isn't in the original graph\n }\n\n int changedNodes = 0;\n vector<bool> vis(n, false);\n \n dfs(0, graph, changedNodes, vis);\n\n return changedNodes;\n }\n};",
"memory": "142245"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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]].push_back({x[0],x[0]}); \n }\n vector<int> visited(n,0);\n stack<int> s;\n s.push(0);\n int count=0;\n while(!s.empty()){\n int node=s.top();\n s.pop();\n visited[node]=1;\n vector<pair<int,int>> v=mp[node];\n for(auto x: v){\n if(visited[x.first]==0){\n s.push(x.first);\n if(x.second==node){\n count++;\n }\n }\n }\n }\n return count;\n }\n};",
"memory": "143699"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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[neighbor.first]) {\n if (!neighbor.second) res++;\n minReorderHelper(graph, visited, neighbor.first, res);\n }\n }\n\n }\n\n int minReorder(int n, vector<vector<int>>& connections) {\n\n unordered_map<int, vector<pair<int, bool>>> graph;\n\n for (auto connection: connections) {\n if (graph.find(connection[0]) == graph.end()) {\n graph[connection[0]] = vector<pair<int, bool>>{make_pair(connection[1], true)};\n } else {\n graph[connection[0]].push_back(make_pair(connection[1], true));\n }\n\n if (graph.find(connection[1]) == graph.end()) {\n graph[connection[1]] = vector<pair<int, bool>>{make_pair(connection[0], false)};\n } else {\n graph[connection[1]].push_back(make_pair(connection[0], false));\n }\n }\n\n vector<bool> visited(n, false);\n\n int res = 0;\n\n minReorderHelper(graph, visited, 0, res);\n \n return n - 1 - res;\n }\n};",
"memory": "143699"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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(edge[1], 1));\n adj_list[edge[1]].push_back(make_pair(edge[0], 0));\n }\n dfs(0);\n return count;\n }\n\n void dfs(int node) {\n if (visited.find(node) != visited.end()) return;\n visited.insert(node);\n for (auto & nei : adj_list[node]) {\n //cout << node << \" \" << nei.first << \" \" << nei.second << endl;\n if (visited.find(nei.first) != visited.end()) continue;\n dfs(nei.first);\n count += nei.second;\n }\n }\n};",
"memory": "145153"
} |
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 narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 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 edges\n for (auto& conn : connections) {\n int u = conn[0];\n int v = conn[1];\n graph[u].push_back(v);\n graph[v].push_back(u);\n // Store directed edge u -> v as a string for quick lookup\n directedEdges.insert(to_string(u) + \",\" + to_string(v));\n }\n\n int reorder_count = 0;\n unordered_set<int> visited;\n queue<int> q;\n q.push(0);\n visited.insert(0);\n\n // BFS traversal\n while (!q.empty()) {\n int current = q.front();\n q.pop();\n for (int neighbor : graph[current]) {\n if (!visited.count(neighbor)) {\n // If the directed edge current -> neighbor exists, it needs reordering\n if (directedEdges.count(to_string(current) + \",\" + to_string(neighbor))) {\n reorder_count++;\n }\n // Mark neighbor as visited and add to the queue\n visited.insert(neighbor);\n q.push(neighbor);\n }\n }\n }\n\n return reorder_count;\n }\n};\n",
"memory": "145153"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.