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. 理解WebKit和Chromium: 网页渲染的基本过程

    转载请注明原文地址:http://blog.csdn.net/milado_nju ## 概述 前面介绍了一些渲染引擎的功能,包括网络,资源加载,DOM树,RenderObject树等等,但是,给人以 ...

  2. AngularJS进阶(二十七)实现二维码信息的集成思路

    AngularJS实现二维码信息的集成思路        赠人玫瑰,手留余香.若您感觉此篇博文对您有用,请花费2秒时间点个赞,您的鼓励是我不断前进的动力,与君共勉!      注:点击此处进行知识充电 ...

  3. 03_Nginx添加新模块

     1 进入nginx安装目录,查看nginx版本及其编译参数: [root@localhost nginx]# ./nginx -V nginx version: nginx/1.8.0 buil ...

  4. 传输控制协议(TCP) -- TCP状态转换图

    TCP状态转换图 在<UNIX网络编程 卷1>一书中,作者给出了TCP状态转换图(如下).本文也将围绕此图进行阐释. 注:上图红框表示比较特殊的地方. TCP状态转换两条主线 图2-4中的 ...

  5. 色彩转换——RGB & HSL

    RGB to HSL The R,G,B values are divided by 255 to change the range from 0..255 to 0..1: R' = R/255 G ...

  6. 基于友善之臂ARM-tiny4412--uboot源码分析

    /* * armboot - Startup Code for OMAP3530/ARM Cortex CPU-core * * Copyright (c) 2004 Texas Instrument ...

  7. UITabbar的一些常规用法(总结)

    往往系统自带的UITabbar 不能满足我们的样式或者颜色设计,所以需要调整UITabbar. 1.自定义UITabbar,也是我学到的第一种方式(简单暴力). 先记录一下思路: 首先,隐藏系统自带的 ...

  8. MFC中char*,string和CString之间的转换

    MFC中char*,string和CString之间的转换 一.    将CString类转换成char*(LPSTR)类型 方法一,使用强制转换.例如:  CString theString( &q ...

  9. LeetCode(45)-Bulls and Cows

    题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ...

  10. Spring Boot Kafka

    1.创建集群 http://kafka.apache.org/documentation/#quickstart 有一句我觉得特别重要: For Kafka, a single broker is j ...