Just Half is Enough

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

요약
방향 그래프가 주어질 때, 간선의 절반 이상에서 u가 v보다 앞서도록 정점을 나열하고, 그런 순서가 없으면 -1을 출력한다.
난이도

보통10점 중 4점

유형
그래프, 정렬, 그리디, 구현
정답자
아직 제출이 없습니다

문제

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)(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: a linear ordering of the graph's vertices such that 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 if k≥⌈m2⌉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 tt (1≤t≤1041 \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 (2≤n≤1052 \le n \le 10^5; 1≤m≤2⋅1051 \le m \le 2 \cdot 10^5).

The ii-th of the following mm lines contains two integers u_iu\_i and v_iv\_i, describing an edge from vertex u_iu\_i to vertex v_iv\_i (1≤u_i,v_i≤n1 \le u\_i, v\_i \le n; u_i≠v_iu\_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 2⋅1052 \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 p_1,p_2,…,p_np\_1, p\_2, \ldots, p\_n, describing the ordering of the given graph (1≤p_i≤n1 \le p\_i \le n). For at least ⌈m2⌉\lceil \frac{m}{2} \rceil of the edges (u_i,v_i)(u\_i, v\_i), integer u_iu\_i must come before integer v_iv\_i in this list. If there are multiple answers, print any of them.

예제1

  1. 예제 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 2 3
    3 1 5 4 2