Source:

PAT A1122 Hamiltonian Cycle (25 分)

Description:

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 Mlines 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 Klines 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

Keys:

Code:

 /*
Data: 2019-06-20 16:30:23
Problem: PAT_A1122#Hamiltonian Cycle
AC: 12:29 题目大意:
H圈定义:简单圈且包含全部顶点;
判断所给圈是否为H圈
*/
#include<cstdio>
#include<set>
#include<algorithm>
using namespace std;
const int M=;
int grap[M][M],path[M]; int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE fill(grap[],grap[]+M*M,);
int n,m,k,v1,v2;
scanf("%d%d", &n,&m);
for(int i=; i<m; i++)
{
scanf("%d%d", &v1,&v2);
grap[v1][v2]=;
grap[v2][v1]=;
}
scanf("%d", &m);
while(m--)
{
set<int> ver;
scanf("%d", &k);
for(int i=; i<k; i++){
scanf("%d", &path[i]);
ver.insert(path[i]);
}
int reach=;
for(int i=; i<k-; i++){
if(grap[path[i]][path[i+]]==){
reach=;break;
}
}
if(reach== || path[]!=path[k-] || ver.size()!=n || k!=n+)
printf("NO\n");
else
printf("YES\n");
} return ;
}

PAT_A1122#Hamiltonian Cycle的更多相关文章

  1. PAT1122: Hamiltonian Cycle

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

  2. A1122. Hamiltonian Cycle

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

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

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

  4. 1122 Hamiltonian Cycle (25 分)

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

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

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

  6. hihoCoder-1087 Hamiltonian Cycle (记忆化搜索)

    描述 Given a directed graph containing n vertice (numbered from 1 to n) and m edges. Can you tell us h ...

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

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

  8. PAT 1122 Hamiltonian Cycle

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

  9. 1122. Hamiltonian Cycle (25)

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

随机推荐

  1. android NDK开发在本地C/C++源码中设置断点单步调试具体教程

    近期在学android NDK开发,折腾了一天,最终可以成功在ADT中设置断点单步调试本地C/C++源码了.网上关于这方面的资料太少了,并且大都不全,并且调试过程中会出现各种各样的问题,真是非常磨人. ...

  2. oracle中使用impdp数据泵导入数据提示“ORA-31684:对象类型已经存在”错误的解决

    转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/47448751 本文出自[我是干勾鱼的博客] oracle中使用impdp数据泵导 ...

  3. servlet中的中文乱码问题

    老师总会说道:学完这个知识点,我们来谈谈中文乱码问题. 乱码的问题总是无处不在,处理不好会给用户带极差的用户体验. 那么我们来记录一下servlet中的乱码问题吧! 1.服务器向客户端响应时出现的乱码 ...

  4. Privoxy shadowscocks代理

    ubuntu已经启动好了sock5的代理, 代理为: 127.0.0.1:1080. #使用Privoxy将sock5代理映射为http代理. 安装Privoxy sudo apt-get updat ...

  5. 电脑升级win10后visio的问题

    上周由于电脑意外蓝屏,系统从win7升级到了win10,昨天工作写文档时才发现缺少画图的工具,于是按照了visio2013,在编辑设计图时发现,一旦用visio打开或编辑图后再到word里设计图的内容 ...

  6. 使用filezella服务器配置ftp

    使用FileZilla配置FTP站点,可参考以下步骤: 1.打开Filezilla Server服务端: 点击[Edit]->[Users],或者点击如下图标新增用户. 2.添加FTP帐号后,设 ...

  7. golang单点推送

    package main import ( "encoding/json" "flag" "fmt" "log" &qu ...

  8. POJ 3635 优先队列BFS

    (感谢lyd学长的幻灯片) 注意vis数组的应用 在vis[i][j]中 i表示到了第i个点 j表示还剩j升油 vis[i][j]表示最小话费. 这样只需搜到话费比它少的更新入堆就OK了 //By: ...

  9. java.util.Date

    package com.etc.usual; import java.util.Calendar; import java.util.Date; /** * * @author Administrat ...

  10. String和八种基本数据类型互相转换

    //String转换为对应的八种基本数据类型 String str="100"; //Value out of range. Value:"200" Radix ...