Skip to content

Commit 04887f6

Browse files
committed
Update index.ts
1 parent 2c9255c commit 04887f6

File tree

1 file changed

+15
-28
lines changed
  • minimum-score-after-removals-on-a-tree

1 file changed

+15
-28
lines changed

minimum-score-after-removals-on-a-tree/index.ts

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,31 @@ export default function minimumScore(
99
);
1010

1111
for (const [a, b] of edges) {
12-
addEdge(edgeMap, a, b);
13-
addEdge(edgeMap, b, a);
12+
edgeMap[a].push(b);
13+
edgeMap[b].push(a);
1414
}
1515
const children = new Array<Array<number>>(nums.length).fill([]).map(() =>
1616
[] as Array<number>
1717
);
1818

19-
const parent = new Array<number | null>(nums.length).fill(null);
2019
const visited = new Set<number>();
2120
const xor = new Array<number>(nums.length).fill(0);
22-
const ancestor = new Array(n).fill(0).map(() =>
21+
const ancestorToGrandson = new Array(n).fill(0).map(() =>
2322
new Array<boolean>(n).fill(false)
2423
);
2524

2625
const root = 0;
27-
bfs([root], visited, edgeMap, children, parent, xor, nums);
28-
29-
for (let i = 1; i < n; i++) {
30-
let j = parent[i];
31-
32-
while (typeof j === "number") {
33-
ancestor[j][i] = true;
34-
35-
j = parent[j];
36-
}
37-
}
26+
bfs([root], visited, edgeMap, children, ancestorToGrandson, xor, nums);
3827

3928
let ans = Infinity;
4029
for (let i = 1; i < n; i++) {
4130
for (let j = i + 1; j < n; j++) {
4231
let a: number, b: number, c: number;
43-
if (ancestor[i][j]) {
32+
if (ancestorToGrandson[i][j]) {
4433
a = xor[0] ^ xor[i];
4534
b = xor[i] ^ xor[j];
4635
c = xor[j];
47-
} else if (ancestor[j][i]) {
36+
} else if (ancestorToGrandson[j][i]) {
4837
a = xor[0] ^ xor[j];
4938
b = xor[j] ^ xor[i];
5039
c = xor[i];
@@ -65,7 +54,7 @@ function bfs(
6554
visited: Set<number>,
6655
edgeMap: number[][],
6756
children: number[][],
68-
parent: (number | null)[],
57+
ancestorToGrandson: boolean[][],
6958
xor: number[],
7059
nums: number[],
7160
) {
@@ -78,24 +67,22 @@ function bfs(
7867
const childs = edgeMap[node];
7968
if (childs.length) {
8069
for (const child of childs) {
81-
if (!visited.has(child) && parent[child] !== node) {
70+
if (!visited.has(child) && !ancestorToGrandson[node][child]) {
8271
visited.add(child);
72+
children[node].push(child);
73+
for (let i = 0; i < nums.length; i++) {
74+
ancestorToGrandson[i][child] =
75+
ancestorToGrandson[i][node];
76+
}
77+
ancestorToGrandson[node][child] = true;
8378

84-
addEdge(children, node, child);
85-
parent[child] = node;
8679
temp.push(child);
8780
}
8881
}
8982
}
9083
}
91-
bfs(temp, visited, edgeMap, children, parent, xor, nums);
84+
bfs(temp, visited, edgeMap, children, ancestorToGrandson, xor, nums);
9285
for (const node of nodes) {
9386
xor[node] = nums[node] ^ children[node].reduce((a, v) => a ^ xor[v], 0);
9487
}
9588
}
96-
97-
function addEdge(edgemap: number[][], a: number, b: number) {
98-
const arr = edgemap[a] ?? [];
99-
arr.push(b);
100-
edgemap[a] = arr;
101-
}

0 commit comments

Comments
 (0)