-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1466.cpp
More file actions
20 lines (20 loc) · 694 Bytes
/
1466.cpp
File metadata and controls
20 lines (20 loc) · 694 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public:
int res = 0;
void dfs(int node, int parent, vector<vector<pair<int, bool>>>& adjacency) {
for (auto& [neighbor, status] : adjacency[node]) {
if (neighbor == parent) continue;
if (status) res++;
dfs(neighbor, node, adjacency);
}
}
int minReorder(int n, vector<vector<int>>& connections) {
vector<vector<pair<int, bool>>> adjacency(n);
for (auto& connection : connections) {
adjacency[connection[0]].push_back({connection[1], true});
adjacency[connection[1]].push_back({connection[0], false});
}
dfs(0, -1, adjacency);
return res;
}
};