Turning a Tree

No attempts yetTime limit1sMemory limit1024 MB

Problem

You are given a tree whose nodes are numbered $1 \dots N$. Node $1$ is the root, and for every node the ordered list of its children (from left to right) is given.

Lift a leaf $K$ of this tree so that it becomes the new root, keeping every edge intact — in particular, keeping the relative rotational order of the edges around each node unchanged. Output the resulting tree.

For example, starting from the tree on the left below and making the leaf $3$ the new root, we obtain the tree in the middle. The tree on the right would be wrong: around node $1$ the neighbours, read counter-clockwise, are $2, 3, 4$ in the original tree, but $2, 4, 3$ there.

    1      3       3
   /|\     |       |
  2 3 4    1       1
          / \     / \
         4   2   2   4

Here a tree is a connected acyclic graph and node $1$ is its root; see Tree (data structure).

Input

The first line contains two integers: the number of nodes $N$ ($1 \le N \le 10,000$) and the index $K$ of the leaf that becomes the new root ($1 \le K \le N$).

Each of the next $N$ lines describes one node of the original tree. The $(i+1)$-th line begins with $m_i$, the number of children of node $i$, followed by the indices of those $m_i$ children listed from left to right.

Output

Print exactly $N$ lines describing the new tree, using the same per-node format as the input. The $i$-th line begins with the number of children of node $i$ in the new tree, followed by those children listed from left to right.

Hint

Take the first example, where leaf $3$ of the star (node $1$ with children $2, 3, 4$) becomes the new root:

  1. Node $1$ now has $2$ children: nodes $4$ and $2$, in this order.
  2. Node $2$ has no children.
  3. Node $3$ has $1$ child: node $1$.
  4. Node $4$ has no children.