A14152 | AB Tree
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Kilani and Abd are neighbors for 3000 years, but then the day came and Kilani decided to move to another house. As a farewell gift, Kilani is going to challenge Abd with a problem written by their other neighbor with the same name Abd.
The problem is:
You are given a connected tree rooted at node $1$ .
You should assign a character a or b to every node in the tree so that the total number of a's is equal to $x$ and the total number of b's is equal to $n - x$ .
Let's define a string for each node $v$ of the tree as follows:
- if $v$ is root then the string is just one character assigned to $v$ :
- otherwise, let's take a string defined for the $v$ 's parent $p_v$ and add to the end of it a character assigned to $v$ .
You should assign every node a character in a way that minimizes the number of distinct strings among the strings of all nodes.
The problem is:
You are given a connected tree rooted at node $1$ .
You should assign a character a or b to every node in the tree so that the total number of a's is equal to $x$ and the total number of b's is equal to $n - x$ .
Let's define a string for each node $v$ of the tree as follows:
- if $v$ is root then the string is just one character assigned to $v$ :
- otherwise, let's take a string defined for the $v$ 's parent $p_v$ and add to the end of it a character assigned to $v$ .
You should assign every node a character in a way that minimizes the number of distinct strings among the strings of all nodes.
输入格式
The first line contains two integers $n$ and $x$ ( $1 \leq n \leq 10^5$ ; $0 \leq x \leq n$ ) — the number of vertices in the tree the number of a's.
The second line contains $n - 1$ integers $p_2, p_3, \dots, p_{n}$ ( $1 \leq p_i \leq n$ ; $p_i \neq i$ ), where $p_i$ is the parent of node $i$ .
It is guaranteed that the input describes a connected tree.
The second line contains $n - 1$ integers $p_2, p_3, \dots, p_{n}$ ( $1 \leq p_i \leq n$ ; $p_i \neq i$ ), where $p_i$ is the parent of node $i$ .
It is guaranteed that the input describes a connected tree.
输出格式
In the first line, print the minimum possible total number of distinct strings.
In the second line, print $n$ characters, where all characters are either a or b and the $i$ -th character is the character assigned to the $i$ -th node.
Make sure that the total number of a's is equal to $x$ and the total number of b's is equal to $n - x$ .
If there is more than one answer you can print any of them.
In the second line, print $n$ characters, where all characters are either a or b and the $i$ -th character is the character assigned to the $i$ -th node.
Make sure that the total number of a's is equal to $x$ and the total number of b's is equal to $n - x$ .
If there is more than one answer you can print any of them.
输入输出样例
输入 #1
9 3 1 2 2 4 4 4 3 1
输出 #1
4 aabbbbbba
The tree from the sample is shown below:
The tree after assigning characters to every node (according to the output) is the following:
Strings for all nodes are the following:
- string of node $1$ is: a
- string of node $2$ is: aa
- string of node $3$ is: aab
- string of node $4$ is: aab
- string of node $5$ is: aabb
- string of node $6$ is: aabb
- string of node $7$ is: aabb
- string of node $8$ is: aabb
- string of node $9$ is: aa
The set of unique strings is $\{\text{a}, \text{aa}, \text{aab}, \text{aabb}\}$ , so the number of distinct strings is $4$ .
The tree after assigning characters to every node (according to the output) is the following:
Strings for all nodes are the following:
- string of node $1$ is: a
- string of node $2$ is: aa
- string of node $3$ is: aab
- string of node $4$ is: aab
- string of node $5$ is: aabb
- string of node $6$ is: aabb
- string of node $7$ is: aabb
- string of node $8$ is: aabb
- string of node $9$ is: aa
The set of unique strings is $\{\text{a}, \text{aa}, \text{aab}, \text{aabb}\}$ , so the number of distinct strings is $4$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted