forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
53 lines (39 loc) · 1.09 KB
/
main.cpp
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/// Source : https://leetcode.com/problems/minimum-time-to-collect-all-apples-in-a-tree/
/// Author : liuyubobobo
/// Time : 2020-05-09
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
/// DFS
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
private:
int apple = 0;
public:
int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {
vector<unordered_set<int>> g(n);
for(const vector<int>& edge: edges)
g[edge[0]].insert(edge[1]), g[edge[1]].insert(edge[0]);
return dfs(n, g, 0, -1, hasApple);
}
private:
int dfs(int n, vector<unordered_set<int>>& g, int v, int p, const vector<bool>& hasApple){
apple += hasApple[v];
int res = 0;
int cur = apple;
for(int next: g[v])
if(next != p){
int tres = dfs(n, g, next, v, hasApple);
if(apple != cur){
res += 2 + tres;
cur = apple;
}
}
return res;
}
};
int main() {
return 0;
}