-
Notifications
You must be signed in to change notification settings - Fork 481
/
1443.java
30 lines (26 loc) · 873 Bytes
/
1443.java
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
class Solution {
public int minTime(int n, int[][] edges, List<Boolean> hasApple) {
for (int[] it : edges) {
int i = it[0], j = it[1];
g.putIfAbsent(i, new ArrayList());
g.putIfAbsent(j, new ArrayList());
g.get(i).add(j);
g.get(j).add(i);
}
boolean[] vis = new boolean[n];
vis[0] = true;
return dfs(0, hasApple, vis);
}
private Map<Integer, List<Integer>> g = new HashMap();
private int dfs(int x, List<Boolean> hasApple, boolean[] vis) {
int res = 0;
for (int i : g.get(x)) {
if (vis[i]) continue;
vis[i] = true;
int cur = dfs(i, hasApple, vis);
if (cur > 0) res += cur + 2;
else if (hasApple.get(i)) res += 2;
}
return res;
}
}