PAT1122: Hamiltonian Cycle
1122. Hamiltonian Cycle (25)
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 思路
图中从一个点出发的一条路径能够走过所有的点并回到出发点,除起始点外所有其他点只能访问一次,这种情况产生的回路叫哈密尔顿回路。
所以验证输入的路径是不是哈密尔顿回路,必须满足以下条件:
1.输入的节点个数必须等于 总结点数 + 1
2.不能有重复出现的节点(只能走一次,起点除外)
3.起点终点必须相同。
4.两个节点之间必须直接相通(即被一条直线直接相连) 代码
#include<iostream>
#include<vector>
#include<set>
using namespace std;
vector<vector<int>> graph(201,vector<int>(201,-1));
int main()
{
int N,M;
while(cin >> N >> M )
{
for(int i = 1;i <= M;i++)
{
int a,b;
cin >> a >> b;
graph[a][b] = graph[b][a] = 1;
}
int K;
cin >> K;
for(int i = 0;i < K;i++)
{
int n;
set<int> visits;
cin >> n;
vector<int> nodes(n + 1);
for(int j = 1;j <= n;j++)
{
cin >> nodes[j];
visits.insert(nodes[j]); }
if(n != N + 1 || nodes[1] != nodes[n] || visits.size() != N)
{
cout << "NO" << endl;
continue;
}
bool isha = true;
for(int j = 2;j <= n;j++)
{
if(graph[nodes[j]][nodes[j - 1]] != 1)
{
isha = false;
break;
}
}
if(isha)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}
}
PAT1122: Hamiltonian Cycle的更多相关文章
- 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 ...
- PAT_A1122#Hamiltonian Cycle
Source: PAT A1122 Hamiltonian Cycle (25 分) Description: The "Hamilton cycle problem" is to ...
- 1122. Hamiltonian Cycle (25)
The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...
随机推荐
- Xcode 下cocos-2dx 环境搭建
一.Cocos2d-x简介 Cocos2d-x是一个开源的移动2D游戏框架,MIT许可证下发布的,这是一个C++ Cocos2d-iPhone项目的版本. Cocos2d-X发展的重点是围绕Cocos ...
- Eclipse集成Android_NDK
Eclipse集成Android NDK说明 2 为什么要用NDK? 以下内容引用自<Pro Android Games> Some Java develo ...
- VS2005的数据断点功能
多年前在VS2005强大的条件断点功能里面讨论过VS2005的条件断点功能. 其实在VS2005里面还有比较好用的(为什么我不用很牛逼呢?因为和OD比起来实在是太简陋了,但是使用上还是比较方便的)内存 ...
- 主流列式数据库评测:InfiniDB
).本文测试的InfiniDB版本是2010年12月20日发布的2.02版,下载文件名分别为InfiniDB64-2.0.2-2.exe 和InfiniDB64-ent-2.0.2-2.exe.安装文 ...
- ubuntu 的挂起与休眠
待机 计算机将目前的运行状态等数据存放在内存,关闭硬盘.外设等设备,进入等待状态.此时内存仍然需要电力维持其数据,但整机耗电很少.恢复时计算机从内存读 出数据,回到挂起前的状态,恢复速度较快.一般笔记 ...
- OpenCV 透视变换实例
参考文献: http://www.cnblogs.com/self-control/archive/2013/01/18/2867022.html http://opencv-code.com/tut ...
- LDA
2 Latent Dirichlet Allocation Introduction LDA是给文本建模的一种方法,它属于生成模型.生成模型是指该模型可以随机生成可观测的数据,LDA可以随机生成一篇由 ...
- SharePoint 调查添加图片支持
前言:今天,碰到一个有趣的问题,就是SharePoint调查里面,添加对于图片的支持,众所周知,SharePoint的调查就支持那么几种字段类型的问题,当然,我们可以开发实现,不过,这个不是我们今天介 ...
- 一个简单的ruby生成器例子(用连续体Continuation实现)
ruby中有很多经典的驱动器结构,比如枚举器和生成器等.这次简单介绍下生成器的概念.生成器是按照功能要求,一次产生一个对象,或称之为生成一个对象的方法.ruby中的连续体正好可以用来完成生成器的功能. ...
- redis删除所有key
flushdb 删除当前数据库的所有keyflushall 删除所有数据库的所有keydbsize 返回当前数据库的key的数量