#P10770. 「CROI · R2」夏风与树

    ID: 10051 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>贪心博弈论线段树洛谷原创洛谷月赛

「CROI · R2」夏风与树

Description

Alice and Bob are planting trees while playing a game.

Alice owns nodes 1n1\sim n, and Bob owns nodes (n+1)2n(n+1)\sim 2n. The weights of these 2n2n nodes form a permutation aa, where aia_i is the weight of node ii.

First, they agree that node 11 is the root of the tree.

Then, Alice decides the parent for nodes 2n2\sim n, where the parent of node ii must be chosen from 1(i1)1\sim (i-1).

Next, Bob decides the parent for nodes (n+1)2n(n+1)\sim 2n, where the parent of node ii can be chosen from 0(i1)0\sim (i-1). Node 00 is not part of their tree, meaning Bob's nodes do not need to be connected to this tree.

Finally, Alice will perform a depth-first search starting from node 11. During this process, she maintains a sequence: whenever she encounters an unvisited node, she appends its weight to the end of the sequence.

Alice wants the lexicographical order of the final sequence to be as small as possible, while Bob wants it to be as large as possible. Both will adopt optimal strategies. Now Bob asks you to determine what the final sequence will be.

Definitions related to lexicographical order:

  • For a sequence aa of length nn, if i>ni > n, we define ai=a_i = -\infty.
  • For two sequences aa and bb, we say aa is lexicographically smaller than bb if there exists an i1i \ge 1 such that for all 1j<i1 \leq j < i, aj=bja_j = b_j, and ai<bia_i < b_i.

Input Format

In some test cases of this problem, Bob will provide you with the parents of nodes 2n2\sim n determined by Alice in the first step.

The first line contains an integer tt, representing the subtask number. Notably, the sample input has subtask number 00.

The second line contains a positive integer nn.

The third line contains 2n2n positive integers, representing the permutation aa.

The fourth line contains (n1)(n-1) integers. If the subtask has Special Property A, these integers represent the parents of nodes 2n2\sim n. Otherwise, all (n1)(n-1) integers are 00.

Output Format

A single line representing the final sequence.

0
5
10 5 1 8 4 3 7 6 2 9
1 1 1 3
10 1 4 9 7 6 5 8 3 2
0
4
7 2 4 1 5 6 3 8
0 0 0

7 1 8 2 4 6 5 3

0
4
2 7 6 4 5 8 1 3
0 0 0
2 4 8 6 7 5 3

Hint

In Sample #1, a possible final tree is shown (numbers are node IDs, values in parentheses are weights):

Data Range

Subtask Points nn Special Properties
11 1010 4\le 4 None
22 105\le 10^5 B
33 3030 A
44 2020 3000\le 3000 None
55 3030 105\le 10^5

Special Property A: The input provides the parents of nodes 2n2\sim n under one of Alice's optimal strategies.

Special Property B: an+1a2na_{n+1}\sim a_{2n} form a permutation of 1n1\sim n.

For 100%100\% of the data: 1n1051 \le n \le 10^5, and the sequence aa is guaranteed to be a permutation of 12n1\sim 2n.