DOJ logoDOJ
Problems
Recently added problemsSaved problems
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord-backed certification.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

Exploration Records

Back to problem

Editorial

Root the tree at vertex 111. Let dvd_vdv​ be the depth of vertex vvv, and let pv=dv mod 2p_v=d_v\bmod 2pv​=dv​mod2.

If the robot starts at uuu with battery level CCC and reaches a vertex xxx on the path, its battery level is

C+Hu−Hx.C+H_u-H_x.C+Hu​−Hx​.

Therefore, if Hmax⁡H_{\max}Hmax​ is the maximum height on the path from uuu to vvv,

A(u,v)=Hmax⁡−Hu,B(u,v)=Hmax⁡−Hv,A(u,v)=H_{\max}-H_u,\qquad B(u,v)=H_{\max}-H_v,A(u,v)=Hmax​−H

and Z(u,v)Z(u,v)Z(u,v) is the first vertex of height Hmax⁡H_{\max}Hmax​ when the path is traversed from uuu to vv.

Every edge changes the height by exactly 111, and H1=0H_1=0H1​=0. Hence

Hv≡dv(mod2).H_v\equiv d_v\pmod 2.Hv​≡dv​(mod2).

Define the integer variable

Kv=Hv−pv2.K_v=\frac{H_v-p_v}{2}.Kv​=2Hv​−

Then Hv=2Kv+pvH_v=2K_v+p_vHv​=2Kv​+pv​ and .

For an edge whose parent is qqq and whose child is ccc, the edge condition is equivalent to

Kc−Kq≤pq,Kq−Kc≤1−pq.K_c-K_q\le p_q,\qquad K_q-K_c\le1-p_q.Kc​−Kq​≤p

Consider a record (u,v,a,b,z)(u,v,a,b,z)(u,v,a,b,z). First, zzz must lie on the path from uuu to vvv.

Since Hz−Hu=aH_z-H_u=aHz​−Hu​=a and Hz−Hv=bH_z-H_v=bH,

2(Kz−Ku)=a+pu−pz,2(K_z-K_u)=a+p_u-p_z,2(Kz​−Ku​)=a+ 2(Kz−Kv)=b+pv−pz.2(K_z-K_v)=b+p_v-p_z.2(Kz​−Kv​)=b+

If either right-hand side is odd, the record is impossible. Otherwise, each equality is represented by two opposite difference constraints.

For a vertex xxx strictly before zzz on the path, the battery must not have reached 000 yet, so Hx<HzH_x<H_zHx​<H. Therefore

