Odd Cycle in a Directed Graph
Time limit3sMemory limit256 MB
Given a directed graph, decide whether it contains an odd directed cycle and output the smallest vertex of a strongly connected component that holds one.
Problem
Some problems that are hard on a general undirected graph become solvable in polynomial time once the graph has no odd cycle. To find out whether the same holds for directed graphs, you first have to decide whether a directed graph contains an odd cycle.
Let be a simple directed graph with no self loops and no duplicate edges. For vertices and of , a path from to is a sequence of vertices where the are pairwise distinct, , , and for every with there is a directed edge from to . If and there is also a directed edge from to , the path is a cycle and is its length. A cycle of odd length is an odd cycle.
A strongly connected component is a maximal set of vertices that can all reach each other. Every cycle lies inside a single strongly connected component.
For each test case, decide whether the graph contains an odd cycle. If it does, also report where such a cycle sits by naming one vertex number of the strongly connected component that holds it.

Figure 1. Simple directed graphs
Input
The first line contains the number of test cases . ()
The first line of each test case contains the number of vertices and the number of edges , separated by a space. (, ) Vertices are numbered from to .
Each of the next lines contains one edge in the form v w, meaning there is a directed edge from vertex to vertex . The graph is a simple directed graph, so and no edge is given twice. An edge from to and an edge from to may both appear.
The sum of over all test cases is at most , and the sum of over all test cases is at most .
Output
Print the following for each test case.
If the graph has no odd cycle, print -1 on one line.
If the graph has an odd cycle, print 1 on the first line, then on the next line print the smallest vertex number among the vertices that belong to a strongly connected component containing an odd cycle. If several such components exist, print the smallest vertex number over all of them. That vertex does not have to lie on an odd cycle itself; it only has to belong to a strongly connected component that contains one.