-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1466.cpp
More file actions
31 lines (28 loc) · 863 Bytes
/
Copy path1466.cpp
File metadata and controls
31 lines (28 loc) · 863 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
31
class Solution {
public:
int minReorder(int n, vector<vector<int>> &connections) {
unordered_set<string> us;
vector<bool> visited(n, false);
vector<vector<int>> adj(n, vector<int>());
for (auto &e : connections) {
us.insert(to_string(e[0]) + " " + to_string(e[1]));
adj[e[0]].push_back(e[1]);
adj[e[1]].push_back(e[0]);
}
int res = 0;
stack<int> st;
st.push(0);
visited[0] = true;
while (!st.empty()) {
int root = st.top();
st.pop();
for (auto c : adj[root])
if (!visited[c]) {
if (!us.count(to_string(c) + " " + to_string(root))) res++;
visited[c] = true;
st.push(c);
}
}
return res;
}
};