-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0684.cpp
More file actions
30 lines (28 loc) · 771 Bytes
/
Copy path0684.cpp
File metadata and controls
30 lines (28 loc) · 771 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
public:
int minReorder(int n, vector<vector<int>> &connections) {
vector<vector<int>> adj(n, vector<int>());
vector<bool> visited(n, false);
stack<int> st;
int res = 0;
for (auto &e : connections) {
adj[e[0]].push_back(e[1]);
adj[e[1]].push_back(-e[0]);
}
st.push(0);
visited[0] = true;
while (!st.empty()) {
int root = st.top();
st.pop();
for (auto c : adj[root]) {
int ac = abs(c);
if (!visited[ac]) {
res += c > 0;
visited[ac] = true;
st.push(ac);
}
}
}
return res;
}
};