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 ...
随机推荐
- ZooKeeper的应用场景(转)
应用场景1 :统一命名服务 分布式应用中,通常需要一套完备的命令机制,既能产生唯一的标识,又方便人识别和记忆. 我们知道,每个ZNode都可以由其路径唯一标识,路径本身也比较简洁直观,另外ZNode上 ...
- JS 缓冲运动 带运动的留言本 小案例
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- linux UID,GID,EUID,EGID,SUID,SGID
SUID, SGID, sticky位可以参考: http://onlyzq.blog.51cto.com/1228/527247/ SUID属性只能运用在可执行文件上,当用户执行该执行文件时,会临时 ...
- c语言文件包含
文件包含是指一个C语言源程序中将另一个C语言源程序包含进来,通过include预处理指令实现. 一般形式: #include”被包含文件名” 或#include<被包含文件名> 2. 作 ...
- 什么是OTN交换?
作者:Babak Samimi 大家不停地听到大数据的显著增长及其带来的全球运营商网络上流量的剧增. 比方.Qmee有一个有意思的infographic,在2013年捕捉了60秒的线上流量,其统计结果 ...
- css3中 弹性盒模型布局之box-flex
box-flex:也就是让子容器针对父容器的宽高属性依照一定的规则来划分 Eg: html代码: <div class="wrap"> <div class=&q ...
- luogu1980 车站分级
题目大意 一些火车站排成一行.给出一些火车的停靠站情况,要求对每一个火车,其经过且不停靠的站的级别比它任意停靠的站的级别小.问所有车站最少需要多少个级别. 题解 不要只看到这道题的背景设立在一个区间上 ...
- 使用powershell来设置时间
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/set-date?view=powers ...
- hdu 1874(最短路 Dilkstra +优先队列优化+spfa)
畅通工程续 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- 【POJ 1011】 Sticks
[题目链接] http://poj.org/problem?id=1011 [算法] 深搜剪枝 首先我们枚举木棍的长度i,那么就有s/i根木棍,其中s为木棍长度的总和,朴素的做法就是对每种长度进行搜索 ...