forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
37 lines (35 loc) · 929 Bytes
/
Solution.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
31
32
33
34
35
36
37
class Solution {
private List<Integer>[] g;
private int[] colors;
private int[] size;
private int ans;
public int maximumSubtreeSize(int[][] edges, int[] colors) {
int n = edges.length + 1;
g = new List[n];
size = new int[n];
this.colors = colors;
Arrays.fill(size, 1);
Arrays.setAll(g, i -> new ArrayList<>());
for (var e : edges) {
int a = e[0], b = e[1];
g[a].add(b);
g[b].add(a);
}
dfs(0, -1);
return ans;
}
private boolean dfs(int a, int fa) {
boolean ok = true;
for (int b : g[a]) {
if (b != fa) {
boolean t = dfs(b, a);
ok = ok && colors[a] == colors[b] && t;
size[a] += size[b];
}
}
if (ok) {
ans = Math.max(ans, size[a]);
}
return ok;
}
}