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 V​1 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<iostream> //水题
#include<vector>
using namespace std;
int main(){
int vn, en, qn;
cin>>vn>>en;
vector<vector<int>> map(vn+1, vector<int>(vn+1, 0));
vector<int> visited(vn+1, 0);
for(int i=0; i<en; i++){
int v1, v2;
cin>>v1>>v2;
map[v1][v2]=map[v2][v1]=1;
}
cin>>qn;
for(int i=0; i<qn; i++){
int n, flag=0;
cin>>n;
vector<int> path(n, 0);
vector<vector<int>> temp=map;
vector<int> visited(vn+1, 0);
for(int j=0; j<n; j++)
cin>>path[j];
if(path[0]!=path[n-1]){
cout<<"NO"<<endl;
continue;
}
for(int j=0; j<n-1; j++)
if(temp[path[j]][path[j+1]]==1){
visited[path[j]]=visited[path[j+1]]=1;
temp[path[j]][path[j+1]]=temp[path[j+1]][path[j]]=0;
}else{
flag=1;
break;
}
for(int j=1; j<=vn; j++)
if(visited[j]!=1)
flag=1;
flag==0?cout<<"YES"<<endl:cout<<"NO"<<endl;
}
return 0;
}

PAT 1122 Hamiltonian Cycle的更多相关文章

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

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

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

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

  3. 1122 Hamiltonian Cycle (25 分)

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

  4. 1122 Hamiltonian Cycle (25 分)

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

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

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

  6. 1122. Hamiltonian Cycle (25)

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

  7. PAT甲题题解-1122. Hamiltonian Cycle (25)-判断路径是否是哈密顿回路

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789799.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  8. 1122 Hamiltonian Cycle

    题意:包含图中所有结点的简单环称为汉密尔顿环.给出无向图,然后给出k个查询,问每个查询是否是汉密尔顿环. 思路:根据题目可知,我们需要判断一下几个条件:(1).首先保证给定的环相邻两结点是连通的:(2 ...

  9. PAT1122: Hamiltonian Cycle

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

随机推荐

  1. 杂项:MySQL

    ylbtech-杂项:MySQL 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部 0. https://www.mysql.com/ 1. https://bai ...

  2. 基于Spark和Tensorflow构建DCN模型进行CTR预测

    实验介绍 数据采用Criteo Display Ads.这个数据一共11G,有13个integer features,26个categorical features. Spark 由于数据比较大,且只 ...

  3. 清北考前刷题day6早安

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #d ...

  4. Java多线程(一) Thread和 Runnable

    http://www.cnblogs.com/lwbqqyumidi/p/3804883.html 1.继承Thread 2.实现Runnable接口 public class MyRunnable ...

  5. SQL 事务篇和约束

    数据库事务: 数据库事务(Database Transaction) ,是指作为单个逻辑工作单元执行的一系列操作,要么完全地执行,要么完全地不执行 事务是恢复和并发控制的基本单位.事务应该具有4个属性 ...

  6. DFS/BFS(同余模) POJ 1426 Find The Multiple

    题目传送门 /* 题意:找出一个0和1组成的数字能整除n DFS:200的范围内不会爆long long,DFS水过~ */ /************************************ ...

  7. C#将类对象转换为字典

    主要是实现将类里面 的属性和对应的值转换为字典的键和值. public class RDfsedfw { /// <summary> /// 将匿名类转换为字典 /// </summ ...

  8. js复制功能

    // 复制功能 copyUrl() { var Url = document.getElementById('biao') Url.select() // 选择对象 document.execComm ...

  9. 292 Nim Game Nim游戏

    您和您的朋友,两个人一起玩 Nim游戏:桌子上有一堆石头,每次你们轮流拿掉 1 到 3 块石头. 拿掉最后一块石头的人就是胜利者.由您来开局.你们两个都是聪明人,相信都有最佳的游戏策略. 请编写一个函 ...

  10. 【转】mysql INSERT的用法

    转自:http://www.cnblogs.com/ggjucheng/archive/2012/11/05/2754938.html insert的语法 INSERT [LOW_PRIORITY | ...