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 bits.
Subtask 1: the tree is a path
If for every , ancestry is exactly the order of vertex numbers. Hence
Alice stores each vertex number in bits. Bob decodes the two numbers and returns the smaller one, so this subtask has .
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 , a vertex number takes bits. Every label can contain
- its own vertex number in bits;
- in bits.
The total is exactly 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 is a light child of and is the heavy child, then
and therefore
Every light edge reduces the current subtree size by more than a factor of two. For , 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 be the entry time of and the final entry time in its subtree.
Suppose the light edges on the path to are
where is the parent and is the light child. Store
- ;
- every ;
- every ;
- the original number of .
Of two labels, call the vertex with smaller entry time and the other one . Scan the light records of from the root side. At the first record satisfying
return . If no record fails, return .
Let and assume neither vertex is an ancestor of the other. Because light children are visited before the heavy child and , the first edge from 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
bits.
Compressing the HLD labels
The same decoder can be retained while the boundaries are represented more compactly.
For a vertex , store the relative tail
instead of the absolute . The size bound after the -th light edge gives
The maximum total number of bits for all tails is
which yields a -bit scheme.
There is additional dependence between the tails. If there are light edges and light edges remain after record , define
and
The forced heavy subtrees imply
for small known bounds .
A tiny DP counts these monotone sequences and supports ranking and unranking them. For , there are possible sequences. Instead of reserving a fixed -bit field, use all bit strings of lengths through :
The actual vector length identifies the block. This gives an -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 bits. To reach , we must stop listing ancestor numbers and DFS boundaries explicitly.
A new viewpoint: a universal grammar
Let be a fixed recursive structure capable of representing every rooted tree of size at most while preserving LCAs. Both programs construct the same independently; it does not depend on the input tree.
Alice embeds every input vertex into a state of . Its label stores this state together with the original vertex number .
Consider one local node of the grammar. State 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
For every logical child direction , assign a tag . The tag system must satisfy three properties.
- For one color, different logical children receive different tags.
- Tags from any two different logical children uniquely determine the color.
- 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 , 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
and
The key lemma is the following.
For every positive integer sequence
with total sum at most , one can choose an order-preserving subsequence
of such that for every .
To prove it, find the first term at which the prefix sum exceeds and match it to the central value . 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 , choose
and set
Then .
In any input subtree, there is at most one child of size at least . Repeatedly following such a child produces a spine.
Except for the final spine subtree, let be one plus the total size of the side subtrees attached at the -th spine vertex. At most vertices lie outside the final large subtree, so
Use the universal sequence lemma to assign these values to an order-preserving subsequence of .
At a wrapper with assigned capacity , sort the side child sizes as
Their sum is at most , hence
The wrapper therefore has a continuation child for the spine and side child grammars with capacities
At the final spine vertex, the first child grammar has capacity , and the remaining ranked child grammars have capacities
By recursion, all side subtrees and the final subtree can be embedded. Thus this construction represents every rooted tree of size at most .
Dynamic programming for the number of states
Let be the number of states in .
For every , try every valid . Build the final local node and then the wrappers of backwards. Replace every child capacity by the previously computed state count , 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 -bit local code
For two logical children with grammar sizes , choose with and encode color as
The local cost is
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 over a finite field, and give children coordinates such as and . Any two distinct coordinates recover .
- Use asymmetric mixed coordinates so that the most expensive child receives the smallest alphabet.
Choosing the cheapest valid local code for every weight vector gives a -bit universal construction. One further saving is needed for full score.
The -bit idea: shared tag namespaces
Let the child grammar state counts be
Instead of giving the first two expensive children independent tag namespaces, let them share part of the tag space.
Create level- tags, each opening the largest grammar , and level- tags, each opening . Use the following undirected edges:
- an edge between two distinct level- tags;
- an edge between a level- tag and a level- tag.
There are
such edges. If this number is at least , map the colors injectively to edges and give the two endpoints to the first two logical children.
For a level---level- edge, both children open . Since 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
Finite-field coordinates for later children
Map all endpoint tags to distinct nonzero elements of a prime field , requiring . For a parameter , define
Call safe if
for every endpoint in use. Parameter is always safe. For nonzero , exactly one field element is forbidden, so there are exactly
safe parameters.
Assign a distinct safe parameter to each additional field-coded child and use a separate namespace of tags for its value .
From an endpoint and ,
The denominator is nonzero by safety.
From two field values with ,
and
Thus are the two roots of
which identifies the graph edge and hence the color.
If more children remain, give each one a private -tag namespace storing the color directly.
With field-coded children, the local cost is
subject to
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
The selected top-level decomposition has and .
Combine the universal state and the original vertex number as
The number of required codes is
The number of nonempty binary strings of lengths from through is
which is larger.
For length , define
If a code lies in
store
as exactly bits. Bob knows from the vector length and reconstructs the code uniquely.
Therefore every label has length at most .
Bob's decoder
Decode both labels into
Parse the two states simultaneously from the root grammar.
- If one state is , 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 . Build the children lists from and traverse the tree from root .
This construction proves and therefore earns all points. It does not claim a lower bound excluding -bit labelings.
Solution written by GPT5.6