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

Evolution

Back to problem

Editorial

This is a tree-labeling problem. Alice knows the complete tree, while Bob must recover an LCA from two short labels without knowing the tree. Distinguishing the two queried vertices is not enough: when their encoded paths diverge, Bob must also recover the original vertex number of the branching point.

We first develop a direct structural encoding and see where it stops. We then place every possible input tree inside a fixed recursive universal grammar. The final improvement shares tag namespaces between child grammars and reduces the maximum label length to 505050 bits.

Subtask 1: the tree is a path

If Pi=i−1P_i=i-1Pi​=i−1 for every 1≤i<N1\leq i<N1≤i<N, ancestry is exactly the order of vertex numbers. Hence

LCA⁡(u,v)=min⁡(u,v).\operatorname{LCA}(u,v)=\min(u,v).LCA(u,v)=min(u,v).

Alice stores each vertex number in 888 bits. Bob decodes the two numbers and returns the smaller one, so this subtask has M=8M=8M=8.

For a general tree, the two vertex numbers do not determine their LCA. The labels must also describe enough of the tree structure.

Baseline: store the complete tree

Since N=256N=256N=256, a vertex number takes 888 bits. Every label can contain

  • its own vertex number in 888 bits;
  • P1,P2,…,P255P_1,P_2,\ldots,P_{255}P1​,P2​,…,P in bits.

The total is exactly 2 0482\,0482048 bits. Bob reconstructs the full parent array from either label and computes the LCA directly.

This baseline is important because it obeys the communication model without relying on shared global state.

First compression: heavy-light decomposition

At each vertex, choose a child of maximum subtree size as the heavy child. Every other child edge is light. Ties are broken deterministically.

If ccc is a light child of vvv and hhh is the heavy child, then

sz⁡(h)≥sz⁡(c),\operatorname{sz}(h)\geq\operatorname{sz}(c),sz(h)≥sz(c),

and therefore

2sz⁡(c)≤sz⁡(v)−1.2\operatorname{sz}(c)\leq\operatorname{sz}(v)-1.2sz(c)≤sz(v)−1.

Every light edge reduces the current subtree size by more than a factor of two. For N=256N=256N=256, a root-to-vertex path contains at most seven light edges.

Run a DFS that visits every light child first and the heavy child last. Let tin⁡(x)\operatorname{tin}(x)tin(x) be the entry time of xxx and tout⁡(x)\operatorname{tout}(x)tout(x) the final entry time in its subtree.

Suppose the light edges on the path to xxx are

