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;
}
}

1122. 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甲题题解-1122. Hamiltonian Cycle (25)-判断路径是否是哈密顿回路

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

  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[比较一般]

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

  6. PAT 1122 Hamiltonian Cycle

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

  7. 1122 Hamiltonian Cycle

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

  8. PAT1122: Hamiltonian Cycle

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

  9. PAT_A1122#Hamiltonian Cycle

    Source: PAT A1122 Hamiltonian Cycle (25 分) Description: The "Hamilton cycle problem" is to ...

随机推荐

  1. htonl(),htons(),ntohl(),ntons()--大小端模式转换函数

    不同机器内部对变量的字节存储顺序不同,有的采用大端模式(big-endian),有的采用小端模式(little-endian). 大端模式是指高字节数据存放在低地址处,低字节数据放在高地址处. 小端模 ...

  2. leetcode-mid-math-29. Divide Two Integers-NO

    mycode   91.28% class Solution(object): def divide(self, dividend, divisor): """ :typ ...

  3. leetcode-mid-sorting and searching - 33. Search in Rotated Sorted Array

    mycode class Solution(object): def search(self, nums, target): """ :type nums: List[i ...

  4. 配置 setting镜像在nexus私服上下载

    在你的本地仓库上 setting文件中配置,一旦nexus服务关闭是无法下载的 1 配置nexus镜像 <mirror> <id>central1</id> < ...

  5. Unsupervised Image-to-Image Translation Networks

    Abstract: 无监督图像到图像的翻译目的是学习不同域图像的一个联合分布,通过使用来自单独域图像的边缘分布.给定一个边缘分布,可以得到很多种联合分布.如果不加入额外的假设条件的话,从边缘分布无法推 ...

  6. JS数组方法的的返回值和是否改变该数组总结

    concat() 方法 concat() 方法用于连接两个或多个数组. 该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本. 返回值 返回一个新的数组.该数组是通过把所有 arrayX 参数添 ...

  7. oracle ogg 单实例单向简单搭建测试(oracle-oracle)

    昨天突然接到消息说有一个线上的ogg出现了问题,看是否能修复,由于ogg以前玩的少,所以就加急搞了个测试环境,练习了一把 环境 db1,db2(单实例)ip: 1*,1*sid: orcl,ogg1o ...

  8. javascript 访问 webservice

    xml: <?xml version="1.0" encoding="UTF-8"?> <boolean xmlns="http:/ ...

  9. c# AES128 加解密算法

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  10. css随笔记(持续更新)

    /*DIV鼠标穿透*/ div{pointer-events:none;} /*清除IE11默认×*/ input::-ms-clear{display:none;} 使用伪类写边框部分三角 右上角三 ...