PAT甲级——A1122 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), 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
第一,遍历点K一定等于N+1,因为既要遍历且一次所有顶点,而且回到起点
NO
NO
NO
YES
NO
第二,遍历的最后一个点一定是起点
使用visit记录顶点是否遍历过了,graph记录路径是否能行
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int n, m, k, a, b, start, graph[][];
bool visit[];//记录每个顶点遍历一次
fill(graph[], graph[] + * , -);
cin >> n >> m;
while (m--)
{
cin >> a >> b;
graph[a][b] = graph[b][a] = ;
}
cin >> m;
while (m--)
{
cin >> k;
bool flag = true;
fill(visit, visit + , false);
for (int i = ; i < k; ++i)
{
cin >> b;
if (flag == false || k != n + )//遍历所有的顶点并回到起点,则一定走过n+1个点
{
flag = false;
continue;
}
if (i == )
start = b;//记录起点
else if (graph[a][b] != )//此路不通
flag = false;
else if (i == k - && b != start)//最后一个点不是起点
flag = false;
else if (i != k - && visit[b] != false)//除了最后一次重复遍历起点,出现了其他点重复遍历,
flag = false;
else
visit[b] = true;//遍历过
a = b;//记录前一个点
}
if (flag)
{
for (int i = ; i <= n && flag == true; ++i)
if (visit[i] == false)//存在没有遍历的顶点
flag = false;
if (flag)
cout << "YES" << endl;
}
if (flag == false)
cout << "NO" << endl;
}
return ;
}
PAT甲级——A1122 Hamiltonian Cycle【25】的更多相关文章
- 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 ...
- PAT A1122 Hamiltonian Cycle (25 分)——图遍历
The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...
- A1122. Hamiltonian Cycle
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甲级】1070 Mooncake (25 分)(贪心水中水)
题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全 ...
- PAT甲题题解-1122. Hamiltonian Cycle (25)-判断路径是否是哈密顿回路
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789799.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT甲级 1121. Damn Single (25)
1121. Damn Single (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue "Dam ...
- PAT甲级 1126. Eulerian Path (25)
1126. Eulerian Path (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue In grap ...
随机推荐
- leetcode-12周双周赛-5090-抛掷硬币
题目描述: 二维dp: class Solution: def probabilityOfHeads(self, prob: List[float], target: int) -> float ...
- NX二次开发-UFUN创建块UF_MODL_create_block
NX9+VS2012 #include <uf.h> #include <uf_modl.h> UF_initialize(); UF_FEATURE_SIGN Sign = ...
- js 彻底理解回调函数
一.前奏 在谈回调函数之前,先看下下面两段代码: 不妨猜测一下代码的结果. function say (value) { alert(value); } alert(say); alert(say(' ...
- JDK 安装步骤
1.JAVA_HOME = JDK安装路径 2. Path = %JAVA_HOME%\bin; 3. CLASSPATH = .;%JAVA_HOME%\lib\dt.jar;%J ...
- Openstack组件实现原理 — OpenVswitch/Gre/vlan
目录 目录 前文提要 Neutron 管理的网络相关实体 OpenVswitchOVS OVS 的架构 VLan GRE 隧道 Compute Node 中的 Instance 通过 GRE 访问 P ...
- Delphi中文件名函数-路径、名称、子目录、驱动器、扩展名
文件名函数 文件名函数可以对文件的名称.所在子目录.驱动器和扩展名等进行操作.下表列出这些函数及其功能. 函数说明 ExpandFileName() //返回文件的全路径(含驱动器.路径) Extra ...
- BackgroundWorker study
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 关于private,default,protected,public,成员变量访问权限
关于private,protected,public,default成员变量的访问权限,请参阅上图! 子类要访问父类的private成员变量,必须采用采用get方法: eg: public class ...
- springDataJpa的官方API
一 . Core concepts(核心概念) 1.springdata中的中心接口是——Repository.这个接口没有什么重要的功能(原句称没什么惊喜的一个接口).主要的作用就是标记和管理.其 ...
- 2018ICPC焦作 F. Honeycomb /// BFS
题目大意: 给定n m表示一共n行每行m个蜂巢 求从S到T的最短路径 input 1 3 4 +---+ +---+ / \ / \ + +---+ +---+ \ \ / \ + + S +---+ ...