PAT 1122 Hamiltonian Cycle[比较一般]
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 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
题目大意:判断给出的路径是否是哈密顿回路,哈密顿回路是一个简单回路,包含图中的每一个点,
我的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[比较一般]的更多相关文章
- PAT 1122 Hamiltonian Cycle
The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...
- PAT甲级 1122. Hamiltonian Cycle (25)
1122. Hamiltonian Cycle (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...
- 1122 Hamiltonian Cycle (25 分)
1122 Hamiltonian Cycle (25 分) The "Hamilton cycle problem" is to find a simple cycle that ...
- 1122 Hamiltonian Cycle (25 分)
1122 Hamiltonian Cycle (25 分) The "Hamilton cycle problem" is to find a simple cycle that ...
- PAT A1122 Hamiltonian Cycle (25 分)——图遍历
The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...
- 1122. Hamiltonian Cycle (25)
The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...
- PAT甲题题解-1122. Hamiltonian Cycle (25)-判断路径是否是哈密顿回路
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789799.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- 1122 Hamiltonian Cycle
题意:包含图中所有结点的简单环称为汉密尔顿环.给出无向图,然后给出k个查询,问每个查询是否是汉密尔顿环. 思路:根据题目可知,我们需要判断一下几个条件:(1).首先保证给定的环相邻两结点是连通的:(2 ...
- PAT1122: Hamiltonian Cycle
1122. Hamiltonian Cycle (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...
随机推荐
- EasyUI 表单 tree
第一步:创建HTML标记 <divid="dlg"style="padding:20px;"> <h2>Account Info ...
- C++ 匿名对象的生命周期
//匿名对象的生命周期 #define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; class Poin ...
- Latex之希腊字母表 花体字母 实数集
花体字母 \mathcal{x} 实数集字母 \mathbb{R} 转自:http://blog.csdn.net/lanchunhui/article/details/49819445 拉丁字母是2 ...
- 【转】【Linux】sed命令详解
sed命令详解 sed是stream editor的简称,也就是流编辑器.它一次处理一行内容,处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令 ...
- OAuth认证协议原理分析及同步消息到Twitter和Facebook使用方法
OAuth有什么用?为什么要使用OAuth? twitter或豆瓣用户一定会发现,有时候,在别的网站,点登录后转到 twitter登录,之后转回原网站,你会发现你已经登录此网站了,这种网站就是这个效果 ...
- db2 import和load
Import和Load 都可以将数据导入到DB2服务器中,但是2者是有很大区别的. Import 其实执行了SQL 的INSERT 操作.和INSERT 操作一样,Import 执行的时候会激活触发器 ...
- ChemDraw怎么绘制H-点或H-划
ChemDraw软件是一款全球领先的化学绘图工具,能够绘制各类化学结构图形和化学方程式,在基础化学.有机化学和分析化学等领域得到了广泛的应用.H-点和H-划是日常作图过程中使用频率较高的化学符号,必须 ...
- UIWindow小记
If you choose to create a window in Interface Builder, be sure to select the Full Screen at Launch o ...
- spring配置文件头部配置解析
http://blog.csdn.net/f_639584391/article/details/50167321
- JavaScript设计模式——观察者模式
观察者模式,又称发布-订阅模式或消息机制,定义了一种依赖关系,解决了主题对象与观察者之间功能的耦合. 通过运用观察者模式,可以解决团队开发中的模块间通讯问题,这是模块间解耦的一种可行方案. 首先,我们 ...