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的更多相关文章

  1. PAT A1122 Hamiltonian Cycle (25 分)——图遍历

    The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...

  2. PAT甲级——A1122 Hamiltonian Cycle【25】

    The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...

  3. PAT_A1122#Hamiltonian Cycle

    Source: PAT A1122 Hamiltonian Cycle (25 分) Description: The "Hamilton cycle problem" is to ...

  4. 1122 Hamiltonian Cycle (25 分)

    1122 Hamiltonian Cycle (25 分) The "Hamilton cycle problem" is to find a simple cycle that ...

  5. PAT1122: Hamiltonian Cycle

    1122. Hamiltonian Cycle (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...

  6. PAT甲级 1122. Hamiltonian Cycle (25)

    1122. Hamiltonian Cycle (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...

  7. hihoCoder-1087 Hamiltonian Cycle (记忆化搜索)

    描述 Given a directed graph containing n vertice (numbered from 1 to n) and m edges. Can you tell us h ...

  8. PAT 1122 Hamiltonian Cycle[比较一般]

    1122 Hamiltonian Cycle (25 分) The "Hamilton cycle problem" is to find a simple cycle that ...

  9. PAT 1122 Hamiltonian Cycle

    The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...

随机推荐

  1. hashCode和equals的关系分析

    hashCode:说白了,简单的就看做一个函数,但是该函数有可能出现:对于某个x值,存在不止一个y值与之对应.这种情况就叫哈希碰撞. 那么: 1.如果hashCode相等,两个对象不一定是同一个对象( ...

  2. java学习之—合并两个数组并排序

    /** * 合并两个数组并排序 * Create by Administrator * 2018/6/26 0026 * 下午 4:29 **/ public class MergeApp { pub ...

  3. python函数、模块、包

    一.函数 定义函数: def fun_name(para_list): coding def fun_name(para_list): coding return xxx 使用函数,fun_name( ...

  4. Spring注解标签详解@Autowired @Qualifier等 @Slf4j

    @Slf4j @Slf4j注解实现日志输出 自己写日志的时候,肯定需要: private final Logger logger = LoggerFactory.getLogger(LoggerTes ...

  5. vs code軟件操作

    https://www.imooc.com/article/39349 https://www.html.cn/archives/8144

  6. 一、zipkin

    zipkin是Twitter基于google的分布式监控系统Dapper(论文)的开发源实现,zipkin用于跟踪分布式服务之间的应用数据链路(具体就是收集微服务之间的调用情况,然后处理调用之间数据延 ...

  7. Python——Label控件说明

    Anchor :   标签中文本的位置: background(bg)foreground(fg) :背景色:前景色: borderwidth(bd) :边框宽度: width  .height   ...

  8. nginx rewrite重写

    通过官方文档可以看到,rewrite的作用上下文是   server location,可以写在 server里面  亦或location里面; 命令: if (条件) {} 条件判断 set #设置 ...

  9. Asp.Net Core 输出 Word

    In one of the ASP.NET Core projects we did in the last year, we created an OutputFormatter to provid ...

  10. Web API2 使用EF Code Migrations to Seed DB

    console Enable-Migrations 运行这个命令,添加一个文件夹Migrations,Configuration.cs在这文件夹中 打开 Configuration.cs file. ...