#P5353. 树上后缀排序

树上后缀排序

Problem Description

Given a tree rooted at 11 with nn nodes, it is guaranteed that for every node from 22 to nn, the index of its parent is smaller than its own index.

Each node has a character. The string represented by a node is defined as the string formed by concatenating all characters on the simple path from the current node up to the root.

Please sort these strings in lexicographical order.

In particular, if the strings represented by two nodes are exactly the same, their order is determined by the order of their parents: the one whose parent has a larger rank is considered larger. If they are still the same, then their order is determined by their node indices: the one with the larger index is considered larger.

Input Format

The first line contains a positive integer nn.

The second line contains n1n - 1 integers f2nf_{2 \dots n}, where fif_i denotes the parent of node ii.

The third line contains a string ss of length nn consisting of lowercase letters, where sis_i is the character on node ii.

Output Format

Output one line with nn positive integers. The ii-th integer denotes the node index that represents the string ranked ii-th.

5
1 1 3 2
abbaa
1 5 4 2 3

Hint

For 20%20\% of the testdata, n103n \le 10 ^ 3.

For 50%50\% of the testdata, n105n \le 10 ^ 5.

For 100%100\% of the testdata, 1n5×1051 \le n \le 5 \times 10 ^ 5.

Translated by ChatGPT 5