n nodes numbered 0 to n - 1; its edges come as a list edges, where edges[i] = [a, b] joins nodes a and b. Node i holds the number values[i].
Delete exactly two different edges, which splits the tree into three connected parts. For each part, take the bitwise XOR of the numbers in all its nodes. The *spread* of the cut is the largest of the three XORs minus the smallest.
Return the smallest spread you can achieve.values=[6, 2, 3, 7, 4], edges=[[0, 1], [1, 2], [0, 3], [3, 4]] → 1
Cutting 1–2 and 3–4 leaves {2} with XOR 3, {4} with XOR 4 and {0, 1, 3} with XOR 6 ^ 2 ^ 7 = 3. The spread is 4 − 3 = 1, and no pair of cuts does better.
values=[9, 9, 5, 5], edges=[[0, 1], [1, 2], [2, 3]] → 5
Cutting 1–2 and 2–3 gives {0, 1} with XOR 0 and {2}, {3} with XOR 5 each, a spread of 5. No other pair of cuts does better.
values=[1, 2, 3], edges=[[0, 1], [0, 2]] → 2
values=[6, 2, 3, 7, 4], edges=[[0, 1], [1, 2], [0, 3], [3, 4]]
1
Run checks the first 3 tests in your browser. Submit checks all 57 tests on the server, hidden ones included.