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

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

Background

The Prüfer sequence is also written as the Prufer sequence.

Problem 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 f1…n−1f_{1 \dots n-1} be its parent sequence (fif_i denotes the parent of node ii when rooting the tree at nn), and let p1…n−2p_{1 \dots n-2} be its Prüfer sequence.

In addition, for a sequence a1…ma_{1 \dots m} of length mm, define its value as xor⁡i=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 n−1n - 1 integers, representing the parent sequence.
If m=2m = 2, the second line contains n−2n - 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 2≤n≤2 \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