父子情深 java深搜提示 越界错误,检查好几遍没有问题啊...
publicclassSolution {
/**
* 从 1 到 n 每个结点的权值。
* @param n int整型
* @param Edge Point类一维数组 (u, v) 集合
* @param Query Point类一维数组 Point.x 为题中 r, Point.y为题中 v
* @return long长整型一维数组
*/
List<List<Integer>> adj;
long[] res;
publiclong[] solve (intn, Point[] Edge,intq, Point[] Query) {
// write code here
intm = 100005;
adj =newArrayList<>(m);
for(inti = 0; i < m; i++){
adj.add(newLinkedList<>());
}
res =newlong[m];
for(Point p : Edge){
adj.get(p.x).add(p.y);
adj.get(p.y).add(p.x);
}
for(Point p : Query){
res[p.x] += (long)p.y;
}
dfs(1, 0);
long[] ans =newlong[n];
for(inti = 1; i <= n; i++){
ans[i-1] = res[i];
}
returnans;
}
publicvoiddfs(intcur,intfather){
List<Integer> child = adj.get(cur);
res[cur] += res[father];
for(intchildren : child){
if(children == father)continue;
dfs(children, cur);
}
}
}
全部评论
(0) 回帖