#P14998. [Nordic OI 2019] Distance Code

    ID: 16908 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>2019Special Judge树的遍历通信题NordicOI(北欧)

[Nordic OI 2019] Distance Code

题目描述

Your task is to create a program that can be used in two ways: as an encoder and as a decoder.

The encoder is given a tree of nn nodes and it removes all nodes from the tree. On each step, the encoder may remove any leaf from the current tree.

The decoder is given a list of distances between successively removed nodes by the encoder, and it has to reconstruct the original structure of the tree.

The decoder has to create any tree that has the same structure as the original tree (more precisely, it has to be isomorphic to the original tree).

输入格式

The first line has an integer tt that is either 1 (encoder) or 2 (decoder).

The second line has an integer nn: the number of nodes in the tree. The nodes are numbered 1,2,,n1, 2, \ldots, n.

If t=1t = 1, there are then n1n - 1 lines that describe the tree. Each line has two integers aa and bb: there is an edge between nodes aa and bb.

If t=2t = 2, there is only one line that has n1n - 1 integers: the distances between successively removed nodes.

输出格式

If t=1t = 1, the encoder has to print a permutation of numbers 1,2,,n1, 2, \ldots, n: the order in which the nodes are removed from the tree.

If t=2t = 2, the decoder has to print n1n - 1 lines that describe the structure of the tree.

1
3
1 2
2 3
1 3 2
2
3
2 1
2 3
1 3

提示

Subtask 1 (21 points)

  • 2n102 \leq n \leq 10

Subtask 2 (47 points)

  • 2n5002 \leq n \leq 500

Subtask 3 (32 points)

  • 2n1052 \leq n \leq 10^5