-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
41 lines (36 loc) · 956 Bytes
/
test.js
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
let adj = {}; // Adjacency list
let sizes = {}; // Sizes of subtrees rooted at each node
function dfs(node) {
if (sizes[node] !== undefined) return sizes[node];
let size = 1; // Include the node itself
for (let neighbor of adj[node] || []) {
size += dfs(neighbor);
}
sizes[node] = size;
return size;
}
// Given array of edges
const edges = [
[1, 2],
[1, 3],
[3, 6],
[3, 4],
[3, 5],
];
// Build the adjacency list
for (const [u, v] of edges) {
if (!adj[u]) adj[u] = [];
adj[u].push(v);
}
const totalPoints = 121;
// Run DFS to populate the 'sizes' object
dfs(1);
console.log(sizes);
// Calculate the number of points each node should get
const points = {};
for (const [node, size] of Object.entries(sizes)) {
points[node] = Math.floor(totalPoints * (size / sizes[1]));
}
console.log(points);
// Output the result in the order of the unique nodes in the tree
const result = edges.map(([parent, child]) => points[child]);