Trains the technique from
LeetCode 3559Number of Ways to Assign Edge Weights IIThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A pipe network is a tree of n junctions numbered 1 through n, joined by n - 1 pipes given as edges, where edges[i] = [u, v] is a pipe between junction u and junction v.
Each query queries[j] = [u, v] names two junctions. Exactly one run of pipes joins them without doubling back; let m be how many pipes that run holds.
Each of those m pipes is to be given a weight of either 1 or 2. For each query return how many ways the weights can be chosen so that they add up to an odd total, taken modulo 1000000007. When the two junctions are the same the run holds no pipes, and no choice of weights can make an odd total, so the answer is 0.
Example 1
The run from junction 3 to junction 4 holds three pipes, so four of its eight weightings total odd. The second query names the same junction twice, so its run holds no pipes. The third holds two pipes.
Example 2
Every junction hangs directly off junction 1, so each of these runs holds two pipes and two of the four weightings total odd.
Example 3
The run from junction 1 to junction 2 goes by way of junction 3 and holds two pipes.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def odd_weightings_per_run(edges: list[list[int]], queries: list[list[int]]) -> list[int]:public List<Integer> oddWeightingsPerRun(int[][] edges, int[][] queries)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.