This page is still under construction.

Parts of this page are still being built. What you see may change.

Odd Cycle in a Directed Graph

Time limit3sMemory limit256 MB

Summary
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.
Level

Medium6 of 10

Topics
Graph, BFS, DFS
Solved
No attempts yet

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 GG be a simple directed graph with no self loops and no duplicate edges. For vertices vv and ww of GG, a path from vv to ww is a sequence of vertices (u1,u2,…,ul)(u_1, u_2, \dots, u_l) where the uiu_i are pairwise distinct, u1=vu_1 = v, ul=wu_l = w, and for every ii with 1≤i<l1 \le i < l there is a directed edge from uiu_i to ui+1u_{i+1}. If l≥2l \ge 2 and there is also a directed edge from ulu_l to u1u_1, the path is a cycle and ll 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 TT. (1≤T≤201 \le T \le 20)

The first line of each test case contains the number of vertices NN and the number of edges MM, separated by a space. (1≤N≤100 0001 \le N \le 100\,000, 0≤M≤1 000 0000 \le M \le 1\,000\,000) Vertices are numbered from 11 to NN.

Each of the next MM lines contains one edge in the form v w, meaning there is a directed edge from vertex vv to vertex ww. The graph is a simple directed graph, so v≠wv \ne w and no edge is given twice. An edge from vv to ww and an edge from ww to vv may both appear.

The sum of NN over all test cases is at most 100 000100\,000, and the sum of MM over all test cases is at most 1 000 0001\,000\,000.

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.

Examples2

  1. Example 1

    Input
    4
    3 3
    2 3
    2 1
    1 3
    3 4
    2 3
    3 2
    1 2
    1 3
    3 4
    2 1
    2 3
    1 3
    3 2
    8 9
    1 2
    2 3
    3 4
    4 1
    5 6
    6 7
    7 8
    8 5
    5 8
    
    Expected output
    -1
    -1
    1
    1
    -1
    
  2. Example 2

    Input
    2
    3 3
    1 2
    2 3
    3 1
    2 2
    1 2
    2 1
    
    Expected output
    1
    1
    -1