1122. Hamiltonian Cycle (25)

时间限制
300 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

————————————————————————————————

题目的意思是给出n个点m条边,对于每次查询判断是否是哈密顿路径

思路:根据查询,分别判断点数,首尾是否一致,是否每个点都遍历,路径是否联通

若均符合则YES否则NO

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std; #define LL long long
const int INF = 0x3f3f3f3f; int mp[500][500];
int a[500];
int main()
{
int n,m,x,y,q,k;
scanf("%d%d",&n,&m);
memset(mp,0,sizeof mp);
for(int i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
mp[x][y]=mp[y][x]=1;
}
scanf("%d",&q);
while(q--)
{
scanf("%d",&k);
for(int i=0;i<k;i++)
scanf("%d",&a[i]);
if(n==1&&k==1)
{
printf("YES\n");
continue;
}
if(k!=n+1||a[0]!=a[k-1])
{
printf("NO\n");
continue;
}
int flag=0;
set<int>s;
s.insert(a[0]);
for(int i=1;i<k;i++)
{
if(mp[a[i]][a[i-1]]==0)
{
flag=1;
break;
}
s.insert(a[i]);
}
if(s.size()!=n)
flag=1;
printf("%s\n",flag==0?"YES":"NO"); }
return 0;
}

  

PAT甲级 1122. Hamiltonian Cycle (25)的更多相关文章

  1. 1122 Hamiltonian Cycle (25 分)

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

  2. PAT甲级——A1122 Hamiltonian Cycle【25】

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

  3. 1122. Hamiltonian Cycle (25)

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

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

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

  5. 1122 Hamiltonian Cycle (25 分)

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

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

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

  7. PAT 1122 Hamiltonian Cycle

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

  8. 【PAT甲级】1070 Mooncake (25 分)(贪心水中水)

    题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全 ...

  9. PAT甲级 1121. Damn Single (25)

    1121. Damn Single (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue "Dam ...

随机推荐

  1. android studio 3.0 集成ijkplayer

    一.ijkplayer编译过程略,有兴趣的朋友可以再研究,以下以编译好的版本讲解. 将ijkplayer相关的so及aar文件复制到app下的libs目录,为支持多版本的手机使用,将所有的so文件都复 ...

  2. yum 安装 php7 ,lamp ,以及安装swoole

    1 更新yum yum update 2 安装依赖 yum -y install pcre* openssl* yum -y install gcc make gd-devel libjpeg-dev ...

  3. BZOJ4326或洛谷2680 运输计划

    BZOJ原题链接 洛谷原题链接 用\(LCA\)初始化出所有运输计划的原始时间,因为答案有单调性,所以二分答案,然后考虑检验答案. 很容易想到将所有超出当前二分的答案的运输计划所经过的路径标记,在这些 ...

  4. Linux vps服务器国产面板wdcp的安装和使用方法

    对于大多数站长来说,稳定的服务器不可或缺,这是我做网站以来的深刻体会,因为之前我在网站运营方面因这个原因吃了很多亏.在这里说出,只希望朋友们不要像我一样.在网站优化过程中,服务器因素导致排名下滑,甚至 ...

  5. Carbon document

    <   Getting Started Docs Reference History Contribute Github Introduction The Carbon class is inh ...

  6. AUC和ROC

    https://www.cnblogs.com/gatherstars/p/6084696.html

  7. 基于tomcat的solr环境搭建(Linux)

    ♥♥  solr是基于lucene的一个全文检索服务器,提供了一些类似webservice的API接口,用户可以通过http请求solr服务器,进行索引的建立和索引的搜索.索引建立的过程:用户提交的文 ...

  8. javase高级技术 - 泛型

    在写案例之前,先简单回顾下泛型的知识 我们知道,java属于强变量语言,使用变量之前要定义,并且定义一个变量时必须要指明它的数据类型,什么样的数据类型赋给什么样的值. 所谓“泛型”,就是“宽泛的数据类 ...

  9. [C#]RichTextBox实现拖放

    amespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeC ...

  10. springMVC 学习 五 参数传递(包括restful风格)

    (一)SpringMVC Controller接受参数的方式 (1) 前端传递的参数,在springMVC的controller中使用基本数据类型或者String 类型进行接受 在前端有一个form表 ...