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 ...
随机推荐
- [bzoj1592][Usaco09Feb]Making the Grade 路面修整_动态规划
Making the Grade 路面修整 bzoj-1592 题目大意:给你n段路,每段路有一个高度h[i],将h[i]修改成h[i]$\pm\delta$的代价为$\delta$,求将这n段路修成 ...
- 解决ubuntu上opengl的问题
装完ubuntu之后,对于opengl的程序总是出现问题,先将解决方案列出如下: http://www.linuxforums.org/forum/ubuntu-linux/175490-graphi ...
- iOS:去除UITableView的空白行
要去除UITableView在运行时显示的多余空白行,只需要将TableView的Style从Plain改为Grouped即可.
- apache ant解压zip。支持多级文件夹解压
package cn.liuc.util; import java.io.File; import java.io.FileOutputStream; import java.io.IOExcepti ...
- Java中接口和抽象类的比較
Java中接口和抽象类的比較-2013年5月写的读书笔记摘要 1. 概述 接口(Interface)和抽象类(abstract class)是 Java 语言中支持抽象类的两种机制,是Java程序设计 ...
- oc46--nonatomic, retain
// // Person.h #import <Foundation/Foundation.h> #import "Room.h" #import "Car. ...
- hdu 6118(最小费用流)
度度熊的交易计划 Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- 【POJ 3322】 Bloxorz I
[题目链接] http://poj.org/problem?id=3322 [算法] 广度优先搜索 [代码] #include <algorithm> #include <bitse ...
- Shuffle'm Up(串)
http://poj.org/problem?id=3087 题意:每组3个串,前两个串长度为n,第三个串长度为2*n,依次从第二个串(s2)中取一个字符,从第一个串(s1)中取一个字符,...... ...
- Java IO流文件复制/解压的几种方法总结
引言 在JavaWeb项目开发过程,涉及到IO文件的读写操作以及文件的复制copy操作是作为一个程序员不可获取的知识,那接下来就总结一些copy文件的一些方法,与大家通过学习,如果还有其他更好的方法, ...