Pointers

시간 제한3초메모리 제한2048 MB

요약
각 노드가 이웃을 가리키는 포인터를 순환시키며 이동할 때, 무한히 반복되는 (현재 노드, 포인터 배열) 상태를 하나 출력한다.
난이도

어려움10점 중 8점

유형
그래프, 시뮬레이션, 구현, 정수론
정답자
아직 제출이 없습니다

문제

You are given a connected undirected graph with nn nodes and mm edges. Each node uu has an ordered list l_ul\_u of its neighbors, and an arrow pointing to one of its neighbors p_up\_u. Initially, p_up\_u is the first neighbor in l_ul\_u.

You start at node ss, and repeat the following process infinitely many times:

  1. Let vv be the node at which you are currently located. Move from vv to p_vp\_v.
  2. Increment p_vp\_v to the next neighbor in l_vl\_v cyclically.

See the sample notes for an example of this process.

Consider the list p_1,p_2,⋯p_np\_1, p\_2, \cdots p\_n over the course of this process, as well as the current node cc. We call this a "state".

Print any state that appears an infinite amount of times.

입력

The first line of the input contains a single integer tt (1≤t≤1041 \le t \le 10^4) --- the number of test cases. The description of the test cases follows.

The first line of each test case contains three integers nn, mm, and kk (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5, n−1≤m≤4⋅105n - 1 \le m \le 4 \cdot 10^5, 1≤s≤n1 \le s \le n) --- the number of vertices and edges in the graph, and the starting node, respectively.

The uu-th of the next nn lines describes the ordered list l_ul\_u of neighbors of uu. It begins with an integer k_uk\_u (1≤k_u<n1 \le k\_u < n) --- the number of neighbors of uu. This is followed by k_uk\_u distinct integers v_1,v_2,⋯v_k_uv\_1, v\_2, \cdots v\_{k\_u} (1≤v_i≤n1 \le v\_i \le n, v_i≠uv\_i \ne u) --- the neighbors of uu.

It is guaranteed that if vv is a neighbor of uu, then uu is a neighbor of vv. It is also guaranteed that there are mm undirected edges in total.

Across all test cases, it is guaranteed that the sum of nn is at most 2⋅1052 \cdot 10^5, and the sum of mm is at most 4⋅1054 \cdot 10^5.

출력

For each test case print any state that repeats infinitely in the format cc p_1p\_1 p_2p\_2 ...... p_np\_n.

힌트

Let's visualize the third sample case. The red node represents your current position, and the arrow pointing out from each node uu points at node p_up\_u. Here is how the graph looks at the start of the process, and after each of the next 88 steps:

We can see that after 88 operations have been performed, we have reached the initial state p_1=4,p_2=1,p_3=2,p_4=3p\_1 = 4, p\_2 = 1, p\_3 = 2, p\_4 = 3 once again, and our current location (node 11) is the same as it was at the beginning. Therefore, a valid answer is to simply print the initial state.

예제1

  1. 예제 1

    입력
    3
    4 3 2
    1 4
    1 3
    2 4 2
    2 3 1
    4 3 3
    1 4
    1 3
    2 4 2
    2 3 1
    4 4 1
    2 4 2
    2 1 3
    2 2 4
    2 3 1
    
    예상 출력
    3 4 3 2 1
    3 4 3 2 1
    1 4 1 2 3