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. python之类和__init__

    构建一个商品类,__init__函数类似于构造方法,self类似于this import random class Goods: def __init__(self, name, price): se ...

  2. 各个版本spring的jar包以及源码下载地址,目前最高版本到spring4.3.8,留存备用:

    http://maven.springframework.org/release/org/springframework/spring/

  3. Django的View(视图)

    Django的View(视图) 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个重定向,一个404错误, ...

  4. 如何创建djiago项目和djiago连接数据库

    介绍 主要介绍在python中如何使用pycharm创建djiago项目以及如何将djiago项目和mysal数据库连接起来 创建djiago项目 1.使用pycharm创建djiao项目 点击pyc ...

  5. 学习Android过程中遇到的未解决问题(个人笔记,细节补充,随时更新)

    201811/13 使用HttpURLConnection对象调用方法又出现IO异常,我又百度个博客搜寻答案,未果.下午试试真机,完美.自己建了服务器tomcat,编写android访问自己tomca ...

  6. 继承&派生 属性查找

    # 在单继承背景下,无论是新式类还是经典类属性查找顺序都一样 # 先object->类->父类->... 实例: class Foo: def f1(self): print('Fo ...

  7. [BJWC2010] 严格次小生成树

    [BJWC2010]严格次小生成树算法及模板 所谓次小生成树,即边权之和第二小的生成树,但所谓严格,就是不能和最小的那个相等. 求解严格次小生成树的方法一般有倍增和LCT两种.当然LCT那么高级的我当 ...

  8. prufer序列

    介绍 其实是\(pr\ddot{u}fer\)序列 什么是prufer序列? 我们认为度数为\(1\)的点是叶子节点 有一颗无根树,每次选出编号最小的叶子节点,加到当前prufer序列的后面,然后删掉 ...

  9. rar自动压缩备份

    rem ******MySQL backup start********@echo offforfiles /p "D:\备份\发布软件备份" /m backup_*.sql -d ...

  10. 「CF838B」 Diverging Directions

    B. Diverging Directions 题意 给出一个n个点2n-2条边的有向图.n-1条指向远离根方向的边形成一棵树,还有n-1条从非根节点指向根节点的边. q次操作,1修改第x条边权值为y ...