Editorial for COCI '16 Contest 2 #6 Burza
Submitting an official solution before solving the problem yourself is a bannable offence.
We will root the tree in node . To begin with, let's notice that, after the move, the coin will be located in a node of depth . It is clear that it is optimal for Daniel to label a node of depth in the move. Now we can reformulate the task: given a set of nodes where none of the nodes is a root and the nodes are of different depths, and there is no such node of depth where none of its predecessors is labeled, does such a set exist?
The nodes of depth will be called leaves. We can remove all nodes which don't have a leaf in its subtree (this includes the nodes of depth larger than ), because Daniel surely wins if the coin is located in such a node. Now the leaves are indeed leaves in the given tree. From now on, we will only observe trees obtained after removing unnecessary nodes.
Let's analyze the following simple algorithm: in the move, we will label a node of depth and remove all nodes in its subtree. By doing this, we have removed at least nodes in the step, which means that, in steps, we have removed at least nodes. This means that, in the case , we know Daniel can surely win.
This is our motivation to find an even better bound, and we will use the following algorithm to do so: initially, for each node , we will define as the minimal depth at which a descendant of has more than one child. In the move, we will label node of depth with the minimal value from the tree and remove all its descendants.
Using the induction principle, we will prove that, by using this algorithm, Daniel wins if it holds . For the sake of induction, we will not observe a single tree, but multiple trees, which will actually represent the subtrees of the original tree. We will denote with the minimal number for which it holds that, after moves, there exists a non-removed node for which it holds . If a number for which this holds doesn't exist, it means that we have made moves total, and in each move removed at least nodes (we count the descendants, but also the predecessors of that node, that are all mutually distinct). But, that means that in each of moves, we removed at least nodes (the worst case is for ), and then we removed nodes to depth , and at least below depth , because the tree branches at that depth.
Let's see what we have left: a new forest of trees that has at most nodes, and the new length to which the coin mustn't propagate is . Now, from the condition , it follows , so the claim holds by the induction assumption.
We have shown that for Daniel always wins, which means that we are left with solving the case when this doesn't hold. But, then , so we don't need to find a polynomial solution!
Let's denote the nodes in the order in which they appear in the dfs traversal of the tree. Now each node in the tree covers an interval of leaves, or, in other words, these are the nodes located in its subtree. We will solve the task using a dynamic programming approach. The state will be represented with a number and a bitmask bits in size. Let denote whether we can cover the first leaves by labeling only nodes of depth written in . The transition is simple, in a given moment we can choose any node out of at most where the first leaf in the tree is labeled with . Let's notice that, even though for each position the transition can be different nodes, each node will appear only in one position in the transition.
The total complexity is .
Comments