luogu#P12114. [NWRRC2024] Just Half is Enough

[NWRRC2024] Just Half is Enough

题目描述

Jacob is studying graph theory. Today he learned that a topological ordering\textit{topological ordering} of a directed graph is a linear ordering of its vertices such that for every directed edge (u,v)(u, v) from vertex uu to vertex vv, uu comes before~vv in the ordering.

It is well-known that topological orderings exist only for graphs without cycles. But how do we generalize this concept for arbitrary graphs?

Jacob came up with the concept of a half-topological ordering\textit{half-topological ordering}: a linear ordering of the graph's vertices such that for at least half\textbf{for at least half} of all directed edges (u,v)(u, v) in the graph, uu comes before vv in the ordering.

In other words, if the graph has mm edges, and for a particular ordering, kk of them satisfy the condition above, then the ordering is called half-topological\textit{half-topological} if km2k \ge \lceil \frac{m}{2} \rceil.

Help Jacob find any half-topological ordering of the given graph, or report that none exist.

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1t1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains two integers nn and mm, denoting the number of vertices and the number of edges in the graph (2n1052 \le n \le 10^5; 1m21051 \le m \le 2 \cdot 10^5).

The ii-th of the following mm lines contains two integers uiu_i and viv_i, describing an edge from vertex uiu_i to vertex viv_i (1ui,vin1 \le u_i, v_i \le n; uiviu_i \ne v_i). The graph does not contain multiple edges: each directed edge (u,v)(u, v) appears at most once. However, having both edges (u,v)(u, v) and (v,u)(v, u) is allowed.

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5, and the sum of mm over all test cases does not exceed 21052 \cdot 10^5.

输出格式

For each test case, print a single integer 1-1 if the required half-topological ordering does not exist.

Otherwise, print nn distinct integers p1,p2,,pnp_1, p_2, \ldots, p_n, describing the ordering of the given graph (1pin1 \le p_i \le n). For at least m2\lceil \frac{m}{2} \rceil of the edges (ui,vi)(u_i, v_i), integer uiu_i must come before integer viv_i in this list. If there are multiple answers, print any of them.

2
3 3
1 2
2 3
3 1
5 6
4 2
2 1
4 3
1 4
3 2
3 5
1 2 3
3 1 5 4 2