P26378 [AtCoder] Coins on the Tree
省选
题目描述

Problem Description

Takahashi is trying to perform exactly $ K $ operations using $ M $ coins on a rooted tree with $ N $ vertices.

The tree is given such that the parent of vertex $ i $ is $ p_i $. The vertex with $ p_i = 0 $ is the root.

In each operation,

  • If no coin is placed at the root, a new coin can be placed at the root,

or

  • A coin can be moved from a vertex with a coin to one of its children without a coin.

After $ K $ operations, all $ M $ coins must be placed somewhere on the tree.

Let the vertices where the coins placed at the root (first coin, second coin, ..., $ M $th coin) are $ v_1 $, $ v_2 $, ..., $ v_M $.

Takahashi wants to know the lexicographically smallest sequence among $ v_1 $, $ v_2 $, ..., $ v_M $.

Please find this sequence for Takahashi.

A sequence $ u_1 $, $ u_2 $, ..., $ u_M $ is lexicographically smaller than a sequence $ v_1 $, $ v_2 $, ..., $ v_M $ if there exists $1 \leq i \leq M$ such that $ u_i < v_i $ and for all $ j < i $, $ u_j = v_j $.

If it is not possible to perform exactly $ K $ operations or place all $ M $ coins on the tree, output $ -1 $.

Input Format

The input is given from the standard input in the following format:

$ N $ $ M $ $ K $ $ p_1 $ $ p_2 $ ... $ p_{N-1} $ $ p_N $

Output Format

If it is possible to place all $ M $ coins after $ K $ operations, output:

$ v_1 $ $ v_2 $ ... $ v_{M-1} $ $ v_M $

Otherwise, output $ -1 $.

Sample Input and Output

Sample Input #1

3 2 4
2 0 2

Sample Output #1

1 3

Sample Input #2

3 2 5
2 0 2

Sample Output #2

-1

Notes/Hints

Constraints

  • $1 \leq M \leq N \leq 300$
  • $0 \leq K \leq 10^5$
  • $0 \leq p_i \leq N$
  • There is exactly one $ i $ such that $ p_i = 0 $
  • The graph formed by connecting edges $ (i, p_i) $ is a tree
  • All inputs are integers

Sample Explanation 1

The following operations can create the sequence $\{1, 3\}$, which is the lexicographically smallest possible sequence:
- Place the first coin at vertex $ 2 $
- Move the first coin from vertex $ 2 $ to $ 1 $
- Place the second coin at vertex $ 2 $
- Move the second coin from vertex $ 2 $ to $ 3 $

In the images, the first coin is represented in red and the second coin in blue.

Sample Explanation 2

Since it is not possible to perform 5 operations, output $ -1 $.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
3 2 4 2 0 2
样例输出
1 3