1122 Hamiltonian Cycle (25 分)

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​​ 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

题目大意:判断给出的路径是否是哈密顿回路,哈密顿回路是一个简单回路,包含图中的每一个点,

我的AC:

#include <iostream>
#include <vector>
#include<cstdio>
#include <map>
using namespace std;
#define inf 9999 int g[][];
int vis[];
int main() {
int n,m,f,t;
cin>>n>>m;
fill(g[],g[]+*,inf);
for(int i=;i<m;i++){
cin>>f>>t;
g[f][t]=;
g[t][f]=;
}
int k,ct;
cin>>k;
while(k--){
fill(vis,vis+,);
cin>>ct;
vector<int> path(ct);
for(int i=;i<ct;i++){
cin>>path[i];
}
if(path[]!=path[ct-]){//首先需要保证两者是相同的。
cout<<"NO\n";continue;
}
bool flag=false;
for(int i=;i<ct-;i++){
if(g[path[i]][path[i+]]==inf){//如果两点之间,没有路径。
cout<<"NO\n";
flag=true;
break;
}
if(vis[path[i+]]==){//如果重复访问那么就不是简单路径,
cout<<"NO\n";
flag=true;break;
}
vis[path[i+]]=;
// cout<<path[i+1]<<'\n';
}
if(!flag){//这里还需要判断是否是所有的点都已经访问过。
bool fg=false;
for(int i=;i<=n;i++){//这里是从1开始判断啊喂!!!
if(vis[i]==){
cout<<"NO\n";
fg=true;break;
}
}
if(!fg)cout<<"YES\n";
}
}
return ;
}

//本来很简单的一道题,两个周没打算法代码了,生疏了。

1.点标号是从1开始的所以 最后判断所有的点是否被遍历过,是从1开始循环的,

2.比较简单,就是几个判断情况,使用邻接矩阵存储图,不是邻接表。

PAT 1122 Hamiltonian Cycle[比较一般]的更多相关文章

  1. PAT 1122 Hamiltonian Cycle

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

  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. 关于Unity中的新手编码技巧

    写代码遇到报错,问题怎么办?怎么查看unity代码的接口?函数参数不记得了怎么办? 解决方法: 1.选择不懂的函数或类,按F12,跳转到代码的定义,自己去看就可知道了. 2.有的时候,选择一个函数,按 ...

  2. Spring Boot可以离开Spring Cloud独立使用开发项目,但是Spring Cloud离不开Spring Boot,属于依赖的关系。

    Spring Boot 是 Spring 的一套快速配置脚手架,可以基于Spring Boot 快速开发单个微服务,Spring Cloud是一个基于Spring Boot实现的云应用开发工具:Spr ...

  3. Mac 文件读写权限问题 OSError: Operation not permitted

    Mac在OS X 10.11以后加入了Rootless功能,主要是限制了root权限,阻止用户对部分路径下的目录进行更改.受到限制的有以下目录: /System /bin /sbin /usr (ex ...

  4. maven公布jar、aar、war等到中央库(Central Repository)的步骤

    步骤一:注冊账号.申请ticket. 注冊在这里:https://issues.sonatype.org 申请ticket:创建一个issue.注意这里要选OSSRH,且是PROJECT而不是TASK ...

  5. hdu 2527:Safe Or Unsafe(数据结构,哈夫曼树,求WPL)

    Safe Or Unsafe Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  6. ORA-00972: 标识符过长

    若是拼接成的sql语句,请查找传递参数时字符型字段是否两边少了引号.

  7. 《转》武​汉​的​I​T​公​司

    本文转载自sherry020406前段时间看到版上有同学问在武汉找工作的情况,我谈谈去年找工作时碰到或者听到的一些企业,希望以下内容对想去武汉工作的同学有帮助,也算是对job版的回报.有些公司的情况可 ...

  8. wap开发体会<转载>

    前二天因工作需要,上头要求做一个wap版的网站,到网上学习了一天,弄了个beta版出来(http://wap.luckty.com 功能很一般),整理几点经验如下: 1.wap网站用的是wml标识,非 ...

  9. HDU 5306 Gorgeous Sequence[线段树区间最值操作]

    Gorgeous Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  10. python基础之2

    1.模块 sys模块注意:python文件的文件名一定不能和下面的要导入的模块同名,如:sys_mokuai.py windows下的python3里直接运行: import sys    ----- ...