156. Strange Graph

time limit per test: 0.25 sec.
memory limit per test: 65536 KB
input: standard input
output: standard output

Let us consider an undirected graph G = <V, E>. We say that two vertices u and v are neighbours if (u, v) in E. In this case we also say that u is a neighbour of v and v is a neighbour of u. Let us denote by N(v) the set of neighbours of v. Recall that the number of neighbours of v is called the degree of this vertex and is denoted by deg v.
We call graph G strange if it is connected and for its every vertex v the following conditions are satisfied:
  1. deg v >= 2 (i.e. there are at least two neighbours of v)
  2. if deg v = 2 then the two neighbours of v are not connected by an edge
  3. if deg v > 2 then there exists u in N(v), such that the following is true:
    (a) deg u = 2
    (b) any two diRerent vertices w1, w2 in N(v) \ {u} are neighbours, i.e. (w1,w2) in E
You are given some strange graph G. Find a hamiltonian cycle in it, i.e. a cycle that goes through every vertex of G exactly once.

The first line of the input file contains two integer numbers N and M - the number of vertices and edges in G respectively (3 <= N <= 10000, M <= 100000). 2M integer numbers follow - each pair represents vertices connected by the corresponding edge (vertices are numbered from 1 to N). It is guaranteed that each edge occurs exactly once in the input file and that there are no loops (i.e. ends of each edge are distinct).

If there is no hamiltonian cycle in G, print -1 in the first line of the output file. In the other case output N numbers - the sequence of vertices of G as they appear in the hamiltonian cycle found (note that the last vertex must be connected to the first one). If there are several solutions, output any one.

Sample test(s)

Sample input #1
4 4
1 2 2 3 3 4 4 1

Sample input #2
9 12
1 2 2 3 3 1 1 4 2 5 3 6
4 7 5 8 6 9 7 8 8 9 9 7

Sample output #1
1 2 3 4

Sample output #2


Author:Andrew Stankevich
Resource:ACM ICPC 2002-2003 NEERC, Northern Subregion
Date:November, 2002