Root the tree at vertex . Let be the depth of vertex , and let .
If the robot starts at with battery level and reaches a vertex on the path, its battery level is
Therefore, if is the maximum height on the path from to ,
and is the first vertex of height when the path is traversed from to .
Every edge changes the height by exactly , and . Hence
Define the integer variable
Then and .
For an edge whose parent is and whose child is , the edge condition is equivalent to
Consider a record . First, must lie on the path from to .
Since and ,
If either right-hand side is odd, the record is impossible. Otherwise, each equality is represented by two opposite difference constraints.
For a vertex strictly before on the path, the battery must not have reached yet, so . Therefore
For a vertex after , it is sufficient to have . Thus
Introduce an auxiliary variable and interpret . The condition becomes , and becomes both and . Every condition is now a difference constraint
The difficulty is that one record creates constraints to many vertices on a path. Expanding every such constraint explicitly produces edges, which is too slow when combined with Bellman-Ford.
Use synchronous Bellman-Ford. Let be the distances before one round and the distances after the round. Start with , and for every constraint edge perform
There are only the variables , so it is enough to examine rounds.
Every path constraint created by a record goes from one vertex to vertices on a tree path. Its weight depends only on the depth parity of the destination. Hence one path constraint can be represented by a pair : an even-depth vertex receives candidate , and an odd-depth vertex receives .
Process all such relaxations in one Bellman-Ford round with HLD.
Vertices of one HLD chain are stored consecutively from the chain head downward. The usual HLD decomposition of an arbitrary tree path consists of complete chain prefixes
and one final interval contained in a single chain.
For a complete chain prefix, do not perform a segment-tree update. Store the pair only at its lower endpoint . After all records have been processed, scan each chain once from bottom to top while maintaining the minimum pair seen so far. This propagates every prefix update to exactly the vertices it covers. Each prefix update costs , and all prefix propagation costs .
Only the final interval inside one chain is processed by a lazy segment tree with a range-chmin update. Every segment-tree node stores two minimum tags, one for each depth parity. One update costs . After all updates, pushing the tags to all leaves costs .
Thus one tree-path relaxation costs : there are chain changes with prefix updates, plus one interval update. Each record needs only two such paths, one before and one after . Their endpoints excluding can be precomputed with LCA and binary lifting.
The cost of one Bellman-Ford round is:
Therefore, the total time complexity is
and the memory complexity, including binary lifting, is .
Initialize every distance to , which is equivalent to adding a super source with a zero-weight edge to every variable. If a value still decreases in the -st round, a negative cycle exists and no answer is possible. Otherwise, once the distances stabilize, recover
Proof
From the expression for the battery level, the minimum valid initial battery is exactly the path maximum minus . With that minimum battery, the battery is exactly at vertices attaining the path maximum. Hence a record is equivalent to the two height-difference equalities at , the strict upper bounds before , and the non-strict upper bounds after .
Solution written by GPT5.6
Depth parity and height parity are equal, so every valid height assignment has a unique integer representation by the variables . Conversely, if the two difference constraints for every tree edge hold, then makes every parent-child height difference exactly . The constraints and give nonnegative heights and .
In one synchronous Bellman-Ford round, a path edge contributes the old value plus its edge weight. The HLD structure sends the minimum of exactly these candidates to exactly the covered vertices. A bottom-to-top chain scan is equivalent to applying every stored chain-prefix update to all vertices in that prefix, and the segment-tree range-chmin operation is equivalent to applying the final chain interval update. Therefore one compressed round produces exactly the same distances as explicitly relaxing every path edge.
The algorithm is consequently identical to synchronous Bellman-Ford on the complete difference-constraint graph. A system of difference constraints is feasible if and only if that graph has no negative cycle. When no negative cycle exists, the stabilized distances satisfy all constraints. Translating them by gives valid , and the reconstructed heights satisfy every original condition and every record.