問題一覧 > 通常問題

No.2998 Rainbow Christmas Tree

レベル : / AI推定難易度 : 2375 / 実行時間制限 : 1ケース 3.000秒 / メモリ制限 : 512 MB / スペシャルジャッジ問題 (複数の解が存在する可能性があります)
タグ : (AC するまで非表示) / 解いたユーザー数 11
作問者 : ygussany
お気に入りにしたユーザー ProblemId : 6608 / 自分の提出
問題文最終更新日: 2024-12-22 21:45:37
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
Advent Calendar Contest 2024 (順位表) の他の問題:

Story

At Christmas in the Kingdom of Y.Y., there is a custom of treating part of the road network as a Christmas tree and lighting up the entire town in sequence. Every year, Christmas trees of various colors and shapes have been made on the road network. This year, the people want to skillfully combine parts of past Christmas trees to make a rainbow-shining Christmas Tree.

Problem Statement

There are $N$ vertices with distinct numbers from $1$ to $N$. On these vertices, you are given $K$ directed spanning trees rooted at vertex $1$ and $(N - 1 - K)$ directed spanning trees rooted at vertex $N$. Determine whether it is possible to make a directed spanning tree by choosing exactly $1$ edge from each of these $(N - 1)$ directed spanning trees, and if it is possible, show one example.

What is a directed spanning tree? A directed graph that is connected (ignoring edge directions), in which exactly $1$ vertex has no incoming edge and every other vertex has exactly $1$ incoming edge, is called a directed tree. The vertex with no incoming edge in a directed tree is called the root, and a directed tree containing all of the specified vertices is called a directed spanning tree.

Input

$N\ \ K$
$P_{1, 1} \ \ P_{1, 2} \ \cdots \ P_{1,N}$
$\ \ \vdots$
$P_{N-1, 1} \ \ P_{N-1, 2} \ \cdots \ P_{N-1,N}$

  • $2 \le N \le 1000$
  • $0 \le K \le N - 1$
  • $0 \le P_{i,j} \le N$ and $P_{i,j} \neq j$ $(1 \le i \le N - 1,\ 1 \le j \le N)$
  • $P_{i,1} = 0$ $(1 \le i \le K)$
  • $P_{i,N} = 0$ $(K + 1 \le i \le N - 1)$
  • For each $i$ $(1 \le i \le N - 1)$, $P_{i,j}$ $(1 \le j \le N)$ defines the $i$-th directed spanning tree in the following way:
    if $P_{i,j} = 0$, vertex $j$ is the root; otherwise, the unique edge entering vertex $j$ is $P_{i,j} \to j$, which starts at $P_{i,j}$.

Output

If it is impossible to make a directed spanning tree by choosing exactly $1$ edge from each of the $(N - 1)$ directed spanning trees, print No.

If it is possible, print Yes on the $1$st line, and print one example of such a directed spanning tree on the $2$nd line in the following format. If there are multiple correct answers, any of them will be accepted.

$Q_1 \ \ Q_2 \ \cdots \ Q_N$
  • $(Q_1, Q_2, \dots, Q_N)$ is a permutation of $(0, 1, \dots, N - 1)$.
  • If $Q_j \neq 0$, then $P_{Q_j, j} \neq 0$.
  • $Q_j$ $(1 \le j \le N)$ defines a directed spanning tree in the following way:
    if $Q_j = 0$, vertex $j$ is the root; otherwise, the unique edge entering vertex $j$ is $P_{Q_j, j} \to j$, chosen from the $Q_j$-th directed spanning tree.

Sample

Sample 1
Input
3 1
0 1 2
2 3 0
Output
Yes
2 0 1

The $1$st directed spanning tree has the shape $1 \to 2 \to 3$, and the $2$nd directed spanning tree has the shape $1 \leftarrow 2 \leftarrow 3$. By choosing the edge $2 \to 3$ entering vertex $3$ from the $1$st tree and the edge $1 \leftarrow 2$ entering vertex $1$ from the $2$nd tree, you can make a directed spanning tree rooted at vertex $2$, with the shape $1 \leftarrow 2 \to 3$.

Sample 2
Input
7 6
0 1 1 2 6 2 5
0 3 7 1 1 5 1
0 4 1 1 1 4 2
0 1 5 2 1 4 3
0 1 2 1 1 1 6
0 4 6 1 4 4 4
Output
Yes
0 5 4 1 2 3 6

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。