A1122. Hamiltonian Cycle
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
#include<cstdio>
#include<iostream>
#include<vector>
using namespace std;
const int INF = ;
int G[][], visit[] = {,};
int main(){
int N, M;
fill(G[], G[] + *, INF);
scanf("%d%d", &N, &M);
for(int i = ; i < M; i++){
int v1, v2;
scanf("%d%d", &v1, &v2);
G[v1][v2] = G[v2][v1] = ;
}
int K;
scanf("%d", &K);
for(int i = ; i < K; i++){
int n, s, vi, vj, tag = ;
fill(visit, visit + , );
scanf("%d%d", &n, &s);
if(n != N + )
tag = ;
visit[s] = ;
vi = s;
for(int j = ; j < n; j++){
scanf("%d", &vj);
if(G[vi][vj] == INF){
tag = ;
}
visit[vj]++;
vi = vj;
}
if(vj != s)
tag = ;
for(int i = ; i <= N; i++){
if(i != s && visit[i] != || i == s && visit[i] != )
tag = ;
}
if(tag == )
printf("NO\n");
else printf("YES\n");
}
return ; }
总结:
1、哈密顿回路:图中所有顶点必须都出现,除了首尾是重复出现外,其它节点仅出现一次。 顶点组成的序列必须是联通的。
2、注意,边读入边做处理时,不要使用break,造成读入数据错乱。
A1122. Hamiltonian Cycle的更多相关文章
- PAT A1122 Hamiltonian Cycle (25 分)——图遍历
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 ...
- PAT_A1122#Hamiltonian Cycle
Source: PAT A1122 Hamiltonian Cycle (25 分) Description: The "Hamilton cycle problem" is to ...
- 1122 Hamiltonian Cycle (25 分)
1122 Hamiltonian Cycle (25 分) The "Hamilton cycle problem" is to find a simple cycle that ...
- PAT1122: Hamiltonian Cycle
1122. Hamiltonian Cycle (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...
- 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 ...
随机推荐
- Setting property 'source' to 'org.eclipse.jst.jee.server:hczm' did not find a matching property
- Linux 文件及目录管理命令基础
pwd 显示当前所在目录 cd 切换目录 cd 命令语法 cd [选项] 目录 cd 的常用选项: cd ~ /cd 切换到当前用户的加目录 cd . 保持当前目录不变 cd .. 切换到上级目录 ...
- java学习之—数组的曾删改查
/** * 数组的曾删改查 * Create by Administrator * 2018/6/8 0008 * 上午 9:54 **/ public class HighArray { priva ...
- Java Annotation详解 理解和使用Annotation
系统中用到了java注解: 查了一下如何使用注解,到底注解是什么: (1)创建方法:MsgTrace Java Class==> 在Create New Class中: name:输入MsgTr ...
- 在delphi中生成GUID/自动获取临时表名......
什么是 GUID ? 全球唯一标识符 (GUID) 是一个字母数字标识符,用于指示产品的唯一性安装.在许多流行软件应用程序(例如 Web 浏览器和媒体播放器)中,都使用 GUID. GUID 的格式为 ...
- PHP通过循环给数组赋值
$userDatas = [ ['user_id'=>1], ['user_id'=>3] ]; $userIds = []; foreach ($userDatas as $item){ ...
- JQuery跳出each循环的方法(包含数组遍历)
0. 前言 也许我们通过 jquery 的循环方法进行数组遍历,但是当不符合条件时,怎么跳出当前循环?(即用each方法内,当不满足条件时想break跳出循环体,想continue继续执行下一个循环遍 ...
- 5.cgroup资源控制
控制组( CGroups)是 Linux 内核的一个特性,主要用来对共享资源进行隔离.限制.审计等. 只有将分配到容器的资源进行控制,才能避免多个容器同时运行时对宿主机系统的资源竞争.每个控制组是一组 ...
- cuda编程-矩阵乘法(1)
本方法采用简单的单线程计算每组行和列乘加运算 代码如下: #include <stdio.h> #include <stdlib.h> #include <iostrea ...
- poj2112(网络流-最大流+二分)
题意:给你k个挤奶器,c头牛,每个挤奶器能放m头牛,问你奶牛需要走的最大距离最小是多少: 解题思路:因为最大距离最小,也就是求最小的距离满足所有牛都能到,所以我们先用floyd跑最短路,把所有点之间的 ...