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), 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 V​1​​ V​2​​ ... V​n​​

where n is the number of vertices in the list, and V​i​​'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
第一,遍历点K一定等于N+1,因为既要遍历且一次所有顶点,而且回到起点
第二,遍历的最后一个点一定是起点
使用visit记录顶点是否遍历过了,graph记录路径是否能行
 #include <iostream>
#include <vector>
using namespace std;
int main()
{
int n, m, k, a, b, start, graph[][];
bool visit[];//记录每个顶点遍历一次
fill(graph[], graph[] + * , -);
cin >> n >> m;
while (m--)
{
cin >> a >> b;
graph[a][b] = graph[b][a] = ;
}
cin >> m;
while (m--)
{
cin >> k;
bool flag = true;
fill(visit, visit + , false);
for (int i = ; i < k; ++i)
{
cin >> b;
if (flag == false || k != n + )//遍历所有的顶点并回到起点,则一定走过n+1个点
{
flag = false;
continue;
}
if (i == )
start = b;//记录起点
else if (graph[a][b] != )//此路不通
flag = false;
else if (i == k - && b != start)//最后一个点不是起点
flag = false;
else if (i != k - && visit[b] != false)//除了最后一次重复遍历起点,出现了其他点重复遍历,
flag = false;
else
visit[b] = true;//遍历过
a = b;//记录前一个点
}
if (flag)
{
for (int i = ; i <= n && flag == true; ++i)
if (visit[i] == false)//存在没有遍历的顶点
flag = false;
if (flag)
cout << "YES" << endl;
}
if (flag == false)
cout << "NO" << endl;
}
return ;
}
 

PAT甲级——A1122 Hamiltonian Cycle【25】的更多相关文章

  1. PAT甲级 1122. Hamiltonian Cycle (25)

    1122. Hamiltonian Cycle (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...

  2. 1122 Hamiltonian Cycle (25 分)

    1122 Hamiltonian Cycle (25 分) The "Hamilton cycle problem" is to find a simple cycle that ...

  3. PAT A1122 Hamiltonian Cycle (25 分)——图遍历

    The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...

  4. A1122. Hamiltonian Cycle

    The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...

  5. 1122. Hamiltonian Cycle (25)

    The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...

  6. 【PAT甲级】1070 Mooncake (25 分)(贪心水中水)

    题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全 ...

  7. PAT甲题题解-1122. Hamiltonian Cycle (25)-判断路径是否是哈密顿回路

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789799.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  8. PAT甲级 1121. Damn Single (25)

    1121. Damn Single (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue "Dam ...

  9. PAT甲级 1126. Eulerian Path (25)

    1126. Eulerian Path (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue In grap ...

随机推荐

  1. 了解linux web的监听工具

    zabbix cacti Nagios 本想安装的,但是安装需要一个 空的服务器,因为服务器已经有安装 LAMP,故没有去了解 尝试了 cacti ,因为本地环境版本问题,只能使用0.8.8a版本,并 ...

  2. js 404页面跳转

    非原创 <script type="text/javascript"> var num = 5; function redirect() { num--; docume ...

  3. delphi xe10 麦克风、摄像头操作

    TakePhotoFromCameraAction1: TTakePhotoFromCameraAction; // 通过手机摄像头获取图片TakePhotoFromLibraryAction1: T ...

  4. delphi JPG转为BMP存入数据库

    delphi  JPG转为BMP存入数据库   必须在uses中引用JPEG procedure TForm1.BitBtn3Click(Sender: TObject);varjpg:TJPEGim ...

  5. 模拟字典序排序——hdu6034

    #include <bits/stdc++.h> #include <iostream> using namespace std; ; ; int n; int maxj; s ...

  6. Linux ifconfig 单网卡配置多网段

      1 2 3 4 5 6 7 8 9 10 11 ifconfig eth0 down ifconfig eth0 hw ether 01:02:03:04:05:06 ifconfig eth0  ...

  7. Java-Class-C:org.springframework.http.HttpEntity

    ylbtech-Java-Class-C:org.springframework.http.HttpEntity 1.返回顶部 1.1. import org.springframework.http ...

  8. Go kit 概览

    该篇为翻译文:原文地址 https://github.com/go-kit/kit Go kit 是一个语言工具包,用于在GO 语言中构建微服务.我们可以解决分布式系统和应用程序架构中的常见问题,因此 ...

  9. java执行顺序之深入理解clinit和init

    原文地址:https://blog.csdn.net/qq_36522306/article/details/80582758 前言: 最近研究了深入理解JVM这本书中的知识,对java中各部分执行的 ...

  10. 转:这里有些sscanf()的一些使用说明,都是从论坛,Blog里整理出来的。供大家使用。

    http://www.cnblogs.com/gmh915/archive/2009/09/30/1576995.html 这里有些sscanf()的一些使用说明,都是从论坛,Blog里整理出来的.供 ...