-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
47 lines (45 loc) · 1.12 KB
/
main.js
File metadata and controls
47 lines (45 loc) · 1.12 KB
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
38
39
40
41
42
43
44
45
46
47
/**
* @param {number} N
* @param {number[][]} edges
* @return {number[]}
*/
var sumOfDistancesInTree = function (N, edges) {
const conn = Array.from({length: N}, () => [])
const nodes = Array.from({length: N + 1}, () => ({}))
const disOfSubTree = Array.from({length: N + 1}, () => ({}))
for (const [a, b] of edges) {
conn[a].push(b)
conn[b].push(a)
nodes[a][b] = -1
nodes[b][a] = -1
disOfSubTree[a][b] = -1
disOfSubTree[b][a] = -1
}
function helper (cur, from) {
if (nodes[from][cur] != null && nodes[from][cur] !== -1) {
return {
dis: disOfSubTree[from][cur],
nodeCnt: nodes[from][cur]
}
}
nodes[from][cur] = 0
disOfSubTree[from][cur] = 0
for (const next of conn[cur]) {
if (next === from) {
continue
}
const {dis, nodeCnt} = helper(next, cur)
nodes[from][cur] += nodeCnt + 1
disOfSubTree[from][cur] += dis + 1 + nodeCnt
}
return {
dis: disOfSubTree[from][cur],
nodeCnt: nodes[from][cur]
}
}
const ans = []
for (let i = 0; i < N; i++) {
ans.push(helper(i, N).dis)
}
return ans
}