今天这题之前写过
做一次operation,原本是指对edge的两个node同时做xor with k
但其实可以把operation给转换成: 对tree的任两个node同时做xor with k
原因是当任两个node有connect
只要对path上所有edge的端点node都做一次operation
就会等于对这任两点做xor with k了
所以这题可以转换成
对所有node做总共偶数次xor with k,能得到的最大sum是多少
用dp就可以结束惹
def maximumValueSum(self, nums, k, edges):
"""
:type nums: List[int]
:type k: int
:type edges: List[List[int]]
:rtype: int
"""
maximum_odd_cur = nums[0]^k
maximum_even_cur = nums[0]
for i in range(1, len(nums)):
maximum_odd_cur, maximum_even_cur = max(maximum_even_cur+(nums[i]^k),
maximum_odd_cur+nums[i]), max(maximum_odd_cur+(nums[i]^k), maximum_even_cur+
nums[i])
return maximum_even_cur