Restoring Weights

Consider any edge $$$(u,v)$$$ with weight $$$w$$$, and let $$$d_u > d_v$$$.

Then the weight of each edge must belong to the interval $$$[\max(1, d_v - d_u), l]$$$.

Additionally, for each vertex $$$v \neq 1$$$, there must exist at least one edge such that $$$d_u + w = d_v$$$.

Note that if all these conditions are satisfied, the weight assignment is valid.

For edges connecting two vertices $$$u, v$$$ with equal $$$d_u = d_v$$$, any weight can be assigned.

All other edges are oriented from smaller $$$d_u$$$ to larger $$$d_v$$$.

For each vertex, the assignment of weights to edges incident to it is independent of the assignment of weights to edges incident to other vertices.

Thus, if $$$f(v)$$$ is the number of ways to assign weights to edges incident to $$$v$$$ correctly, the answer is

$$$f(2) \cdot f(3) \cdot \ldots \cdot f(n)$$$.

For a fixed vertex, we first count the number of ways to assign weights to edges incident to this vertex. This value is equal to the product of the lengths of the intervals of possible edge weights over all edges.

However, in some situations, the actual distance to $$$v$$$ will exceed $$$d_v$$$, specifically when there does not exist an edge $$$(u,v)$$$ with weight $$$w$$$ such that $$$d_u + w = d_v$$$. In these cases, the weight of each edge $$$u \to v$$$ must be strictly greater than $$$d_v - d_u$$$. For such "bad" cases, the number of assignments is computed similarly, and then subtracted from the current value.

For edges with pre-determined weights, it is sufficient to check that their weight lies within the required interval; otherwise, the current answer is replaced with $$$0$$$.