1122. Hamiltonian Cycle (25)
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;
}
}
最新文章
- Object是什么
- 进击的Python【第二十一章】
- Rabbitmq集群升级方案
- a版本冲刺第六天
- 数据存储之CoreData
- Web Essentials之JavaScript,TypeScript和CoffeeScript
- [Chapter 3 Process]Practice 3.3 Discuss three major complications that concurrent processing adds to an operating system.
- Entity Framework 关系约束配置
- Kafka学习笔记(一):概念介绍
- 基于TLS的反调试技术
- 高并发编程陷阱之check and set
- Tomcat 改变localhost主页,映射到应用地址
- 用Maven整合SpringMVC+Spring+Hibernate 框架,实现简单的插入数据库数据功能
- Vue.js 基础指令实例讲解(各种数据绑定、表单渲染大总结)——新手入门、高手进阶
- MongoDB批量操作及与MySQL效率对比
- 【Java学习笔记之十】Java中循环语句foreach使用总结及foreach写法失效的问题
- CDQ 分治算法模板
- 【转】C语言堆栈入门——堆和栈的区别
- android--------WebView实现 Html5 视频标签加载
- Robot Framework 教程 (7) - 使用For循环
热门文章
- 尚硅谷Docker---6-10、docker的安装
- 阶段3 1.Mybatis_11.Mybatis的缓存_4 mybatis一对多实现延迟加载
- 【ABAP系列】SAP ABAP 开发中的SMARTFORMS 参数
- MathType的配置问题;将word中的公式转换为mathtype格式失败,缺少OMML2MML.XSL
- .net core 学习小结之 PostMan报415
- iptables添加开放端口
- python+selenium下弹窗alter对象处理02
- [转帖]同事推荐的的aira2
- MySQL-快速入门(7)索引
- express 实现我猜你喜欢功能