-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminTime.cpp
More file actions
26 lines (26 loc) · 751 Bytes
/
minTime.cpp
File metadata and controls
26 lines (26 loc) · 751 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
class Solution {
public:
int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {
vector<vector<int>> graph(n);
for (auto &v: edges) {
graph[v[0]].push_back(v[1]);
graph[v[1]].push_back(v[0]);
}
vector<bool> vis(n, false);
vis[0] = true;
std::function<int(int)> dfs = [&](int vertex) {
int time = 0;
for (int e: graph[vertex]) {
if (!vis[e]) {
vis[e] = true;
time += dfs(e);
}
}
if (vertex > 0 && (time > 0 || hasApple[vertex])) {
time += 2;
}
return time;
};
return dfs(0);
}
};