#P9558. [SDCPC 2023] Trie

[SDCPC 2023] Trie

题目描述

Recall the definition of a trie:

  • A trie of size nn is a rooted tree with nn vertices and (n1)(n - 1) edges, where each edge is marked with a character.
  • Each vertex in a trie represents a string. Let s(x)s(x) be the string vertex xx represents.
  • The root of the trie represents an empty string. Let vertex uu be the parent of vertex vv, and let cc be the character marked on the edge connecting vertex uu and vv, we have s(v)=s(u)+cs(v) = s(u) + c. Here ++ indicates string concatenation, not the normal addition operation.
  • The string each vertex represents is distinct.

We now present you a rooted tree with (n+1)(n + 1) vertices. The vertices are numbered 0,1,,n0, 1, \cdots, n and vertex 00 is the root. There are mm key vertices in the tree where vertex kik_i is the ii-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 (n+1)(n + 1). Let's consider the sequence A={s(k1),s(k2),,s(km)}A = \{s(k_1), s(k_2), \cdots, s(k_m)\} consisting of all strings represented by the key vertices. Let B={w1,w2,,wm}B = \{w_1, w_2, \cdots, w_m\} be the string sequence formed by sorting all strings in sequence AA from smallest to largest in lexicographic order. Please find a way to mark the edges so that sequence BB is minimized.

We say a string P=p1p2pxP = p_1p_2\cdots p_x of length xx is lexicographically smaller than a string Q=q1q2qyQ = q_1q_2\cdots q_y of length yy, if

  • x<yx < y and for all 1ix1 \le i \le x we have pi=qip_i = q_i, or
  • there exists an integer 1tmin(x,y)1 \le t \le \min(x, y) such that for all 1i<t1 \le i < t we have pi=qip_i = q_i, and pt<qtp_t < q_t.

We say a string sequence F={f1,f2,,fm}F = \{f_1, f_2, \cdots, f_m\} of length mm is smaller than a string sequence G={g1,g2,,gm}G = \{g_1, g_2, \cdots, g_m\} of length mm, if there exists an integer 1tm1 \le t \le m such that for all 1i<t1 \le i < t we have fi=gif_i = g_i, and ftf_t is lexicographically smaller than gtg_t.

输入格式

There are multiple test cases. The first line of th input contains an integer TT indicating the number of test cases. For each test case:

The first line contains two integers nn and mm (1mn2×1051 \le m \le n \le 2 \times 10^5) indicating the number of vertices other than the root and the number of key vertices.

The second line contains nn integers a1,a2,,ana_1, a_2, \cdots, a_n (0ai<i0 \le a_i < i) where aia_i is the parent of vertex ii. It's guaranteed that each vertex has at most 2626 children.

The third line contains mm integers k1,k2,,kmk_1, k_2, \cdots, k_m (1kin1 \le k_i \le n) where kik_i is the ii-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 nn of all test cases will not exceed 2×1052 \times 10^5.

输出格式

For each test case output one line containing one answer string c1c2cnc_1c_2\cdots c_n consisting of lower-cased English letters, where cic_i is the letter marked on the edge between aia_i and ii. If there are multiple answers strings so that sequence BB is minimized, output the answer string with the smallest lexicographic order.

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 11 is a. The string represented by vertex 44 is aba. The string represented by vertex 33 is aa. The string represented by vertex 55 is abb. So B={B = \{a, aa, aba, abb}\}.