Logo
(追記) (追記ここまで)

32925번 - Just Half is Enough 스페셜 저지다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 2048 MB73393154.386%

문제

Jacob is studying graph theory. Today he learned that a topological ordering of a directed graph is a linear ordering of its vertices such that for every directed edge $(u, v)$ from vertex $u$ to vertex $v,ドル $u$ comes before $v$ 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: a linear ordering of the graph's vertices such that for at least half of all directed edges $(u, v)$ in the graph, $u$ comes before $v$ in the ordering.

In other words, if the graph has $m$ edges, and for a particular ordering, $k$ of them satisfy the condition above, then the ordering is called half-topological if $k \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 $t$ (1ドル \le t \le 10^4$). The description of the test cases follows.

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

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

It is guaranteed that the sum of $n$ over all test cases does not exceed 10ドル^5,ドル and the sum of $m$ over all test cases does not exceed 2ドル \cdot 10^5$.

출력

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

Otherwise, print $n$ distinct integers $p_1, p_2, \ldots, p_n,ドル describing the ordering of the given graph (1ドル \le p_i \le n$). For at least $\lceil \frac{m}{2} \rceil$ of the edges $(u_i, v_i),ドル integer $u_i$ must come before integer $v_i$ in this list. If there are multiple answers, print any of them.

제한

예제 입력 1

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

예제 출력 1

1 2 3
3 1 5 4 2

힌트

출처

ICPC > Regionals > Northern Eurasia > Northwestern Russia Regional Contest > ICPC 2024-2025 Northwestern Russia Regional Contest J번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /