Turning a Tree | 프로그래밍의 벗 PivotOJ
PivotOJ

Turning a Tree

시간 제한: 1000ms메모리 제한: 1024MB출처: EIO 2014-15 openBOJ 7191

문제

You are given a tree with nodes numbered 1N1\dots N, where node 11 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 KK 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 33 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 11 (listed counter-clockwise) are 22, 33, 44 in the original tree, but 22, 44, 33 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 NN (1 ≤ N ≤ 10\,000) and the index KK of the leaf to become the new root (1 ≤ K ≤ N). The following NN lines describe the structure of the original tree. The (i+1)(i+ 1)-th line first contains mim_i, the number of child nodes of the node ii, and then the indices of the mim_i child nodes, listed from left to right.

출력

The output should contain exactly NN lines: the structure of the new tree, in the format used in the input file.

힌트

Explanation of the output lines:

  1. Node 11 has 22 children, nodes 44 and 22 (in this order).
  2. Node 22 has no children.
  3. Node 33 has 11 child, node 11.
  4. Node 44 has no children.

예제

예제 1

입력
4 3
3 2 3 4
0
0
0
출력
2 4 2
0
1 1
0
코드를 제출하려면 로그인하세요.