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 V​1 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<vector>
using namespace std;
int main(){
int vn, en, qn;
cin>>vn>>en;
vector<vector<int>> map(vn+1, vector<int>(vn+1, 0));
vector<int> visited(vn+1, 0);
for(int i=0; i<en; i++){
int v1, v2;
cin>>v1>>v2;
map[v1][v2]=map[v2][v1]=1;
}
cin>>qn;
for(int i=0; i<qn; i++){
int n, flag=0;
cin>>n;
vector<int> path(n, 0);
vector<vector<int>> temp=map;
vector<int> visited(vn+1, 0);
for(int j=0; j<n; j++)
cin>>path[j];
if(path[0]!=path[n-1]){
cout<<"NO"<<endl;
continue;
}
for(int j=0; j<n-1; j++)
if(temp[path[j]][path[j+1]]==1){
visited[path[j]]=visited[path[j+1]]=1;
temp[path[j]][path[j+1]]=temp[path[j+1]][path[j]]=0;
}else{
flag=1;
break;
}
for(int j=1; j<=vn; j++)
if(visited[j]!=1)
flag=1;
flag==0?cout<<"YES"<<endl:cout<<"NO"<<endl;
}
return 0;
}

PAT 1122 Hamiltonian Cycle的更多相关文章

  1. PAT 1122 Hamiltonian Cycle[比较一般]

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

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

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

  3. 1122 Hamiltonian Cycle (25 分)

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

  4. 1122 Hamiltonian Cycle (25 分)

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

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

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

  6. 1122. Hamiltonian Cycle (25)

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

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

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

  8. 1122 Hamiltonian Cycle

    题意:包含图中所有结点的简单环称为汉密尔顿环.给出无向图,然后给出k个查询,问每个查询是否是汉密尔顿环. 思路:根据题目可知,我们需要判断一下几个条件:(1).首先保证给定的环相邻两结点是连通的:(2 ...

  9. PAT1122: Hamiltonian Cycle

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

随机推荐

  1. Java多线程 -- 正确使用Volatile变量

    Java 语言中的 volatile 变量可以被看作是一种 “程度较轻的 synchronized”:与 synchronized 块相比,volatile 变量所需的编码较少,并且运行时开销也较少, ...

  2. gitlab https

    https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/install/installation.md#using-https https:// ...

  3. bzoj 1034: [ZJOI2008]泡泡堂BNB【贪心】

    是贪心 先把两个数组排序,然后贪心的选让a数组占优的(如果没有就算输),这是最大值,最小值是2n-贪心选b数组占优 #include<iostream> #include<cstdi ...

  4. bzoj [Usaco2010 Hol]cowpol 奶牛政坛【树链剖分】

    意识流虚树 首先考虑只有一个党派,那么可以O(n)求树的直径,步骤是随便指定一个根然后找距离根最远点,然后再找距离这个最远点最远的点,那么最远点和距离这个最远点最远的点之间的距离就是直径 那么考虑多党 ...

  5. P4097 [HEOI2013]Segment

    传送门 简单来说就是对于每条线段,先把它拆成\(O(logn)\)条,然后对于每一条再\(O(logn)\)判断在所有子区间的优劣程度 //minamoto #include<bits/stdc ...

  6. zabbix详细介绍及其自动动态发现

    zabbix3.2.1 第1章 安装 1.1 查看系统环境 [root@centos7-2 ~]# [root@centos7-2 ~]# hostname -I 10.0.0.10 172.16.1 ...

  7. JavaScript--DOM访问子结点childNodes

    访问子结点childNodes 访问选定元素节点下的所有子节点的列表,返回的值可以看作是一个数组,他具有length属性. 语法: elementNode.childNodes 注意: 如果选定的节点 ...

  8. TFS修改了工作区

    计算机修改名字后,更换了TFS工作区,但原工作区的有些文件忘记签入: 解决方案: 删除原工作区即可,实现:到TFS工作区 - “管理工作区”,选中“显示远程工作区”,找到原工作区,删除即可.

  9. jQuery学习笔记(1)-初探

    一.jQuery是什么 1.jQuery是一套JavaScript脚本库,而不是框架:就好比"System是程序集"是类库,而"ASP.NET MVC"是框架: ...

  10. 42使用NanoPiM1Plus在Android4.4.2下的录音测试

    42使用NanoPiM1Plus在Android4.4.2下的录音测试 大文实验室/大文哥壹捌陆捌零陆捌捌陆捌贰21504965 AT qq.com完成时间:2017/12/5 17:51版本:V1. ...