Number of Ways to Assign Edge Weights II Problem
Description
LeetCode Problem 3559.
There is an undirected tree with n nodes labeled from 1 to n, rooted at node 1. The tree is represented by a 2D integer array edges of length n - 1, where edges[i] = [u_i, v_i] indicates that there is an edge between nodes u_i and v_i.
Initially, all edges have a weight of 0. You must assign each edge a weight of either 1 or 2.
The cost of a path between any two nodes u and v is the total weight of all edges in the path connecting them.
You are given a 2D integer array queries. For each queries[i] = [u_i, v_i], determine the number of ways to assign weights to edges in the path such that the cost of the path between u_i and v_i is odd.
Return an array answer, where answer[i] is the number of valid assignments for queries[i].
Since the answer may be large, apply modulo 10^9 + 7 to each answer[i].
Note: For each query, disregard all edges not in the path between node u_i and v_i.
Example 1:

Input: edges = [[1,2]], queries = [[1,1],[1,2]]
Output: [0,1]
Explanation:
- Query [1,1]: The path from Node 1 to itself consists of no edges, so the cost is 0. Thus, the number of valid assignments is 0.
- Query [1,2]: The path from Node 1 to Node 2 consists of one edge (1 → 2). Assigning weight 1 makes the cost odd, while 2 makes it even. Thus, the number of valid assignments is 1.
Example 2:

Input: edges = [[1,2],[1,3],[3,4],[3,5]], queries = [[1,4],[3,4],[2,5]]
Output: [2,1,4]
Explanation:
- Query [1,4]: The path from Node 1 to Node 4 consists of two edges (1 → 3 and 3 → 4). Assigning weights (1,2) or (2,1) results in an odd cost. Thus, the number of valid assignments is 2.
- Query [3,4]: The path from Node 3 to Node 4 consists of one edge (3 → 4). Assigning weight 1 makes the cost odd, while 2 makes it even. Thus, the number of valid assignments is 1.
- Query [2,5]: The path from Node 2 to Node 5 consists of three edges (2 → 1, 1 → 3, and 3 → 5). Assigning (1,2,2), (2,1,2), (2,2,1), or (1,1,1) makes the cost odd. Thus, the number of valid assignments is 4.
Constraints:
2 <= n <= 10^5edges.length == n - 1edges[i] == [u_i, v_i]1 <= queries.length <= 10^5queries[i] == [u_i, v_i]1 <= u_i, v_i <= nedgesrepresents a valid tree.
Solution
Approach: LCA + Counting
For a query (u, v) the path has d = depth[u] + depth[v] - 2 * depth[lca(u, v)] edges. As in the first version, the number of assignments with an odd total is 2^(d-1) when d >= 1, and 0 when d = 0.
A BFS from node 1 gives each node's parent and depth, and binary lifting over the parents answers lowest-common-ancestor queries. Precompute the powers of 2 modulo 10^9 + 7 once, so every query costs O(log n).
Complexity: time O((n + q) log n), space O(n log n), where n is the number of nodes and q the number of queries.
from collections import deque
class Solution:
def assignEdgeWeights(
self, edges: List[List[int]], queries: List[List[int]]
) -> List[int]:
n = len(edges) + 1
m = n.bit_length()
g = [[] for _ in range(n + 1)]
for u, v in edges:
g[u].append(v)
g[v].append(u)
f = [[0] * m for _ in range(n + 1)]
p = [0] * (n + 1)
depth = [0] * (n + 1)
q = deque([1])
while q:
i = q.popleft()
f[i][0] = p[i]
for j in range(1, m):
f[i][j] = f[f[i][j - 1]][j - 1]
for j in g[i]:
if j != p[i]:
p[j] = i
depth[j] = depth[i] + 1
q.append(j)
mod = 10**9 + 7
pow2 = [1] * n
for i in range(1, n):
pow2[i] = pow2[i - 1] * 2 % mod
ans = []
for u, v in queries:
x, y = u, v
if depth[x] < depth[y]:
x, y = y, x
for j in range(m - 1, -1, -1):
if depth[x] - depth[y] >= (1 << j):
x = f[x][j]
for j in range(m - 1, -1, -1):
if f[x][j] != f[y][j]:
x, y = f[x][j], f[y][j]
if x != y:
x = p[x]
d = depth[u] + depth[v] - 2 * depth[x]
ans.append(0 if d == 0 else pow2[d - 1])
return ans