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 ...
随机推荐
- 聊聊javaMail
今天闲着无事 看了看用java发送邮件的相关知识 代码参考自<<精通Java Web整合开发(JSP+AJAX+Struts+Hibernate)>>(第2版) 不多说 先上图 ...
- mysql进阶(十八)完全卸载mysql数据库图文教程
完全卸载mysql数据库图文教程 有时候MySQL不能完全卸载,这时候必须通过一些途径删除掉注册表和一些残余的文件,然后才能重新安装才可以成功! 方法/步骤 1.控制面板-->所有控制面板项-- ...
- mysql进阶(十七)Cannot Connect to Database Server
Cannot Connect to Database Server 缘由 由于不同的项目中使用的数据库用户名与密码出现了不一致的情况,在其中之前较早一个项目执行过程中出现"The user ...
- 青年之锋文学网( www.xcqnzf…
青年之锋文学网( www.xcqnzf.com )简介: 青年之锋文学网创建于2013年秋,是河南农业大学(应用科技学院)--青年之锋文学社的官方网站,网站以长篇写作和出版校刊为主题,短篇精彩丰富为中 ...
- MVC学习笔记(一)
首先感谢慕课网这个平台提供给我的学习机会,感谢PengCheng老师的"MVC架构模式分析与设计课程". 1.数组的声明: $controllerAllow = array('te ...
- 【面试笔试算法】Program 6: 字符消除(hiho题库)
时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi最近在玩一个字符消除游戏.给定一个只包含大写字母"ABC"的字符串s,消除过程是如下进行的: 1) ...
- Linux - man page
使用man date来查看date命令的详细信息. lucifer@lucifer-virtual-machine:~$ man date DATE(1) User Commands DATE(1) ...
- CentOS 7 运行级别的切换
CentOS 7 运行级别的切换 由命令行级别切换到窗口级别的命令未变:init 5或startx 由窗口级别切换到命令行级别的命令未变:init 3 新版本的运行级别都定义在 /lib/system ...
- C# DataTable,DataSet,IList,IEnumerable 互转扩展属性
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.R ...
- objective-c 2.0的字面量Literals
obj-c 2.0增加了许多核心对象字面量的简单语法,向ruby学习吗? 直接上代码: #import <Foundation/Foundation.h> int main(void){ ...