PAT甲级 1122. Hamiltonian Cycle (25)
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
————————————————————————————————
题目的意思是给出n个点m条边,对于每次查询判断是否是哈密顿路径
思路:根据查询,分别判断点数,首尾是否一致,是否每个点都遍历,路径是否联通
若均符合则YES否则NO
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std; #define LL long long
const int INF = 0x3f3f3f3f; int mp[500][500];
int a[500];
int main()
{
int n,m,x,y,q,k;
scanf("%d%d",&n,&m);
memset(mp,0,sizeof mp);
for(int i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
mp[x][y]=mp[y][x]=1;
}
scanf("%d",&q);
while(q--)
{
scanf("%d",&k);
for(int i=0;i<k;i++)
scanf("%d",&a[i]);
if(n==1&&k==1)
{
printf("YES\n");
continue;
}
if(k!=n+1||a[0]!=a[k-1])
{
printf("NO\n");
continue;
}
int flag=0;
set<int>s;
s.insert(a[0]);
for(int i=1;i<k;i++)
{
if(mp[a[i]][a[i-1]]==0)
{
flag=1;
break;
}
s.insert(a[i]);
}
if(s.size()!=n)
flag=1;
printf("%s\n",flag==0?"YES":"NO"); }
return 0;
}
PAT甲级 1122. Hamiltonian Cycle (25)的更多相关文章
- 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 (25 分)
1122 Hamiltonian Cycle (25 分) The "Hamilton cycle problem" is to find a simple cycle that ...
- PAT 1122 Hamiltonian Cycle[比较一般]
1122 Hamiltonian Cycle (25 分) The "Hamilton cycle problem" is to find a simple cycle that ...
- PAT 1122 Hamiltonian Cycle
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甲级 1121. Damn Single (25)
1121. Damn Single (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue "Dam ...
随机推荐
- OKI系列针式打印机更换色带图解教程
色带一直换不好,今天找到一个带图的教程,收藏一下 打开新色带的包装后,我们可以仔细观察一下新色带,找到里面带有一段“扭曲”色带的位置,这段色带就是:“莫比乌斯带”结构. 找到“莫比乌斯带”结构(就是有 ...
- spec文件写作规范
spec文件写作规范 2008-09-28 11:52:17 分类: LINUX 1.The RPM system assumes five RPM directories BUILD:rpmbuil ...
- PS故障风海报制作技术分享
1.首先找一张看起来很酷的图(也可以选择自己喜欢的图片): 2. 复制图层,点击添加图层样式,选择混合选项,在高级混合里面的通道选项,有R.G.B三个通道选项,默认是全部勾选的状态,选择其中一个勾掉( ...
- Intellij idea 系列教程目录
Intellij idea 系列教程目录 Intellij idea 系列教程之破解方法 Intellij idea 系列教程之常用快捷键 Intellij idea 系列教程之常用配置项 每天用心记 ...
- Netty 源码 Channel(二)核心类
Netty 源码 Channel(二)核心类 Netty 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) 一.Channel 类图 二. ...
- Django web project
在virtualenv下 (myvenv) ~/djangogirls$ django-admin startproject mysite . 生成web 工程目录 djangogirls ├───m ...
- Tomcat优化详细1
在Tomcat和应用程序进行了压力测试后,如果您对应用程序的性能结果不太满意,就可以采取一些性能调整措施了,当然了前提是应用程序没有问题,我们这里只讲Tomcat的调整.由于Tomcat的运行依赖于J ...
- 棋盘问题(NOIP1997)
题目链接:棋盘问题 这道题水不水呢?还是很水的,为什么?因为数据太小了.直接算就行了. #include<bits/stdc++.h> using namespace std; int m ...
- Cisco interview
A. 1. Self-introduction I am Yanlin He . I am a master degree candidate of school of infomation sci ...
- 2018.10.25 bzoj3928: [Cerc2014] Outer space invaders(区间dp)
传送门 区间dpdpdp好题. 首先肯定需要把坐标离散化. 然后在数轴上面区间dpdpdp. 对于当前区间,区间中最大的数一定会被选. 于是我们记f[i,j]f[i,j]f[i,j]表示所有左端点在i ...