-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1466.cpp
More file actions
37 lines (31 loc) · 936 Bytes
/
1466.cpp
File metadata and controls
37 lines (31 loc) · 936 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
32
33
34
35
36
37
class Solution {
public:
int flips;
void dfs(int n, vector<bool>& visited, vector<vector<int>>& edges, vector<vector<int>>& opp){
visited[n] = true;
for(int next : opp[n]){
if(!visited[next]){
dfs(next, visited, edges, opp);
}
}
for(int next : edges[n]){
if(!visited[next]){
flips++;
dfs(next, visited, edges, opp);
}
}
}
int minReorder(int n, vector<vector<int>>& connections) { //O(n)
flips = 0;
vector<vector<int>> edges(n);
vector<vector<int>> opp(n);
for(vector<int>& v : connections){
int a = v[0]; int b = v[1];
edges[a].push_back(b);
opp[b].push_back(a);
}
vector<bool> visited(n);
dfs(0, visited, edges, opp);
return flips;
}
};