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. mysql -- 按时间查询 今天、昨天、明天、上月....

    今天   select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天   SELECT * FROM 表名 WHERE TO_DAYS( NO ...

  2. 转载:基于Redis实现分布式锁

    转载:基于Redis实现分布式锁  ,出处: http://blog.csdn.net/ugg/article/details/41894947 背景在很多互联网产品应用中,有些场景需要加锁处理,比如 ...

  3. 浅谈Spring(一)

    Spring 框架是一个分层架构,由 7 个定义良好的模块组成.Spring 模块构建在核心容器之上,核心容器定义了创建.配置和管理 bean 的方式. watermark/2/text/aHR0cD ...

  4. hdu 1201:18岁生日(水题,闰年)

    18岁生日 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  5. 【DeepLearning】一些资料

    记录下,有空研究. http://nlp.stanford.edu/projects/DeepLearningInNaturalLanguageProcessing.shtml http://nlp. ...

  6. std__vector介绍

    vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单地说,vec ...

  7. oracle 存储过程返回结果集

    好久没上来了, 难道今天工作时间稍有空闲, 研究了一下oracle存储过程返回结果集. 配合oracle临时表, 使用存储过程来返回结果集的数据读取方式可以解决海量数据表与其他表的连接问题. 在存储过 ...

  8. nexus的pom配置

    <groupId>com .sms</groupId><artifactId>sms </artifactId><packaging>pom ...

  9. vue mixins的使用

    官网传送 刚开始接触vue的时候,官网关于mixins的例子看了好几遍,发现还是不会用,包括vuex也是后来慢慢理解一点的,不过和vuex比起来.mixns还是很好理解,简单很多了 就我目前理解mix ...

  10. Angular2+学习第2篇 cli 环境搭建过程

    Angular-cli是angular团队针对Angular2提供的脚手架,用于环境搭建,运行等:具体参考Angular-cli GitHub Angular的启动过程,需要先回答三个问题: 启动时加 ...