100 atcoder#ABC142F. [ABC142F] Pure

[ABC142F] Pure

Score : 600600 points

Problem Statement

Given is a directed graph GG with NN vertices and MM edges. The vertices are numbered 11 to NN, and the ii-th edge is directed from Vertex AiA_i to Vertex BiB_i. It is guaranteed that the graph contains no self-loops or multiple edges.

Determine whether there exists an induced subgraph (see Notes) of GG such that the in-degree and out-degree of every vertex are both 11. If the answer is yes, show one such subgraph. Here the null graph is not considered as a subgraph.

Notes

For a directed graph G=(V,E)G = (V, E), we call a directed graph G=(V,E)G' = (V', E') satisfying the following conditions an induced subgraph of GG:

  • VV' is a (non-empty) subset of VV.
  • EE' is the set of all the edges in EE that have both endpoints in VV'.

Constraints

  • 1N10001 \leq N \leq 1000
  • 0M20000 \leq M \leq 2000
  • 1Ai,BiN1 \leq A_i,B_i \leq N
  • AiBiA_i \neq B_i
  • All pairs (Ai,Bi)(A_i, B_i) are distinct.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN MM

A1A_1 B1B_1

A2A_2 B2B_2

::

AMA_M BMB_M

Output

If there is no induced subgraph of GG that satisfies the condition, print -1. Otherwise, print an induced subgraph of GG that satisfies the condition, in the following format:

KK

v1v_1

v2v_2

:

vKv_K

This represents the induced subgraph of GG with KK vertices whose vertex set is {v1,v2,,vK}\{v_1, v_2, \ldots, v_K\}. (The order of v1,v2,,vKv_1, v_2, \ldots, v_K does not matter.) If there are multiple subgraphs of GG that satisfy the condition, printing any of them is accepted.

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

The induced subgraph of GG whose vertex set is {1,2,4}\{1, 2, 4\} has the edge set {(1,2),(2,4),(4,1)}\{(1, 2), (2, 4), (4, 1)\}. The in-degree and out-degree of every vertex in this graph are both 11.

4 5
1 2
2 3
2 4
1 4
4 3
-1

There is no induced subgraph of GG that satisfies the condition.

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