| 시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
|---|---|---|---|---|---|
| 1 초 | 1024 MB | 22 | 11 | 8 | 44.444% |
You are given a tree with nodes numbered 1ドル\dots N,ドル where node 1ドル$ is the root of the tree and for each node the list of its child nodes is known.1
Find the tree we would get by lifting the leaf $K$ of the original tree to be the new root, but leaving all edges intact, including the relative ordering of the edges at each node.
For example, starting from the tree shown on the left in the figure below and making the leaf 3ドル$ the new root, we would get the tree shown in the middle in the figure. The three shown on the right in the figure would not be correct answer, because the neighbors for the node 1ドル$ (listed counter-clockwise) are 2ドル,ドル 3ドル,ドル 4ドル$ in the original tree, but 2ドル,ドル 4ドル,ドル 3ドル$ in this tree.
1 3 3 /|\ | | 2 3 4 1 1 / \ / \ 4 2 2 4
1See also https://en.wikipedia.org/wiki/Tree_(data_structure)
The first line of input contains the number of nodes $N$ (1ドル ≤ N ≤ 10,000円$) and the index $K$ of the leaf to become the new root (1ドル ≤ K ≤ N$). The following $N$ lines describe the structure of the original tree. The $(i+ 1)$-th line first contains $m_i,ドル the number of child nodes of the node $i,ドル and then the indices of the $m_i$ child nodes, listed from left to right.
The output should contain exactly $N$ lines: the structure of the new tree, in the format used in the input file.
4 3 3 2 3 4 0 0 0
2 4 2 0 1 1 0
Explanation of the output lines: