Contest Duration: - (local time) (120 minutes) Back to Home

## D - Deterministic Placing Editorial by evima

### Notations

For a tree with some pieces on it, a sliding path is a simple path with two or more vertices where all vertices, except exactly one of the endpoints, are occupied with a piece. A sliding path decomposition is a decomposition of the tree into one or more sliding paths.
Two sliding path decompositions are distinguished when they are different as path decompositions, or the positions of the pieces are different.

### Observing the valid placements

Consider a placement of pieces that satisfy the condition. Given $$S_0$$ and $$S_1$$, the following algorithm finds the moves of the pieces by the first good operation.

• Mark all vertices as unprocessed.
• Repeat the following until there are no unprocessed vertices.
• Choose an unprocessed vertex without a piece, $$v$$.
• If $$S_1$$ contains $$v$$, mark $$v$$ as processed.
• Otherwise, among the one or more connected components obtained by deleting $$v$$ from the tree, there is exactly one connected component that currently has more pieces than the intersection of that component and $$S_1$$. From the vertex adjacent to $$v$$ in such a connected component, move the piece to $$v$$. Then, mark $$v$$ as processed.

Considering that no vertex is absent in both $$S_0$$ and $$S_1$$ (otherwise, one would be able to move a piece to that vertex, so the condition would not be satisfied), this algorithm gives the correspondence from a valid placement to a sliding path decomposition. Additionally, considering that different placements correspond to different sliding path decompositions (if they correspond to some decompositions), the set of the valid placements corresponds one-to-one to some subset $$X'$$ of the set $$X$$ of the sliding path decompositions.

### When is a sliding path decomposition contained in $$X'$$?

One way to move the pieces in a sliding path decomposition is to slide the pieces back and forth within each sliding path. If there is more than one way to move the pieces, that sliding path decomposition is not in $$X'$$, which can be shown to happen if and only if there are some adjacent sliding paths $$x$$ and $$y$$ such that one can move a piece from $$x$$ to $$y$$ without moving a piece from $$y$$ to other sliding paths.

Proof
If it were impossible to move a piece from a sliding path to another, there would be a unique way to move the pieces, so it must be possible to move a piece from some sliding path to another. Considering that the graph is a tree and each edge is traversed by at most one piece, the give-and-take relation between the sliding paths has no cycle, so there is a sliding path that is the endpoint of a chain. That endpoint is $$y$$, and the sliding path that gives a piece to $$y$$ is $$x$$.

From the above, it can be shown (by case-by-case analysis) that a sliding path decomposition is contained in $$X'$$ when the following holds for every pair of adjacent sliding paths $$x$$ and $$y$$.

• The occupied endpoint of one of $$x$$ and $$y$$ is connected by an edge to the unoccupied endpoint of the other.
• Non-endpoint vertices of $$x$$ and $$y$$ are connected by an edge.

### Finding $$|X'|$$ with tree DP

Let us root the tree at an arbitrary vertex. Now, we can count the valid sliding path decompositions by DP, classifying the vertices into the following five categories:

1. A vertex, not the occupied endpoint, in a sliding path whose unoccupied endpoint is a descendant of that vertex (or itself)
2. A vertex, not the unoccupied endpoint, in a sliding path whose occupied endpoint is a descendant of that vertex (or itself)
3. The occupied endpoint of a sliding path whose unoccupied endpoint is a descendant of that vertex
4. The unoccupied endpoint of a sliding path whose occupied endpoint is a descendant of that vertex
5. A vertex in a sliding path whose endpoints are both descendants of that vertex

Here is one of the transitions in this DP.

The current vertex falls into Category 1 when one of the following is satisfied, considering the condition for being in $$X'$$.

• All of the children fall into Category 3.
• One of the children falls into Category 1, and the others fall into Category 5.

Thus, the count for the current vertex can be found using the counts for the children.

Also, by symmetry, we can consider Patterns 1 and 2 the same, as well as Patterns 3 and 4. In this case, we have two ways to put pieces independently for each part of a sliding path decomposition separated by edges connecting non-endpoint vertices, so we should multiply the counts by $$2$$ in a transition between non-endpoint vertices.

posted:
last update: