luogu#P12114. [NWRRC2024] Just Half is Enough
[NWRRC2024] Just Half is Enough
题目描述
Jacob is studying graph theory. Today he learned that a of a directed graph is a linear ordering of its vertices such that for every directed edge from vertex to vertex , comes before~ 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 : a linear ordering of the graph's vertices such that of all directed edges in the graph, comes before in the ordering.
In other words, if the graph has edges, and for a particular ordering, of them satisfy the condition above, then the ordering is called if .
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 (). The description of the test cases follows.
The first line of each test case contains two integers and , denoting the number of vertices and the number of edges in the graph (; ).
The -th of the following lines contains two integers and , describing an edge from vertex to vertex (; ). The graph does not contain multiple edges: each directed edge appears at most once. However, having both edges and is allowed.
It is guaranteed that the sum of over all test cases does not exceed , and the sum of over all test cases does not exceed .
输出格式
For each test case, print a single integer if the required half-topological ordering does not exist.
Otherwise, print distinct integers , describing the ordering of the given graph (). For at least of the edges , integer must come before integer 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