(p0,c0),(p1,c1),…,(pk−1,ck−1),(p_0,c_0),(p_1,c_1),\ldots,(p_{k-1},c_{k-1}),(p0​,c0​),(p

where pip_ipi​ is the parent and cic_ici​ is the light child. Store

  • tin⁡(x)\operatorname{tin}(x)tin(x);
  • every pip_ipi​;
  • every tout⁡(ci)\operatorname{tout}(c_i)tout(ci;

Of two labels, call the vertex with smaller entry time xxx and the other one yyy. Scan the light records of xxx from the root side. At the first record satisfying

tin⁡(y)>tout⁡(ci),\operatorname{tin}(y)>\operatorname{tout}(c_i),tin(y)>tout(ci​),

return pip_ipi​. If no record fails, return xxx.

Let l=LCA⁡(x,y)l=\operatorname{LCA}(x,y)l=LCA(x,y) and assume neither vertex is an ancestor of the other. Because light children are visited before the heavy child and tin⁡(x)<tin⁡(y)\operatorname{tin}(x)<\operatorname{tin}(y)tin(x)<tin(y), the first edge from ll toward must be light. Earlier recorded light subtrees contain both vertices, while this one contains but not . Thus the first failed record has parent exactly .

There are at most seven records, so this scheme uses

8+7⋅16+8=1288+7\cdot16+8=1288+7⋅16+8=128

bits.

Compressing the HLD labels

The same decoder can be retained while the boundaries are represented more compactly.

For a vertex xxx, store the relative tail

di=tout⁡(ci)−tin⁡(x)d_i=\operatorname{tout}(c_i)-\operatorname{tin}(x)di​=tout(ci​)−tin(x)

instead of the absolute tout⁡(ci)\operatorname{tout}(c_i)tout(ci​). The size bound after the iii-th light edge gives

0≤di≤27−i−2.0\leq d_i\leq2^{7-i}-2.0≤di​≤27−i−2.

The maximum total number of bits for all tails is

7+6+5+4+3+2=27,7+6+5+4+3+2=27,7+6+5+4+3+2=27,

which yields a 999999-bit scheme.

There is additional dependence between the tails. If there are kkk light edges and t=k−i−1t=k-i-1t=k−i−1 light edges remain after record iii, define

Bi=2t+1−t−2B_i=2^{t+1}-t-2Bi​=2t+1−t−2

and

zi=di−Bi.z_i=d_i-B_i.zi​=di​−Bi​

The forced heavy subtrees imply

A0≥z0≥z1≥⋯≥zk−1≥0A_0\geq z_0\geq z_1\geq\cdots\geq z_{k-1}\geq0A0​≥z0​≥z

for small known bounds AiA_iAi​.

A tiny DP counts these monotone sequences and supports ranking and unranking them. For k=6k=6k=6, there are 939 993939\,993939993 possible sequences. Instead of reserving a fixed 202020-bit field, use all bit strings of lengths 151515 through 191919:

215+216+217+218+219=1 015 808.2^{15}+2^{16}+2^{17}+2^{18}+2^{19}=1\,015\,808.215+216+217+

The actual vector length identifies the block. This gives an 838383-bit construction.

This HLD family is a useful direct description of the input tree. Its bottleneck is also clear: up to seven original light-ancestor numbers already cost 565656 bits. To reach 505050, we must stop listing ancestor numbers and DFS boundaries explicitly.

A new viewpoint: a universal grammar

Let UnU_nUn​ be a fixed recursive structure capable of representing every rooted tree of size at most nnn while preserving LCAs. Both programs construct the same U256U_{256}U256​ independently; it does not depend on the input tree.

Alice embeds every input vertex vvv into a state state⁡(v)\operatorname{state}(v)state(v) of U256U_{256}U256​. Its label stores this state together with the original vertex number vvv.

Consider one local node of the grammar. State 000 represents the current location. Every nonzero state consists of a tag selecting a child grammar and a substate inside that grammar.

Let the original input vertex represented at this local node have color

c∈{0,1,…,255}.c\in\{0,1,\ldots,255\}.c∈{0,1,…,255}.

For every logical child direction iii, assign a tag fi(c)f_i(c)fi​(c). The tag system must satisfy three properties.

  1. For one color, different logical children receive different tags.
  2. Tags from any two different logical children uniquely determine the color.
  3. The grammar opened by a tag is large enough for the corresponding child subtree.

Bob parses the two states simultaneously. If the tags first become different at a local node, the two input vertices lie in different child directions of the current input vertex. That input vertex is their LCA, and the two tags reveal its color, which is its original number.

If the tags are equal, both states continue in the same logical direction. If either state is 000, that label's vertex is the current location and is therefore the LCA.

It remains to build a small grammar that can embed every tree.

A universal sequence for bounded total mass

Define

A0=()A_0=()A0​=()

and

Am=A⌊(m−1)/2⌋+[m]+A⌈(m−1)/2⌉.A_m=A_{\lfloor(m-1)/2\rfloor}+[m]+A_{\lceil(m-1)/2\rceil}.Am​=A⌊(m−1)/2⌋​

The key lemma is the following.

For every positive integer sequence

b1,b2,…,btb_1,b_2,\ldots,b_tb1​,b2​,…,bt​

with total sum at most mmm, one can choose an order-preserving subsequence

a1,a2,…,ata_1,a_2,\ldots,a_ta1​,a2​,…,at​

of AmA_mAm​ such that bi≤aib_i\leq a_ibi​≤ai​ for every .

To prove it, find the first term at which the prefix sum exceeds ⌊(m−1)/2⌋\lfloor(m-1)/2\rfloor⌊(m−1)/2⌋ and match it to the central value mmm. The sum before it fits in the left recursive sequence, and the sum after it fits in the right recursive sequence. Apply induction to both sides.

Embedding an arbitrary tree

When constructing UnU_nUn​, choose

0≤m≤⌊n−12⌋0\leq m\leq\left\lfloor\frac{n-1}{2}\right\rfloor0≤m≤⌊2n−1​⌋

and set

C=n−m.C=n-m.C=n−m.

Then C>n/2C>n/2C>n/2.

In any input subtree, there is at most one child of size at least CCC. Repeatedly following such a child produces a spine.

Except for the final spine subtree, let bib_ibi​ be one plus the total size of the side subtrees attached at the iii-th spine vertex. At most mmm vertices lie outside the final large subtree, so

∑bi≤m.\sum b_i\leq m.∑bi​≤m.

Use the universal sequence lemma to assign these values to an order-preserving subsequence of AmA_mAm​.

At a wrapper with assigned capacity aia_iai​, sort the side child sizes as

s1≥s2≥⋯ .s_1\geq s_2\geq\cdots.s1​≥s2​≥⋯.

Their sum is at most ai−1a_i-1ai​−1, hence

sr≤⌊ai−1r⌋.s_r\leq\left\lfloor\frac{a_i-1}{r}\right\rfloor.sr​≤⌊rai​

The wrapper therefore has a continuation child for the spine and side child grammars with capacities

ai−1,⌊ai−12⌋,⌊ai−13⌋,….a_i-1,\left\lfloor\frac{a_i-1}{2}\right\rfloor, \left\lfloor\frac{a_i-1}{3}\right\rfloor,\ldots.ai​−1,⌊2

At the final spine vertex, the first child grammar has capacity C−1C-1C−1, and the remaining ranked child grammars have capacities

⌊n−12⌋,⌊n−13⌋,….\left\lfloor\frac{n-1}{2}\right\rfloor, \left\lfloor\frac{n-1}{3}\right\rfloor,\ldots.⌊2n−1​⌋,⌊3

By recursion, all side subtrees and the final subtree can be embedded. Thus this construction represents every rooted tree of size at most nnn.

Dynamic programming for the number of states

Let W(n)W(n)W(n) be the number of states in UnU_nUn​.

W(0)=0,W(1)=1.W(0)=0,\qquad W(1)=1.W(0)=0,W(1)=1.

For every nnn, try every valid mmm. Build the final local node and then the wrappers of AmA_mAm​ backwards. Replace every child capacity sss by the previously computed state count W(s)W(s), evaluate the required local state count, and keep the smallest construction.

The DP also stores the chosen decomposition and local tag scheme. The implementation reconstructs the actual grammar, the state wrapping operation, and its inverse parser; merely computing the numerical cost would not be sufficient.

A 515151-bit local code

For two logical children with grammar sizes w0,w1w_0,w_1w0​,w1​, choose L,RL,RL,R with LR≥256LR\geq256 and encode color as

(c mod L,⌊c/L⌋).(c\bmod L,\lfloor c/L\rfloor).(cmodL,⌊c/L⌋).

The local cost is

1+Lw0+Rw1.1+Lw_0+Rw_1.1+Lw0​+Rw1​.

With more children, one may use several pairwise-decodable codes.

  • Different children omit disjoint subsets of the eight color bits, so any two tags together contain all bits.
  • View the color as two coordinates (x,y)(x,y)(x,y) over a finite field, and give children coordinates such as xxx and y+txy+txy+tx. Any two distinct coordinates recover (x,y)(x,y)(x,.

Choosing the cheapest valid local code for every weight vector gives a 515151-bit universal construction. One further saving is needed for full score.

The 505050-bit idea: shared tag namespaces

Let the child grammar state counts be

w0≥w1≥w2≥⋯ .w_0\geq w_1\geq w_2\geq\cdots.w0​≥w1​≥w2​

Instead of giving the first two expensive children independent tag namespaces, let them share part of the tag space.

Create xxx level-000 tags, each opening the largest grammar G0G_0G0​, and yyy level-111 tags, each opening . Use the following undirected edges:

  • an edge between two distinct level-000 tags;
  • an edge between a level-000 tag and a level-111 tag.

There are

(x2)+xy\binom{x}{2}+xy(2x​)+xy

such edges. If this number is at least 256256256, map the 256256256 colors injectively to edges and give the two endpoints to the first two logical children.

For a level-000--level-000 edge, both children open G0G_0G0​. Since G0G_0G0​ is at least as capable as , the second child subtree can be re-embedded into . For a level---level- edge, each child opens its nominal grammar.

The endpoints of an edge are distinct, and the edge uniquely identifies the color. The cost of the first two children becomes

xw0+yw1.xw_0+yw_1.xw0​+yw1​.

Finite-field coordinates for later children

Map all endpoint tags to distinct nonzero elements a,ba,ba,b of a prime field Fq\mathbb{F}_qFq​, requiring x+y≤q−1x+y\leq q-1x+y≤q. For a parameter , define

ht=a+b+tab(modq).h_t=a+b+tab\pmod q.ht​=a+b+tab(modq).

Call ttt safe if

1+tz≠0(modq)1+tz\neq0\pmod q1+tz=0(modq)

for every endpoint zzz in use. Parameter 000 is always safe. For nonzero ttt, exactly one field element z=−1/tz=-1/tz=−1/t is forbidden, so there are exactly

q−(x+y)q-(x+y)q−(x+y)

safe parameters.

Assign a distinct safe parameter to each additional field-coded child and use a separate namespace of qqq tags for its value hth_tht​.

From an endpoint aaa and hth_tht​,

b=ht−a1+ta.b=\frac{h_t-a}{1+ta}.b=1+taht​−a​.

The denominator is nonzero by safety.

From two field values ht,huh_t,h_uht​,hu​ with t≠ut\neq ut=,

ab=ht−hut−u,ab=\frac{h_t-h_u}{t-u},ab=t−uht​−h

and

a+b=ht−t(ab).a+b=h_t-t(ab).a+b=ht​−t(ab).

Thus a,ba,ba,b are the two roots of

X2−(a+b)X+ab,X^2-(a+b)X+ab,X2−(a+b)X+ab,

which identifies the graph edge and hence the color.

If more children remain, give each one a private 256256256-tag namespace storing the color directly.

With rrr field-coded children, the local cost is

1+xw0+yw1+q∑i=2r+1wi+256∑i=r+2d−1wi,1+xw_0+yw_1 +q\sum_{i=2}^{r+1}w_i +256\sum_{i=r+2}^{d-1}w_i,1+xw0​+yw

subject to

(x2)+xy≥256,x+y≤q−1,r≤q−(x+y).\binom{x}{2}+xy\geq256, \qquad x+y\leq q-1, \qquad r\leq q-(x+y).(2x​)+xy≥256,x+

The reference construction compares every useful shared pattern with the independent local-code candidates and keeps the cheapest valid scheme.

Tags that open a larger grammar

A subtle but essential point is that a logical child may use a shared tag opening an earlier, larger grammar rather than its nominal grammar. Alice must not construct a state in the nominal grammar and merely replace the tag. She must re-embed that child subtree into the grammar actually referenced by the tag.

The child grammars are nested by capacity, so the larger grammar always accepts the smaller subtree. This re-embedding is what makes shared tags valid.

Final state count and variable-length encoding

Putting the shared local code into the universal DP gives

W50(256)=8 480 600 182 708.W_{50}(256)=8\,480\,600\,182\,708.W50​(256)=8480600182708.

The selected top-level decomposition has m=87m=87m=87 and C=169C=169C=169.

Combine the universal state and the original vertex number as

code⁡=256⋅state⁡+own⁡.\operatorname{code}=256\cdot\operatorname{state}+\operatorname{own}.code=256⋅state+own.

The number of required codes is

256W50(256)=2 171 033 646 773 248.256W_{50}(256)=2\,171\,033\,646\,773\,248.256W50​(256)=2171033646773248.

The number of nonempty binary strings of lengths from 111 through 505050 is

21+22+⋯+250=251−2=2 251 799 813 685 246,2^1+2^2+\cdots+2^{50} =2^{51}-2 =2\,251\,799\,813\,685\,246,21+22+⋯+2

which is larger.

For length ℓ\ellℓ, define

start⁡(ℓ)=2ℓ−2.\operatorname{start}(\ell)=2^\ell-2.start(ℓ)=2ℓ−2.

If a code lies in

[2ℓ−2,2ℓ+1−3],[2^\ell-2,2^{\ell+1}-3],[2ℓ−2,2ℓ+1−3],

store

code⁡−start⁡(ℓ)\operatorname{code}-\operatorname{start}(\ell)code−start(ℓ)

as exactly ℓ\ellℓ bits. Bob knows ℓ\ellℓ from the vector length and reconstructs the code uniquely.

Therefore every label has length at most 505050.

Bob's decoder

Decode both labels into

own⁡=code⁡ mod 256,state⁡=⌊code⁡256⌋.\operatorname{own}=\operatorname{code}\bmod256, \qquad \operatorname{state}=\left\lfloor\frac{\operatorname{code}}{256}\right\rfloor.own=codemod256,state=⌊256

Parse the two states simultaneously from the root grammar.

  • If one state is 000, that label's vertex is represented at the current location. It is an ancestor of the other vertex and is the LCA.
  • If the two tags are equal, both paths use the same logical child. Continue with the two substates in the common next grammar.
  • If the tags differ, the input paths diverge at the current input vertex. Decode its original number from the tag pair and return it.

For a fixed color, tags of different logical children are distinct, and any valid pair from different children uniquely determines the color. Hence every case is correct.

An unused universal wrapper does not contain an actual input vertex with occupied side children, so two valid input paths cannot first diverge there. The first differing tags always correspond to the true LCA in the input tree.

Two-stage implementation details

Alice and Bob independently build the same deterministic universal DP and local schemes. No runtime pointer, table, global variable, or file created by Alice is needed by Bob. Identical constants, tie-breaking, and state enumeration are sufficient for both processes to interpret the labels consistently.

Do not assume Pi<iP_i<iPi​<i. Build the children lists from PPP and traverse the tree from root 000.

This construction proves M≤50M\leq50M≤50 and therefore earns all 100100100 points. It does not claim a lower bound excluding 494949-bit labelings.

Solution written by GPT5.6

255
​
255⋅8=2 040255\cdot8=2\,040255⋅8=2040
1​
,
c1​
)
,
…
,
(
pk−1​
,
ck−1​
)
,
​
)
  • the original number of xxx.
  • l
    xxx
    xxx
    yyy
    lll
    .
    1
    ​
    ≥
    ⋯≥
    zk−1​≥
    0
    218
    +
    219=
    1015808.
    +
    [m]+
    A⌈(m−1)/2⌉​.
    iii
    −
    1
    ​
    ⌋
    .
    a
    i​
    −
    1
    ​
    ⌋
    ,
    ⌊3ai​−1​⌋
    ,
    …
    .
    n−1
    ​
    ⌋
    ,
    …
    .
    W
    (
    s
    )
    L
    R
    ≥
    256
    ccc
    y
    )
  • Use asymmetric mixed coordinates so that the most expensive child receives the smallest alphabet.
  • ≥
    ⋯.
    G1G_1
    G1​
    G1G_1G1​
    G0G_0G0​
    000
    111
    −
    1
    ttt
    u
    u
    ​
    ​
    ,
    1
    ​
    +
    qi=2∑r+1​wi​+
    256i=r+2∑d−1​wi​,
    y≤
    q−
    1,r≤
    q−
    (x+
    y).
    50
    =
    251−
    2=
    2251799813685246,
    code
    ​
    ⌋
    .