CF1623E.Middle Duplication

省选/NOI-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

A binary tree of nn nodes is given. Nodes of the tree are numbered from 11 to nn and the root is the node 11. Each node can have no child, only one left child, only one right child, or both children. For convenience, let's denote lul_u and rur_u as the left and the right child of the node uu respectively, lu=0l_u = 0 if uu does not have the left child, and ru=0r_u = 0 if the node uu does not have the right child.

Each node has a string label, initially is a single character cuc_u. Let's define the string representation of the binary tree as the concatenation of the labels of the nodes in the in-order. Formally, let f(u)f(u) be the string representation of the tree rooted at the node uu. f(u)f(u) is defined as follows: $$ f(u) = \begin{cases} \texttt{ \lt empty string \gt }, & \text{if }u = 0; \\ f(l_u) + c_u + f(r_u) & \text{otherwise}, \end{cases} $$ where ++ denotes the string concatenation operation.

This way, the string representation of the tree is f(1)f(1).

For each node, we can duplicate its label at most once, that is, assign cuc_u with cu+cuc_u + c_u, but only if uu is the root of the tree, or if its parent also has its label duplicated.

You are given the tree and an integer kk. What is the lexicographically smallest string representation of the tree, if we can duplicate labels of at most kk nodes?

A string aa is lexicographically smaller than a string bb if and only if one of the following holds:

  • aa is a prefix of bb, but a≠ba \ne b;
  • in the first position where aa and bb differ, the string aa has a letter that appears earlier in the alphabet than the corresponding letter in bb.

给定一棵包含 nn 个节点的二叉树。树中节点编号为 11 到 nn,根节点为节点 11。每个节点可以没有子节点、仅有一个左子节点、仅有一个右子节点,或同时具有左右两个子节点。为方便起见,记节点 uu 的左子节点和右子节点分别为 lul_u 和 rur_u;若 uu 无左子节点,则令 lu=0l_u = 0;若 uu 无右子节点,则令 ru=0r_u = 0。

每个节点带有一个字符串标签,初始时为单个字符 cuc_u。我们定义该二叉树的字符串表示为所有节点按中序遍历顺序的标签拼接结果。形式化地,令 f(u)f(u) 表示以节点 uu 为根的子树的字符串表示。f(u)f(u) 定义如下:

f(u)={<empty string>,if u=0;f(lu)+cu+f(ru)otherwise,f(u) = \begin{cases} \texttt{<empty string>}, & \text{if }u = 0; \\ f(l_u) + c_u + f(r_u) & \text{otherwise}, \end{cases}

其中 ++ 表示字符串拼接操作。

因此,整棵树的字符串表示即为 f(1)f(1)。

对于每个节点,我们至多可将其标签复制一次,即把 cuc_u 替换为 cu+cuc_u + c_u(即自身拼接),但前提是:该节点是整棵树的根节点,或者其父节点的标签也已被复制。

现给定该树及一个整数 kk。若我们最多可对 kk 个节点执行上述复制操作,那么该树可能得到的字典序最小的字符串表示是什么?

字符串 aa 字典序小于字符串 bb,当且仅当满足以下任一条件:

  • aa 是 bb 的真前缀(即 aa 是 bb 的前缀,但 a≠ba \ne b);
  • 在 aa 与 bb 首次出现差异的位置上,aa 中的字符在字母表中位于 bb 中对应字符之前。

输入格式

The first line contains two integers nn and kk (1≤k≤n≤2⋅1051 \le k \le n \le 2 \cdot 10^5).

The second line contains a string cc of nn lower-case English letters, where cic_i is the initial label of the node ii for 1≤i≤n1 \le i \le n. Note that the given string cc is not the initial string representation of the tree.

The ii-th of the next nn lines contains two integers lil_i and rir_i (0≤li,ri≤n0 \le l_i, r_i \le n). If the node ii does not have the left child, li=0l_i = 0, and if the node ii does not have the right child, ri=0r_i = 0.

It is guaranteed that the given input forms a binary tree, rooted at 11.

第一行包含两个整数 nn 和 kk(1≤k≤n≤2⋅1051 \le k \le n \le 2 \cdot 10^5)。

第二行包含一个长度为 nn 的字符串 cc,由小写英文字母组成,其中 cic_i 表示节点 ii 的初始标签(1≤i≤n1 \le i \le n)。注意:所给字符串 cc 并非该树的初始字符串表示。

接下来的 nn 行中,第 ii 行包含两个整数 lil_i 和 rir_i(0≤li,ri≤n0 \le l_i, r_i \le n)。若节点 ii 无左子节点,则 li=0l_i = 0;若节点 ii 无右子节点,则 ri=0r_i = 0。

保证所给输入构成一棵以节点 11 为根的二叉树。

输出格式

Print a single line, containing the lexicographically smallest string representation of the tree if at most kk nodes have their labels duplicated.

输出一行,包含在至多 kk 个节点的标签被重复的情况下,字典序最小的树的字符串表示。

输入输出样例

  • 输入#1

    4 3
    abab
    2 3
    0 0
    0 4
    0 0

    输出#1

    baaaab
  • 输入#2

    8 2
    kadracyn
    2 5
    3 4
    0 0
    0 0
    6 8
    0 7
    0 0
    0 0

    输出#2

    daarkkcyan
  • 输入#3

    8 3
    kdaracyn
    2 5
    0 3
    0 4
    0 0
    6 8
    0 7
    0 0
    0 0

    输出#3

    darkcyan

说明/提示

The images below present the tree for the examples. The number in each node is the node number, while the subscripted letter is its label. To the right is the string representation of the tree, with each letter having the same color as the corresponding node.

Here is the tree for the first example. Here we duplicated the labels of nodes 11 and 33. We should not duplicate the label of node 22 because it would give us the string "bbaaab", which is lexicographically greater than "baaaab".

In the second example, we can duplicate the labels of nodes 11 and 22. Note that only duplicating the label of the root will produce a worse result than the initial string.

In the third example, we should not duplicate any character at all. Even though we would want to duplicate the label of the node 33, by duplicating it we must also duplicate the label of the node 22, which produces a worse result.

There is no way to produce string "darkkcyan" from a tree with the initial string representation "darkcyan" :(.

下方图片展示了示例中的树结构。每个节点中的数字表示节点编号,下标字母表示该节点的标签。右侧为该树的字符串表示形式,其中每个字母的颜色与其对应节点的颜色相同。

以下是第一个示例对应的树。此处我们复制了节点 11 和节点 33 的标签。我们不应复制节点 22 的标签,因为那样会得到字符串 "bbaaab",其字典序大于 "baaaab"。

在第二个示例中,我们可以复制节点 11 和节点 22 的标签。注意:仅复制根节点的标签将导致结果比初始字符串更差。

在第三个示例中,我们完全不应复制任何字符。尽管我们可能希望复制节点 33 的标签,但一旦复制它,就不得不同时复制节点 22 的标签,从而导致更差的结果。

无法从初始字符串表示为 "darkcyan" 的树中生成字符串 "darkkcyan" :(.

输入解题思路,AI测评打分。不知道怎么写?

首页