private int[] visit;
private long[] temp;
private ArrayList<Integer>[] adj;
private long answer;
public long solution(int[] a, int[][] edges) {
int sum = 0;
int n = a.length;
visit = new int[n];
adj = new ArrayList[n];
temp = new long[n];
answer = 0;
for (int i = 0; i < n; i++) {
adj[i] = new ArrayList<>();
sum += a[i];
temp[i] = a[i];
}
if (sum != 0) {
return -1;
}
buildGraph(edges);
dfs(0);
return answer;
}
private void buildGraph(int[][] edges) {
for (int i = 0; i < edges.length; i++) {
int u = edges[i][0];
int v = edges[i][1];
adj[u].add(v);
adj[v].add(u);
}
}
private long dfs(int i) {
visit[i] = 1;
for (int j = 0; j < adj[i].size(); j++) {
int next = adj[i].get(j);
if (visit[next] == 0) {
temp[i] += dfs(next);
}
}
answer += Math.abs(temp[i]);
return temp[i];
}
출처:https://school.programmers.co.kr/learn/courses/30/lessons/76503