The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a graph. Such a cycle is called a "Hamiltonian cycle".

In this problem, you are supposed to tell if a given cycle is a Hamiltonian cycle.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers N (2< N <= 200), the number of vertices, and M, the number of edges in an undirected graph. Then M lines follow, each describes an edge in the format "Vertex1 Vertex2", where the vertices are numbered from 1 to N. The next line gives a positive integer K which is the number of queries, followed by K lines of queries, each in the format:

n V1 V2 ... Vn

where n is the number of vertices in the list, and Vi's are the vertices on a path.

Output Specification:

For each query, print in a line "YES" if the path does form a Hamiltonian cycle, or "NO" if not.

Sample Input:

6 10
6 2
3 4
1 5
2 5
3 1
4 1
1 6
6 3
1 2
4 5
6
7 5 1 4 3 6 2 5
6 5 1 4 3 6 2
9 6 2 1 6 3 4 5 2 6
4 1 2 5 1
7 6 1 3 4 5 2 6
7 6 1 2 5 4 3 1

Sample Output:

YES
NO
NO
NO
YES
NO

哈密顿圈 所有点只经过一次的回路。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int mp[][];
int n,m,a,b,c,v[],k;
int main()
{
cin>>n>>m;
for(int i = ;i < m;i ++)
{
cin>>a>>b;
mp[a][b] = mp[b][a] = ;
}
cin>>m;
for(int i = ;i < m;i ++)
{
cin>>k;
int flag = ;
if(k != n + )flag = ;
cin>>a;
c = a;
memset(v,,sizeof(v));
v[a] ++;
for(int j = ;j < k;j ++)
{
cin>>b;
if(!mp[a][b])flag = ;
v[b] ++;
if(v[b] == && b != c || v[b] > )flag = ;
a = b;
}
if(b != c)flag = ;
if(flag)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}

最新文章

  1. Object是什么
  2. 进击的Python【第二十一章】
  3. Rabbitmq集群升级方案
  4. a版本冲刺第六天
  5. 数据存储之CoreData
  6. Web Essentials之JavaScript,TypeScript和CoffeeScript
  7. [Chapter 3 Process]Practice 3.3 Discuss three major complications that concurrent processing adds to an operating system.
  8. Entity Framework 关系约束配置
  9. Kafka学习笔记(一):概念介绍
  10. 基于TLS的反调试技术
  11. 高并发编程陷阱之check and set
  12. Tomcat 改变localhost主页,映射到应用地址
  13. 用Maven整合SpringMVC+Spring+Hibernate 框架,实现简单的插入数据库数据功能
  14. Vue.js 基础指令实例讲解(各种数据绑定、表单渲染大总结)——新手入门、高手进阶
  15. MongoDB批量操作及与MySQL效率对比
  16. 【Java学习笔记之十】Java中循环语句foreach使用总结及foreach写法失效的问题
  17. CDQ 分治算法模板
  18. 【转】C语言堆栈入门——堆和栈的区别
  19. android--------WebView实现 Html5 视频标签加载
  20. Robot Framework 教程 (7) - 使用For循环

热门文章

  1. 尚硅谷Docker---6-10、docker的安装
  2. 阶段3 1.Mybatis_11.Mybatis的缓存_4 mybatis一对多实现延迟加载
  3. 【ABAP系列】SAP ABAP 开发中的SMARTFORMS 参数
  4. MathType的配置问题;将word中的公式转换为mathtype格式失败,缺少OMML2MML.XSL
  5. .net core 学习小结之 PostMan报415
  6. iptables添加开放端口
  7. python+selenium下弹窗alter对象处理02
  8. [转帖]同事推荐的的aira2
  9. MySQL-快速入门(7)索引
  10. express 实现我猜你喜欢功能