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. modern php笔记---1、新时代的php

    modern php笔记---1.新时代的php 一.总结 一句话总结: php有Zend Engine 和 Facebook开发的 HipHop Virtual Machine两套引擎 1.php也 ...

  2. leetcode-easy-array-66 .plus one

    mycode 主要在计算商和余数的时候一定要用还没更新的商和余数哦 class Solution(object): def plusOne(self, digits): ""&qu ...

  3. Ubuntu C/C++的编译环境

    Ubuntu缺省情况下,并没有提供C/C++的编译环境,因此还需要手动安装.但是如果单独安装gcc以及g++比较麻烦,幸运的是,Ubuntu提供了一个build-essential软件包.查看该软件包 ...

  4. Mysql基本管理知识

    数据库的启动 [root@node80 ~]# /etc/init.d/mysqld start #mysqld是从安装包拷贝的mysql.server Starting MySQL. SUCCESS ...

  5. C#中winform下利用ArcEngine调用ArcGIS Server发布的服务 AE9.3

    主要使用了AE中的IAGSServerOject接口及IMapServer接口.Private void GetServerTest_Click(object sender, EventArgs e) ...

  6. Django中的自定义过滤器

    一.为什么要自定义Django中的自定义过滤器:Django中提供了很多内置的过滤器和标签,详见链接django官网,主要有以下几个: autoescape(自动转义)block(模板继承)csrf_ ...

  7. hadoop 2.5.2源码编译

    编译过程漫长无比,错误百出,需要耐心耐心!! 1.准备的环境及软件 操作系统:Centos6.4 64位 jdk:jdk-7u80-linux-x64.rpm,不要使用1.8 maven:apache ...

  8. big data env setup

    install Spark on CentOS: https://aodba.com/how-to-install-apache-spark-in-centos-standalone/ https:/ ...

  9. 【Qt开发】几个傻不拉几关于char*和const char*的不兼容问题

    1. string转const char* string s ="abc";constchar* c_s = s.c_str(); 2. const char*转string   ...

  10. Go语言入门篇-基本流程控制

    一.if语句 二.switch语句 三.for语句 四.select语句