leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0684.cpp (771B)
0 class Solution {
1 public:
2 int minReorder(int n, vector<vector<int>> &connections) {
3 vector<vector<int>> adj(n, vector<int>());
4 vector<bool> visited(n, false);
5 stack<int> st;
6 int res = 0;
8 for (auto &e : connections) {
9 adj[e[0]].push_back(e[1]);
10 adj[e[1]].push_back(-e[0]);
11 }
13 st.push(0);
14 visited[0] = true;
15 while (!st.empty()) {
16 int root = st.top();
17 st.pop();
18 for (auto c : adj[root]) {
19 int ac = abs(c);
20 if (!visited[ac]) {
21 res += c > 0;
22 visited[ac] = true;
23 st.push(ac);
24 }
25 }
26 }
27 return res;
28 }
29 };