#P17000. [NWERC 2019] Balanced Cut

    ID: 19288 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: (无) 上传者: 标签>动态规划 DP贪心2019Special JudgeICPCAd-hoc

[NWERC 2019] Balanced Cut

Problem Description

:::align{center}

:::

Professor Anna van Lier is preparing to give a lecture on balanced binary search trees. Recall that these are binary trees with two properties:

  • Balanced tree: For every node, the height of its left subtree and the height of its right subtree differ by at most 11. For instance in Figure, the left and right subtrees of node 77 have heights 22 and 11, respectively. If a node does not have a left (or right) subtree then that subtree is considered to have height 00.

  • Search tree: Each node has a value. The value of a node is greater than all the values in the left subtree of the node, and smaller than all the values in the right subtree of the node. For instance in Figure, the left subtree of node 77 contains the values 44, 55 and 66 which are all smaller than 77.

Anna got a picture of such a tree from a colleague. This tree has nn nodes with the values 11 to nn. However, it turns out to be too big to fit on her slides so she would like to make it smaller. In particular, she would like to erase some nodes from the tree such that it has exactly kk remaining nodes. Whenever she erases a node, she also erases the subtrees of that node. Of course, the resulting tree must still be a balanced binary search tree.

For pedagogical purposes, Anna would like the node values in her final tree to be small. Therefore, she wants the list of the kk remaining node values to be the lexicographically smallest possible. For example she would prefer a tree containing values 2,5,92, 5, 9 over a tree containing values 2,6,72, 6, 7.

As Anna is far too busy doing more important things, the task of finding which nodes to erase falls upon one of her teaching assistants, i.e., you.

Input Format

The input consists of:

  • One line with two integers nn and kk (2n51052 \leq n \leq 5 \cdot 10^{5}, 1kn11 \leq k \leq n - 1), the number of nodes in the tree and the number of nodes to keep.
  • nn lines, the iith of which contains an integer pip_i (1pin1\leq p_i \leq n or pi=1p_i = -1), the parent of the node with value ii or 1-1 if the node with value ii is the root.

It is guaranteed that the given tree is a balanced binary search tree.

Output Format

Output a single line with a binary string of length nn. The iith character should be ‘1’ if the node with value ii should be kept, and ‘0’ if it should be erased.

3 1
2
-1
2
010
8 5
3
1
-1
5
7
5
3
7
11101010