Minimum Cost Path With Even XOR Parity Problem
Description
LeetCode Problem 4100.
You are given a tree with n nodes numbered from 0 to n - 1, represented by a 2D integer array edges. Each edges[i] = [u_i, v_i, c_i] represents an edge between nodes u_i and v_i with cost c_i.
You are also given an integer array values of length n, where values[i] is the value of node i.
A path between two distinct nodes is Balanced-XOR if the bitwise XOR of the values of all nodes on the path, including both endpoints, has an even number of set bits.
The cost of a path is the sum of its edge costs.
Return the minimum cost of a Balanced-XOR path. If no such path exists, return -1.
The number 0 has zero set bits.
Example 1:

Input: edges = [[0,1,4],[0,2,6],[0,3,3],[3,4,5]], values = [3,1,7,1,3]
Output: 7
Explanation:
Balanced-XOR Path XOR Value Total Cost
1 - 0 - 2 1 XOR 3 XOR 7 = 5 4 + 6 = 10
1 - 0 - 3 1 XOR 3 XOR 1 = 3 4 + 3 = 7
1 - 0 - 3 - 4 1 XOR 3 XOR 1 XOR 3 = 0 4 + 3 + 5 = 12
2 - 0 - 3 7 XOR 3 XOR 1 = 5 6 + 3 = 9
2 - 0 - 3 - 4 7 XOR 3 XOR 1 XOR 3 = 6 6 + 3 + 5 = 14
The minimum cost is 7, achieved by the path 1 - 0 - 3. Thus, the answer is 7.
Example 2:

Input: edges = [[0,1,5],[1,2,10]], values = [1,7,3]
Output: 5
Explanation:
Balanced-XOR Path XOR Value Total Cost
0 - 1 1 XOR 7 = 6 5
0 - 1 - 2 1 XOR 7 XOR 3 = 5 5 + 10 = 15
The minimum cost is 5, achieved by the path 0 - 1. Thus, the answer is 5.
Example 3:

Input: edges = [[0,1,5],[0,2,7],[0,3,9]], values = [1,3,5,9]
Output: -1
Explanation:
- Every path between two distinct nodes has an XOR value with an odd number of set bits.
- Therefore, no Balanced-XOR path exists, so the answer is -1.
Constraints:
2 <= n <= 10^5edges.length == n - 1edges[i] = [u_i, v_i, c_i]0 <= u_i, v_i <= n - 11 <= c_i <= 10^9
values.length == n1 <= values[i] <= 10^9edgesrepresents a valid tree.
Solution
Approach: Tree DP on Parity
The XOR of some values has an even number of set bits exactly when the number of values with an odd popcount is even, because the parity of the popcount of an XOR is the XOR of the parities. So give every node a parity odd[v] = popcount(values[v]) % 2. A path is Balanced-XOR when it has at least two nodes and an even number of odd nodes. The cost of a path is the sum of its edge costs, and a path between two nodes of a tree is unique.
Root the tree at node 0 and let down[v][p] be the cheapest path that starts at v, goes down into its subtree, and whose nodes have total parity p. At first down[v][odd[v]] = 0 (the single node v) and the other entry is infinite.
Process the nodes from the bottom up. When a child v with edge cost c is merged into its parent p:
- A path can turn at
p. Join a path already hanging fromp(paritye, includingp) with one coming up fromv(paritye, so the total is even). The cost isdown[p][e] + down[v][e] + c. Take the minimum over all merges. - Then
v's paths extendp's: for each parityaof a path fromv,down[p][a ^ odd[p]]can be improved withdown[v][a] + c.
Because each child is merged after the ones before it, every pair of paths that meet at p is considered once. If no answer is found, return -1.
Complexity: O(n) time and O(n) space.
class Solution:
def minActivationCost(self, edges: List[List[int]], values: List[int]) -> int:
n = len(values)
odd = [bin(v).count("1") & 1 for v in values]
adj = [[] for _ in range(n)]
for u, v, c in edges:
adj[u].append((v, c))
adj[v].append((u, c))
# BFS order from node 0, so that children come after their parent
parent = [-1] * n
cost = [0] * n
order = [0]
for v in order:
for w, c in adj[v]:
if w != parent[v]:
parent[w] = v
cost[w] = c
order.append(w)
INF = 1 << 60
# down[v][p]: cheapest path that starts at v and goes down, whose nodes have popcount parity p
down = [[INF, INF] for _ in range(n)]
for v in range(n):
down[v][odd[v]] = 0
ans = INF
for v in reversed(order):
p = parent[v]
if p < 0:
continue
c = cost[v]
for e in range(2):
# join a path already hanging from p with a path coming up from v
ans = min(ans, down[p][e] + down[v][e] + c)
for a in range(2):
q = a ^ odd[p]
down[p][q] = min(down[p][q], down[v][a] + c)
return -1 if ans >= INF else ans