시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 512 MB0000.000%

문제

Let G be a simple undirected graph having no self-loops or multiple edges. For two vertices x and y of G, a path from x to y in G is a sequence <v1, v2, … , v> of distinct vertices of G such that v1 = x, v = y, and vi is adjacent to vi+1 for all i ∈ {1, … , − 1}. If ≥ 3 and v is adjacent to v1, the path is called a cycle. In a graph in which a weight of +1 or −1 is assigned to each edge, the weight product of the edges in a cycle will be +1 or −1. If the weight product is −1, the cycle is called a negative cycle; it is called a positive cycle otherwise. Refer to the graphs shown below for illustrative examples. The left graph contains negative cycles <v1, v2, v3> and <v1, v3, v4> and also positive cycle <v1, v2, v3, v4>, whereas the right graph contains no negative cycle, but positive cycles <v1, v2, v3, v4>, <v1v4, v5>, and <v1, v2, v3, v4, v5>.

Write a computer program that determines the existence of a negative cycle in the graph given as an input and report an arbitrary negative cycle, if any.

입력

Your program is to read from standard input. The first line contains two positive integers n and m, respectively indicating the numbers of vertices and edges of a simple undirected graph, in which we assume n ≤ 20,000 and m ≤ 200,000. The vertices are indexed from 1 to n. In the following m lines, each line contains three integers x, y, and w ∈ {+1, −1}, which represent an edge (x, y) of weight w. The integers given in a single line are always separated by a single space.

출력

Your program is to write to standard output. The first line must contain an integer indicating whether the given graph has a negative cycle. If yes, the integer must be 1; otherwise -1. When and only when the first line is 1, it must be followed by the description of an arbitrary negative cycle of the input graph. A cycle is described by a single line containing an integer , representing its length, followed by lines containing, one by one, the vertices encountered when we traverse the cycle starting from an arbitrary vertex. Note that the vertices of the cycle must be distinct.

예제 입력 1

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

예제 출력 1

1
3
1
2
3

예제 입력 2

5 6
1 4 +1
4 5 -1
5 1 -1
1 2 -1
2 3 +1
3 4 -1

예제 출력 2

-1

예제 입력 3

6 4
1 2 -1
2 3 +1
4 5 +1
5 6 -1

예제 출력 3

-1