#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 as its root.
For an unrooted tree, let be its parent sequence ( denotes the parent of node when rooting the tree at ), and let be its Prüfer sequence.
In addition, for a sequence of length , define its value as .
Input Format
The first line contains two integers , representing the number of nodes in the tree and the conversion type.
If , the second line contains integers, representing the parent sequence.
If , the second line contains integers, representing the Prüfer sequence.
Output Format
If , output one integer on a single line, representing the value of the Prüfer sequence corresponding to the given parent sequence.
If , 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]
.
[Sample 2 Explanation]
.
[Constraints]
| Test Point ID | ||
|---|---|---|
Translated by ChatGPT 5
京公网安备 11011102002149号