#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 NN vertices numbered through 11 to NN. 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 NN coins. You have a string S\mathbf{S} which is initially empty. You first visit the vertex 11 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 S\mathbf{S}. When there is already no coin on the node you visit, you do nothing. Note that the coin on the vertex 11 is collected first.

After collecting all the NN coins, you have a string S\mathbf{S} of length NN. What is the lexicographically smallest possible string that S\mathbf{S} 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 NN (2N200,0002 \leq N \leq 200,000), which is the number of vertices on the tree.

The second line consists of N1N - 1 integers. Each pip_i (2iN2 \leq i \leq N, 1pi<i1 \leq p_i < i) represents that the vertices ii and pip_i are adjacent. Note that p1p_1 is not given.

The third line consists of a string of NN lowercase English letters. The ii-th letter cic_i (1iN1 \leq i \leq N) is the letter on the coin on the vertex ii.

输出格式

Print the lexicographically smallest possible string that S\mathbf{S} can become after collecting all the NN coins.

7
1 1 2 2 3 3
abbabac
abababc
8
1 1 3 4 3 1 7
icpcicpc
icpccipc