PAT_A1122#Hamiltonian Cycle
Source:
Description:
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 Mlines 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 Klines 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, orNO
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
Keys:
Code:
/*
Data: 2019-06-20 16:30:23
Problem: PAT_A1122#Hamiltonian Cycle
AC: 12:29 题目大意:
H圈定义:简单圈且包含全部顶点;
判断所给圈是否为H圈
*/
#include<cstdio>
#include<set>
#include<algorithm>
using namespace std;
const int M=;
int grap[M][M],path[M]; int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE fill(grap[],grap[]+M*M,);
int n,m,k,v1,v2;
scanf("%d%d", &n,&m);
for(int i=; i<m; i++)
{
scanf("%d%d", &v1,&v2);
grap[v1][v2]=;
grap[v2][v1]=;
}
scanf("%d", &m);
while(m--)
{
set<int> ver;
scanf("%d", &k);
for(int i=; i<k; i++){
scanf("%d", &path[i]);
ver.insert(path[i]);
}
int reach=;
for(int i=; i<k-; i++){
if(grap[path[i]][path[i+]]==){
reach=;break;
}
}
if(reach== || path[]!=path[k-] || ver.size()!=n || k!=n+)
printf("NO\n");
else
printf("YES\n");
} return ;
}
PAT_A1122#Hamiltonian Cycle的更多相关文章
- PAT1122: Hamiltonian Cycle
1122. Hamiltonian Cycle (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...
- A1122. Hamiltonian Cycle
The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...
- 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 分)
1122 Hamiltonian Cycle (25 分) The "Hamilton cycle problem" is to find a simple cycle that ...
- PAT甲级 1122. Hamiltonian Cycle (25)
1122. Hamiltonian Cycle (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...
- hihoCoder-1087 Hamiltonian Cycle (记忆化搜索)
描述 Given a directed graph containing n vertice (numbered from 1 to n) and m edges. Can you tell us h ...
- 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 ...
- 1122. Hamiltonian Cycle (25)
The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...
随机推荐
- K - Count the string kmp_Next数组应用
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- [转]十五天精通WCF——第十一天 如何对wcf进行全程监控
说点题外话,我们在玩asp.net的时候,都知道有一个叼毛玩意叫做“生命周期”,我们可以用httpmodule在先于页面的page_load中 做一些拦截,这样做的好处有很多,比如记录日志,参数过滤, ...
- OpenCV使用说明
我在这边大概说一下OpenCV的使用,具体环境配置参考下面我给出的两个链接. 1. 对于目前OpenCV来说,安装变的简单了很多,现在官方已经给出了预编译文件,不要重新编译.具体使用可以参考http: ...
- ajax——dom基础
javascript中dom实现能够使我们在ajax中通过javascript代码对html和xml数据进行dom方式操作,从而做到页面的动态改动更新和数据的提取处理. dom概念 dom文档对象模型 ...
- IntelliJ IDEA 14注冊码
User:xring Key:21423-V4P36-U7W8K-8CYUK-93HXA-MKGZ5 User:arix Key:52998-LJT74-J7YEX-UPVT3-Q5GUF-5G4B5 ...
- 《CS:APP》 chapter 9 Vitrual Memory 笔记
Vitrual Memory In order to manage memory more efficiently and with fewer errors, modern systems prov ...
- storm 并行度
1个worker进程运行的是1个topology的子集(注:不会出现1个worker为多个topology服务).1个worker进程会启动1个或多个executor线程来运行1个topology的c ...
- [AtCoder3954]Painting Machines
https://www.zybuluo.com/ysner/note/1230961 题面 有\(n\)个物品和\(n-1\)台机器,第\(i\)台机器会为第\(i\)和\(i+1\)个物品染色.设有 ...
- RTP协议分析和详解
一.RTP协议分析 第1章. RTP概述 1.1. RTP是什么 RTP全名是Real-time Transport Protocol(实时传输协议).它是IETF提出的一个标准,对应的RF ...
- .Net-ASP.NET Web API:目录
ylbtech-.Net-ASP.NET Web API:目录 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 0. https://www.asp.net/we ...