#P15750. [JAG 2024 Summer Camp #3] Coins on a Tree
[JAG 2024 Summer Camp #3] Coins on a Tree
题目描述
There is an undirected tree with vertices numbered through to . There is a coin on each vertex, and a lowercase English letter is written on each coin.
You are playing a game to collect all the coins. You have a string which is initially empty. You first visit the vertex and then repeatedly move to an adjacent vertex. You may visit each vertex any number of times. Whenever you visit a vertex where a coin is still put, you collect the coin and append the letter on the coin to the end of . When there is already no coin on the node you visit, you do nothing. Note that the coin on the vertex is collected first.
After collecting all the coins, you have a string of length . What is the lexicographically smallest possible string that can become?
输入格式
The input consists of a single test case of the following format.
$$\begin{aligned} &N \\ &p_2 \ p_3 \ \ldots \ p_N \\ &c_1c_2 \ldots c_N \end{aligned} $$The first line consists of an integer (), which is the number of vertices on the tree.
The second line consists of integers. Each (, ) represents that the vertices and are adjacent. Note that is not given.
The third line consists of a string of lowercase English letters. The -th letter () is the letter on the coin on the vertex .
输出格式
Print the lexicographically smallest possible string that can become after collecting all the coins.
7
1 1 2 2 3 3
abbabac
abababc
8
1 1 3 4 3 1 7
icpcicpc
icpccipc