Number of Ways to Assign Edge Weights II - Problem

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.

Input & Output

Example 1 — Basic Tree Path
$ Input: edges = [[1,2],[2,3],[3,4]], queries = [[1,4],[2,3]]
Output: [4,2]
💡 Note: For query [1,4]: path 1→2→3→4 has 3 edges, so 2^(3-1) = 4 ways to make sum odd. For query [2,3]: path 2→3 has 1 edge, so 2^(1-1) = 1 way, but we need 2^0 = 1, actually it's 2^(1-1) = 1, wait - for 1 edge we have 2 choices (1 or 2), exactly 1 makes it odd, so answer is 1. Actually, let me recalculate: for k edges, we need odd sum. If we have 1 edge, we can assign weight 1 (odd) or 2 (even), so 1 way gives odd. For 3 edges, we need 1 or 3 edges to have weight 1: C(3,1) + C(3,3) = 3 + 1 = 4 ways.
Example 2 — Adjacent Nodes
$ Input: edges = [[1,2],[2,3]], queries = [[1,2],[2,3],[1,3]]
Output: [1,1,2]
💡 Note: Query [1,2]: 1 edge, 1 way to make odd (weight=1). Query [2,3]: 1 edge, 1 way to make odd. Query [1,3]: path 1→2→3 has 2 edges, 2^(2-1) = 2 ways to make sum odd.
Example 3 — Same Node Query
$ Input: edges = [[1,2]], queries = [[1,1],[2,2]]
Output: [0,0]
💡 Note: Same node queries have 0 edges in path, cannot make sum odd (sum is 0), so 0 ways.

Constraints

  • 1 ≤ n ≤ 104
  • edges.length == n - 1
  • 1 ≤ ui, vi ≤ n
  • 1 ≤ queries.length ≤ 104
  • queries[i].length == 2

Visualization

Tap to expand
Tree Edge Weight Assignment Problem12341 or 21 or 21 or 2QueryTargetPath 1→4 has 3 edges, each can be weight 1 or 2Need odd sum: exactly 1 or 3 edges must have weight 1Ways = C(3,1) + C(3,3) = 3 + 1 = 4Formula: 2^(k-1) ways for k edges
Understanding the Visualization
1
Input Tree
Tree with edges and query node pairs
2
Find Paths
Identify unique path between query nodes
3
Count Ways
Calculate 2^(edges-1) ways to make sum odd
Key Takeaway
🎯 Key Insight: For k edges in a path, exactly 2^(k-1) weight assignments produce an odd sum
Asked in
Google 15 Amazon 12 Microsoft 8
12.5K Views
Medium Frequency
~35 min Avg. Time
387 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen