#P13966. [VKOSHP 2024] Intermediate Verticality

[VKOSHP 2024] Intermediate Verticality

题目描述

Two classical graph algorithms --- depth-first search and breadth-first search --- construct two spanning trees in a graph. The depth-first search is known for producing a tree that has no horizontal\textit{horizontal} edges, which are edges connecting vertices that are not ancestors of each other, while breadth-first search is known for producing a tree that has no vertical\textit{vertical} edges --- edges connecting a vertex to its ancestor in the tree. In this problem, you will need to construct an intermediate spanning tree that has a specified number of horizontal and vertical edges.

Recall that an undirected graph consists of a set of vertices VV and a set of edges EE, where each edge connects two vertices. We will consider connected graphs, where it is possible to reach any vertex from any other vertex via edges. A tree is a connected undirected graph that contains no cycles, and a spanning tree in a graph is a subset of its edges that forms a tree, allowing one to reach any vertex of the graph from any other. Recall two fundamental properties of a tree: in a tree with nn vertices, there are exactly n1n-1 edges, and there is exactly one path between any two vertices in a tree.

We will designate a vertex rr in the graph, which we will call the root\textit{root} of the tree. The vertices that lie on the unique path from vertex xx to vertex rr are called the ancestors\textit{ancestors} of vertex xx, and the first vertex on this path is called the parent\textit{parent} of vertex xx and is denoted as pxp_x. The root has no parent.

If a root and a spanning tree are fixed in the graph, then all edges of the graph can be divided into three types:

  • tree edges\textit{tree edges} --- the edges of the chosen spanning tree;
  • vertical edges\textit{vertical edges} --- the edges not belonging to the tree that connect a vertex to its ancestor;
  • horizontal edges\textit{horizontal edges} --- the remaining edges of the graph.

:::align{center} :::

The verticality\textit{verticality} of the spanning tree in the graph is defined as the number of vertical edges.

You are given a graph with nn vertices and mm edges, the root of the tree rr, and a number hh, 0hmn+10 \le h \le m-n+1. You need to construct a spanning tree of the given graph with root at rr, having a verticality equal to hh, or report that such a tree does not exist.

输入格式

Each test consists of several sets of input data. The first line contains one integer tt --- the number of sets of input data (1t1051 \le t \le 10^5). Following this are descriptions of tt sets of input data.

In the first line of each set of input data, there are four integers nn, mm, rr, and hh --- the number of vertices and edges in the graph, respectively, the index of the root vertex, and the required verticality of the future spanning tree (2n31052 \le n \le 3 \cdot 10^5; n1m3105n - 1 \le m \le 3 \cdot 10^5; 1rn1 \le r \le n; 0hmn+10 \le h \le m - n + 1).

In each of the following mm lines, there are two integers uiu_i, viv_i --- the indices of the vertices connected by an edge in the graph (1ui,vin1 \le u_i, v_i \le n; uiviu_i \ne v_i).

It is guaranteed that all graphs are connected, contain no loops or multiple edges. It is guaranteed that the sum of nn across all input data sets does not exceed 31053 \cdot 10^5. It is guaranteed that the sum of mm across all input data sets does not exceed 31053 \cdot 10^5.

输出格式

For each test case, find the required spanning tree TT and output in a separate line nn integers p1,p2,,pnp_1, p_2, \ldots, p_n, where pip_i is the index of the parent of the ii-th vertex in the tree TT (1pin1 \le p_i \le n). For prp_r, you can output any number from 11 to nn. If a tree TT with the desired properties does not exist, output nn numbers 1-1 instead.

4
5 7 2 0
1 2
2 3
3 4
4 1
3 5
5 4
1 5
5 7 2 1
1 2
2 3
3 4
4 1
3 5
5 4
1 5
5 7 2 2
1 2
2 3
3 4
4 1
3 5
5 4
1 5
5 7 2 3
1 2
2 3
3 4
4 1
3 5
5 4
1 5
2 1 2 3 3
2 1 4 1 1
2 1 5 5 1
2 1 4 1 3