Editorial for COCI '16 Contest 2 #6 Burza


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

We will root the tree in node 1. To begin with, let's notice that, after the i^\text{th} move, the coin will be located in a node of depth i. It is clear that it is optimal for Daniel to label a node of depth i in the i^\text{th} 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 k where none of its predecessors is labeled, does such a set exist?

The nodes of depth k 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 k), 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 i^\text{th} move, we will label a node of depth i and remove all nodes in its subtree. By doing this, we have removed at least k-i+1 nodes in the i^\text{th} step, which means that, in k steps, we have removed at least \frac{k(k+1)}{2} nodes. This means that, in the case n \le \frac{k(k+1)}{2}, 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 v, we will define f(v) as the minimal depth at which a descendant of v has more than one child. In the i^\text{th} move, we will label node v of depth i with the minimal value f(v) 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 k^2 \ge n. 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 d the minimal number for which it holds that, after d moves, there exists a non-removed node v for which it holds f(v) = d. If a number d for which this holds doesn't exist, it means that we have made k moves total, and in each move removed at least k nodes (we count the descendants, but also the predecessors of that node, that are all mutually distinct). But, that means that in each of d moves, we removed at least 2k-d nodes (the worst case is for f(v) = d), and then we removed d nodes to depth d, and at least 2(k-d) below depth d, because the tree branches at that depth.

Let's see what we have left: a new forest of trees that has at most n_2 \le n-d \times (2k-d) nodes, and the new length to which the coin mustn't propagate is k_2 = k-d. Now, from the condition k^2 \ge n, it follows k_2^2 \ge n_2, so the claim holds by the induction assumption.

We have shown that for k^2 \ge n Daniel always wins, which means that we are left with solving the case when this doesn't hold. But, then k < 20, 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 k bits in size. Let dp[T][mask] denote whether we can cover the first T leaves by labeling only nodes of depth written in mask. The transition is simple, in a given moment we can choose any node out of at most k where the first leaf in the tree is labeled with T. Let's notice that, even though for each position T the transition can be k different nodes, each node will appear only in one position in the transition.

The total complexity is \mathcal O(2^{\sqrt n} \times n).


Comments

There are no comments at the moment.