编程介的小学生 2017-03-22 06:05 采纳率: 20.5%
浏览 735
已采纳

Strange Graph

Let us consider an undirected graph G = . Let us denote by N(v) the set of vertices connected to vertex v (i.e. the set of neighbours of v). Recall that the number of vertices connected to v is called the degree of this vertex and is denoted by deg v.

We will 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 vertices connected to 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 is u N(v), such that the following is true: (a) deg u = 2 (b) Any two different vertices w1, w2 N(v) \ {u} are connected, i.e. ( w1, w2 ) E.

You are given some strange graph G. Find hamiltonian cycle in it, i.e. find such cycle that it goes through every vertex of G exactly once.

Input

Input consists of multiple test cases.
For each case, the first line contains two integer numbers N and M -- the number of vertices and edges in G respectively (3 N 10 000, M 100 000). 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).
Output

For each test case, if there is no hamiltonian cycle in G, print in one line -1, otherwise 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 Input

4 4
1 2 2 3 3 4 4 1
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
Possible Output for Sample Input

1 2 3 4
-1

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-03-29 15:06
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值