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 ...
随机推荐
- Centos Apache 80 代理Tomcat 8080端口
运行环境:Centos 6.5 Apache: 2.2.5 开启apache proxy的相应模块 编辑 /etc/httpd/conf/httpd.conf文件 sudo vim /etc/http ...
- 【POJ3155】生活的艰辛Hard Life
题面 Description ADN公司内部共 n个员工,员工之间可能曾经因为小事有了过节,总是闹矛盾.若员工u和员工 v有矛盾,用边(u, v)表示,共 m个矛盾.最近,ADN公司内部越来越不团结, ...
- 14. static(静态) 关键字
1.修饰成员变量 1)定义:数据需要被共享给所有对象使用使用static修饰(全局变量) 2)注意: 1.用static中创建的成员变量在内存中只有一份 2.千万不要为了方便访问数据而使用static ...
- bzoj1036题解
[解题思路] 直接上树剖套线段树/BIT即可.复杂度o(n+qlog22n)(线段树)或o(n+qlog23n)(BIT). [参考代码] 树剖套BIT.(这个树剖好naive啊QAQ) #inclu ...
- NOIp2018集训test-9-6(am)
Problem A. divisor 发现x为k可表达一定可以表示成这种形式,如k=3,x=(1/3+1/2+1/6)x. 于是可以搜索k(k<=7)个1/i加起来等于1的情况,如果一个数是这些 ...
- 谈谈E语言
基于中国文化底蕴的编程语言, 绝对不是E语言那个样子. 基于中文的编程,必将是计算机届的一次原子爆炸!
- Spring Boot 遇到空指针
@Autowired private IRoadRescueService roadRescueService; 千万不要把注入的service类设为static
- Django中的HttpResponse和JsonResponse
Django中的HttpResponse和JsonResponse 我们在编写一些借口函数的时候,经常需要给调用者返回json格式的数据,那么如何返回可直接解析的数据呢? 首先第一种方式: from ...
- Codeforces 1163A - Eating Soup
题目链接:http://codeforces.com/problemset/problem/1163/A 题意:n 只猫围成一圈,离开 m 只,最多剩下几组猫. 思路:当 n == m 即猫都离开时 ...
- shell得到两个文件的差集
第一种方法: grep: [root@hdp05 src]# grep -vxFf leadering.txt leaderNum.txt [root@hdp05 src]# cat leaderin ...