Kx−Kz≤{−1pz=0,0pz=1, px=0,−1pz=1, px=1.K_x-K_z\le \begin{cases} -1 & p_z=0,\\ 0 & p_z=1,\ p_x=0,\\ -1 & p_z=1,\ p_x=1. \end{cases}Kx​−

For a vertex xxx after zzz, it is sufficient to have Hx≤HzH_x\le H_zHx​≤Hz​. Thus

Kx−Kz≤{0pz=0, px=0,−1pz=0, px=1,0pz=1.K_x-K_z\le \begin{cases} 0 & p_z=0,\ p_x=0,\\ -1 & p_z=0,\ p_x=1,\\ 0 & p_z=1. \end{cases}Kx​−

Introduce an auxiliary variable X0X_0X0​ and interpret Kv=Xv−X0K_v=X_v-X_0Kv​=X. The condition becomes , and becomes both and . Every condition is now a difference constraint

Xy−Xx≤c.X_y-X_x\le c.Xy​−Xx​≤c.

The difficulty is that one record creates constraints to many vertices on a path. Expanding every such constraint explicitly produces O(NM)O(NM)O(NM) edges, which is too slow when combined with Bellman-Ford.

Use synchronous Bellman-Ford. Let DDD be the distances before one round and D′D'D′ the distances after the round. Start with D′=DD'=DD′=D, and for every constraint edge perform

Dy′←min⁡(Dy′,Dx+c).D'_y\leftarrow\min(D'_y,D_x+c).Dy′​←min(Dy′​

There are only the variables 0,1,⋯ ,N0,1,\cdots,N0,1,⋯,N, so it is enough to examine N+1N+1N+1 rounds.

Every path constraint created by a record goes from one vertex zzz 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 (C0,C1)(C_0,C_1)(C0​,C1​): an even-depth vertex xx 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 O(log⁡N)O(\log N)O(logN) complete chain prefixes

[head⁡(x),x][\operatorname{head}(x),x][head(x),x]

and one final interval contained in a single chain.

For a complete chain prefix, do not perform a segment-tree update. Store the pair (C0,C1)(C_0,C_1)(C0​,C1​) only at its lower endpoint xxx. 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 O(, 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 O(log⁡N)O(\log N)O(logN). After all updates, pushing the tags to all leaves costs O(N)O(N)O(N).

Thus one tree-path relaxation costs O(log⁡N)O(\log N)O(logN): there are O(log⁡N)O(\log N)O(logN) chain changes with O(1)O(1)O(1) prefix updates, plus one O(log⁡N)O(\log N) 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:

  • edge constraints, nonnegativity constraints, and equality constraints from records: O(N+M)O(N+M)O(N+M);
  • path constraints from records: O(Mlog⁡N)O(M\log N)O(MlogN);
  • propagation of HLD-prefix and segment-tree tags to actual vertices: O(N)O(N)O(N).

Therefore, the total time complexity is

O(N(N+Mlog⁡N)),O\left(N(N+M\log N)\right),O(N(N+MlogN)),

and the memory complexity, including binary lifting, is O(Nlog⁡N+M)O(N\log N+M)O(NlogN+M).

Initialize every distance to 000, which is equivalent to adding a super source with a zero-weight edge to every variable. If a value still decreases in the (N+1)(N+1)(N+1)-st round, a negative cycle exists and no answer is possible. Otherwise, once the distances stabilize, recover

Kv=Dv−D0,Hv=2Kv+pv.K_v=D_v-D_0,\qquad H_v=2K_v+p_v.Kv​=Dv​−D

Proof

From the expression C+Hu−HxC+H_u-H_xC+Hu​−Hx​ for the battery level, the minimum valid initial battery is exactly the path maximum minus HuH_u. 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

u
​
,
B
(
u
,
v
)
=
Hmax​−
Hv​,
v
pv​
​
.
Kv≥0K_v\ge0Kv​≥0
q
​
,
Kq​
−
Kc​≤
1−
pq​.
z​
−
Hv​=
b
pu​
−
pz​,
pv​
−
pz​.
z​
Kz​
≤
⎩⎨⎧​−10−1​pz​=0,pz​=1, px​=0,pz​=1, px​=1.​
K
z​
≤
⎩⎨⎧​0−10​pz​=0, px​=0,pz​=0, px​=1,pz​=1.​
v
​
−
X0​
Kv≥0K_v\ge0Kv​≥0
X0−Xv≤0X_0-X_v\le0X0​−Xv​≤0
K1=0K_1=0K1​=0
X1−X0≤0X_1-X_0\le0X1​−X0​≤0
X0−X1≤0X_0-X_1\le0X0​−X1​≤0
x→yx\to y
x→y
,
Dx​
+
c).
x
Dz+C0D_z+C_0Dz​+C0​
Dz+C1D_z+C_1Dz​+C1​
1)O(1)
O(1)
O(N)O(N)O(N)
O
(
log
N
)
zzz
zzz
zzz
0
​
,
Hv​
=
2Kv​+
pv​.
Hu​
000
zzz
zzz
zzz

Depth parity and height parity are equal, so every valid height assignment has a unique integer representation by the variables KvK_vKv​. Conversely, if the two difference constraints for every tree edge hold, then Hv=2Kv+pvH_v=2K_v+p_vHv​=2Kv​+pv​ makes every parent-child height difference exactly 111. The constraints Kv≥0K_v\ge0Kv​≥0 and K1=0K_1=0K1​=0 give nonnegative heights and H1=0H_1=0H1​=0.

In one synchronous Bellman-Ford round, a path edge z→xz\to xz→x contributes the old value DzD_zDz​ 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 D0D_0D0​ gives valid KvK_vKv​, and the reconstructed heights satisfy every original condition and every record.