#P9558. [SDCPC2023] Trie
[SDCPC2023] Trie
题目描述
Recall the definition of a trie:
- A trie of size is a rooted tree with vertices and edges, where each edge is marked with a character.
- Each vertex in a trie represents a string. Let be the string vertex represents.
- The root of the trie represents an empty string. Let vertex be the parent of vertex , and let be the character marked on the edge connecting vertex and , we have . Here indicates string concatenation, not the normal addition operation.
- The string each vertex represents is distinct.
We now present you a rooted tree with vertices. The vertices are numbered and vertex is the root. There are key vertices in the tree where vertex is the -th key vertex. It's guaranteed that all leaves are key vertices.
Please mark a lower-cased English letter on each edge so that the rooted tree changes into a trie of size . Let's consider the sequence consisting of all strings represented by the key vertices. Let be the string sequence formed by sorting all strings in sequence from smallest to largest in lexicographic order. Please find a way to mark the edges so that sequence is minimized.
We say a string of length is lexicographically smaller than a string of length , if
- and for all we have , or
- there exists an integer such that for all we have , and .
We say a string sequence of length is smaller than a string sequence of length , if there exists an integer such that for all we have , and is lexicographically smaller than .
输入格式
There are multiple test cases. The first line of th input contains an integer indicating the number of test cases. For each test case:
The first line contains two integers and () indicating the number of vertices other than the root and the number of key vertices.
The second line contains integers () where is the parent of vertex . It's guaranteed that each vertex has at most children.
The third line contains integers () where is the -th key vertex. It's guaranteed that all leaves are key vertices, and all key vertices are distinct.
It's guaranteed that the sum of of all test cases will not exceed .
输出格式
For each test case output one line containing one answer string consisting of lower-cased English letters, where is the letter marked on the edge between and . If there are multiple answers strings so that sequence is minimized, output the answer string with the smallest lexicographic order.
题目大意
【题目描述】
请回忆字典树的定义:
- 一棵大小为 的字典树是一棵有 个节点和 条边的有根树,每一条边都标有一个字符。
- 字典树中的每个节点都代表一个字符串,令 表示节点 代表的字符串。
- 字典树的根代表的是空字符串。设节点 为节点 的父节点,设 表示节点 和 之间的边上标有的字符,则 。这里的 代表字符串连接,而不是普通的加法。
- 所有节点代表的字符串互不相同。
给定一棵有 个节点的有根树,节点编号为 ,其中节点 是根节点。树上共有 个关键节点,其中第 个关键节点的编号为 。保证所有叶子节点都是关键节点。
请为每一条边标上一个小写字母,使得这棵有根树变为一棵大小为 的字典树。考虑所有关键节点代表的字符串构成的序列 ,设 是由序列 中所有字符串按字典序从小到大排序后得到的字符串序列,您需要找到一个标记字母的方案,使得序列 最小。
称长度为 的字符串 的字典序小于长度为 的字符串 ,若
- 且对于所有 有 ,或者
- 存在一个整数 ,对于所有 有 ,且 。
称长度为 的字符串序列 小于长度为 的字符串序列 ,若存在一个整数 ,对于所有 有 ,且 的字典序小于 的字典序。
【输入格式】
有多组测试数据。第一行输入一个整数 表示测试数据组数。对于每组测试数据:
第一行输入两个正整数 和 ()表示除了根节点以外的节点数量和关键节点的数量。
第二行输入 个整数 (),其中 代表节点 的父节点。保证每个节点至多有 个子节点。
第三行输入 个整数 (),其中 代表第 个关键节点的编号。保证所有叶子节点都是关键节点,且没有重复的关键节点。
保证所有测试数据 之和不超过 。
【输出格式】
每组数据输出一行一个由小写字母组成的答案字符串 ,其中 表示节点 到 的边上标有的小写字母。若有多种答案字符串使得字符串序列 最小,请输出字典序最小的答案字符串。
【样例解释】
第一组样例数据的答案如下图所示。
其中,节点 代表的字符串为 a
,节点 代表的字符串为 aba
,节点 代表的字符串为 aa
,节点 代表的字符串为 abb
。因此 a
, aa
, aba
, abb
。
2
5 4
0 1 1 2 2
1 4 3 5
1 1
0
1
abaab
a
提示
The answer of the first sample test case is shown as follows.
The string represented by vertex is a
. The string represented by vertex is aba
. The string represented by vertex is aa
. The string represented by vertex is abb
. So a
, aa
, aba
, abb
.