-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.py
37 lines (34 loc) · 1017 Bytes
/
Solution.py
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
class Solution:
def minimumScore(self, nums: List[int], edges: List[List[int]]) -> int:
def dfs(i, fa, x):
res = nums[i]
for j in g[i]:
if j != fa and j != x:
res ^= dfs(j, i, x)
return res
def dfs2(i, fa, x):
nonlocal s, s1, ans
res = nums[i]
for j in g[i]:
if j != fa and j != x:
a = dfs2(j, i, x)
res ^= a
b = s1 ^ a
c = s ^ s1
t = max(a, b, c) - min(a, b, c)
ans = min(ans, t)
return res
g = defaultdict(list)
for a, b in edges:
g[a].append(b)
g[b].append(a)
s = 0
for v in nums:
s ^= v
n = len(nums)
ans = inf
for i in range(n):
for j in g[i]:
s1 = dfs(i, -1, j)
dfs2(i, -1, j)
return ans