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 ...
随机推荐
- Ubuntu 18.04 切换使用Python3
我安装的Ubuntu 默认的python是2.7.5 python -V 我参考网上照到的文章,如果需要默认python为 python3 python命令默认是 python 3 sudo cp / ...
- 快速求1~n的k!,k的逆元
1.求1~n的k! 2.求inv(k!) 3.inv((k-1)!)=inv(k!)*k%mod 4.inv(k)=inv(k!)*((k-1)!)%mod 如 https://www.cnblogs ...
- Codeforces Round #563 (Div. 2) E. Ehab and the Expected GCD Problem
https://codeforces.com/contest/1174/problem/E dp 好题 *(if 满足条件) 满足条件 *1 不满足条件 *0 ///这代码虽然写着方便,但是常数有点大 ...
- MySQL 刷题知识点整理
1. left join on 与 right join on, inner join on 的区别: left join on 把左表中的行全部展示,而将寻找右表中符合的行展示: right joi ...
- leetcood学习笔记-169-求众数
题目描述: 方法一:排序输出中位数 class Solution(object): def majorityElement(self, nums): """ :type ...
- Servlet - Servlet相关
1. 概念 Servlet是指任何实现了Servlet接口的类, Servlet运行于支持Java的应用服务器中, Servlet可以响应任何类型的请求, 但大多数情况下, Servlet只用来扩展基 ...
- python系统模块
Python中大多数系统接口都集中在两个模块:sys和os.这么说有点过于简单化 还有一些其他的表转模块也属于这个领域他们包括: glob 用于文件名的扩展 socket 用于网络连接和进程间通信(I ...
- Java初识方法
5.初识方法 方法就是一段代码片段,这个片段可以完成特定的功能,并且可以重复利用. 5.1 方法的定义 5.1.1方法的定义格式 [方法修饰列表] 返回值类型 方法名(方法参数列表){ 方法体 } ① ...
- 回滚线段树+bitset优化01背包——cf981E
/*首先考虑如何计算一个点的可能凑出的值,这就是一个01可行性背包问题那么再拓展到一段区间[1..n]的点上,每个query都可以看做是一段区间上的点[l,r]加上一个体积为x的物品,转换到01背包上 ...
- CommandLineToArgvW调EXE传入参数【转载】
#include <afxwin.h> // TODO: add your code here LPWSTR *szArglist = NULL; ; szArglist = Comma ...