1122. Hamiltonian Cycle (25)

时间限制
300 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

  1. A1122. Hamiltonian Cycle

    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. 1122 Hamiltonian Cycle (25 分)

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

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

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

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

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

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

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

  7. PAT 1122 Hamiltonian Cycle

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

  8. PAT_A1122#Hamiltonian Cycle

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

  9. 1122. Hamiltonian Cycle (25)

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

随机推荐

  1. SpriteBuilder中音频文件格式的需要注意的地方

    就像在SpriteBuilder项目子目录中的其他资源文件一样,音频文件夹需要确定完整的文件夹路径. 并且如果音频文件输出格式为MP4,则扩展为.m4a(audio-only MPEG4)而不是.mp ...

  2. JUnit4快速入门

    JUnit最佳实践 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class CardServiceTest {     /**      * 最佳 ...

  3. C语言颜色转换宏

    C语言颜色转换宏 #define COLOR_BPP16_RGB555 /* Win RGB */ #define COLOR_RGB(r,g,b) ((COLORREF)(((BYTE)(r)|(( ...

  4. 中国象棋游戏Chess(3) - 实现走棋规则

    棋盘的绘制和走棋参看博文:中国象棋游戏Chess(1) - 棋盘绘制以及棋子的绘制,中国象棋游戏Chess(2) - 走棋 现在重新整理之前写的代码,并且对于每个棋子的走棋规则都进行了限制,不像之前那 ...

  5. androd输入管理系统机制解析

     android的输入管理系统主要完成按键.触摸板.鼠标等输入设备的事件输入,功能包括,输入设备的事件输入及向焦点窗口和焦点视图的事件派发,事件的插入,事件的过滤,事件的拦截等功能. 整个输入系统 ...

  6. 【编程练习】快速select算法的实现

    代码来自: http://blog.csdn.net/v_JULY_v 算法思想: // Quick_select.cpp : 定义控制台应用程序的入口点. // #include "std ...

  7. unity C#更改系统默认鼠标指针

    最近项目需要替换鼠标的默认图标,实现的效果是初始状态为一种图标,点击鼠标左键要换成另一种图标,按网上通用的方法做了以后,隐藏鼠标指针,在指针的位置画一个图片就可以了,但不知道怎么回事,这种方法画的图标 ...

  8. Markdown语法及编辑器

    宗旨 Markdown 的目标是实现「易读易写」. 可读性,无论如何,都是最重要的.一份使用 Markdown 格式撰写的文件应该可以直接以纯文本发布,并且看起来不会像是由许多标签或是格式指令所构成. ...

  9. 简单了解AJAX

    // ajax的俩个版本: var xmlhttp; if(window.xmlHttpRequest){ xmlhttp = new xmlHttpRequest(); }else{ xmlhttp ...

  10. List内存分配

    当采用默认构造函数List<int> value = new List<int>();实例化一个List<T>对象时,.Net Framework只是在内存中申请了 ...