forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution2.java
34 lines (34 loc) · 1.01 KB
/
Solution2.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
class Solution {
int n, k;
int[] values;
int[] dfs(int curr, int parent, List<List<Integer>> adj) {
int[] res = new int[] {0, values[curr] % k};
for (int next : adj.get(curr)) {
if (next == parent) {
continue;
}
int[] update = dfs(next, curr, adj);
res[0] += update[0];
res[1] += update[1];
}
res[1] %= k;
res[0] += res[1] == 0 ? 1 : 0;
return res;
}
public int maxKDivisibleComponents(int n, int[][] edges, int[] values, int k) {
this.n = n;
this.k = k;
this.values = values;
List<List<Integer>> adj = new ArrayList<>();
int[][] dp = new int[n][2];
for (int i = 0; i < n; i++) {
adj.add(new ArrayList<>());
}
for (int[] edge : edges) {
adj.get(edge[0]).add(edge[1]);
adj.get(edge[1]).add(edge[0]);
}
int[] ans = dfs(0, -1, adj);
return ans[1] == 0 ? ans[0] : 0;
}
}