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. nodejs 模板引擎ejs的简单使用(2)

    test.ejs <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...

  2. 使用sqlyog工具同步两个相同结构的数据库之间的数据

    compare two database data 因为工作上遇到 同一个项目被部署到不同服务器上,原项目(后统称"源")在运行中,后部署的项目(后统称"目标" ...

  3. react 使用react-router-dom 在Route对象上component 参数接收的是一个方法而非一个对象

    其实对于jsx语法 一直觉的它有点清晰都不是很好,js和html混在一起有点不伦不类的样子,以下是我在使用react中遇到的一个很奇葩的事情 假定你定义了一个component Mine import ...

  4. html 通过input video canvas 打开摄像头 定制相机

    在机缘巧合之下,了解到用HTML5和javascript调用摄像头来实现拍照功能,今天就把大致原理写下来.页面布局很简单,就是一个input标签,两个HTML5元素video.canvas和一个but ...

  5. 自然数幂和&伯努利数(Bernoulli)

    二项式定理求自然数幂和 由二项式定理展开得 \[ (n+1)^{k+1}-n^{k+1}=\binom {k+1}1n^k+\binom {k+1}2n^{k-1}+\cdots+\binom {k+ ...

  6. swiper-animate

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. 【Problem】xampp in ubuntu下命令行启动mysql报错: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/opt/lampp/var/mysql/mysql.sock' (2)

    xampp in ubuntu下命令行启动mysql报错: reddevil@reddevil-Lenovo:/opt/lampp$ ./bin/mysql -u root -p Enter pass ...

  8. 原生js如何获取某一元素的高度

    三种方法: 1.document.getElementById("id").style.height,这种方法的前提是必须之前已经显示的在css中声明过height,才能取得正确的 ...

  9. php数组长度怎么获取

    我们可以将元素添加到数组或从数组中删除元素,那么如果我们想要知道数组中存在的元素的总长度或总数,我们就可以使用count() 或sizeof函数. 下面我们就通过简单的示例,给大家介绍php获取数组长 ...

  10. NX二次开发-获取UG界面窗口句柄UF_UI_get_default_parent

    1 extern "C" DllExport void ufusr(char *param, int *retcod, int param_len) 2 { 3 4 if (UF_ ...