#P6086. 【模板】Prüfer(Prufer) 序列

【模板】Prüfer(Prufer) 序列

Description

Please implement the conversion between a Prüfer sequence and an unrooted tree.

To make implementation easier, although the tree is unrooted, when reading the input we still treat nn as its root.

For an unrooted tree, let f1n1f_{1 \dots n-1} be its parent sequence (fif_i denotes the parent of node ii when rooting the tree at nn), and let p1n2p_{1 \dots n-2} be its Prüfer sequence.

In addition, for a sequence a1ma_{1 \dots m} of length mm, define its value as xori=1mi×ai\operatorname{xor}_{i = 1}^m i \times a_i.

Input Format

The first line contains two integers n,mn, m, representing the number of nodes in the tree and the conversion type.

If m=1m = 1, the second line contains n1n - 1 integers, representing the parent sequence.
If m=2m = 2, the second line contains n2n - 2 integers, representing the Prüfer sequence.

Output Format

If m=1m = 1, output one integer on a single line, representing the value of the Prüfer sequence corresponding to the given parent sequence.
If m=2m = 2, output one integer on a single line, representing the value of the parent sequence corresponding to the given Prüfer sequence.

6 1
3 6 4 6 1
29
6 2
4 6 5 2
4

Hint

[Sample 1 Explanation]

p={6 1 3 4}p = \{6\ 1\ 3\ 4\}.

[Sample 2 Explanation]

f={4 6 6 5 2}f = \{4\ 6\ 6\ 5\ 2\}.


[Constraints]

Test Point ID 2n2 \le n \le m=m =
11 10310^3 11
22 10510^5
33
44 5×1065 \times 10^6
55
66 10310^3 22
77 10510^5
88
99 5×1065 \times 10^6
1010

Translated by ChatGPT 5