【PAT甲级】1122 Hamiltonian Cycle

简介: 【PAT甲级】1122 Hamiltonian Cycle

1122 Hamiltonian Cycle


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

题意


哈密顿回路问题是找到一个包含图中每个顶点的简单回路。

这样的回路称为“哈密顿回路”。

在本题中,你需要做的是判断给定路径是否为哈密顿回路。


思路


用邻接矩阵来存储每个结点之间是否存在一条边,如果存在则置为 true 。

判断给定的路径是否为哈密顿回路。

先判断首尾结点是否相同,并且给定路径结点数是否为 n+1 个点,因为首尾结点相同,所以会多出一个点。

然后判断该路径中每条边是否存在,并用 st 来存储出现过的结点。

最后再判断所有结点是否都出现过。

根据判断的结果输出最终答案。

代码

#include<bits/stdc++.h>
using namespace std;
const int N = 210;
int n, m;
bool g[N][N], st[N];
int nodes[N * 2];
bool check(int cnt)
{
    //首尾结点必须相同,并且一共要有n个结点
    if (nodes[0] != nodes[cnt - 1] || cnt != n + 1)  return false;
    //先判断每条边是否存在
    memset(st, false, sizeof st);
    for (int i = 0; i < cnt - 1; i++)
    {
        st[nodes[i]] = true;
        if (!g[nodes[i]][nodes[i + 1]])
            return false;
    }
    //再判断是否有结点没有包含在内
    for (int i = 1; i <= n; i++)
        if (!st[i])
            return false;
    return true;
}
int main()
{
    cin >> n >> m;
    //输入边的信息
    for (int i = 0; i < m; i++)
    {
        int a, b;
        cin >> a >> b;
        g[a][b] = g[b][a] = true;
    }
    int k;
    cin >> k;
    while (k--)  //判断是否为哈密顿回路
    {
        int cnt;
        cin >> cnt;
        for (int i = 0; i < cnt; i++)    cin >> nodes[i];
        if (check(cnt))  cout << "YES" << endl;
        else    cout << "NO" << endl;
    }
    return 0;
}


目录
相关文章
|
Web App开发 存储 缓存
RDMA优化整理(一)
简要的介绍了下RDMA的背景,并给出了一些RDMA编程优化技巧
5530 1
RDMA优化整理(一)
|
Dubbo Java 测试技术
分布式RPC框架性能大比拼 dubbo、motan、rpcx、gRPC、thrift的性能比较
Dubbo 是阿里巴巴公司开源的一个Java高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和 Spring框架无缝集成。不过,略有遗憾的是,据说在淘宝内部,dubbo由于跟淘宝另一个类似的框架HSF(非开源)有竞争关系,导致dubbo团队已经解散(参见http://www.oschina.net/news/55059/druid-1-0-9 中的评论),反到是当当网的扩展版本仍在持续发展,墙内开花墙外香。
9001 0
|
资源调度 算法 Linux
Linux进程/线程的调度机制介绍:详细解析Linux系统中进程/线程的调度优先级规则
Linux进程/线程的调度机制介绍:详细解析Linux系统中进程/线程的调度优先级规则
4630 0
|
存储 监控 安全
【探索嵌入式虚拟化技术与应用】— 虚拟化技术深入浅出自学系列
【探索嵌入式虚拟化技术与应用】— 虚拟化技术深入浅出自学系列
630 2
|
网络协议 Dubbo Java
什么是RPC?RPC和HTTP对比?RPC有什么缺点?市面上常用的RPC框架?
选择合适的RPC框架和通信协议,对于构建高效、稳定的分布式系统至关重要。开发者需要根据自己的业务需求和系统架构,综合考虑各种因素,做出适宜的技术选型。
2131 1
|
存储 数据挖掘 API
DPDK_Hash(1)
DPDK_Hash(1)
726 0
|
网络协议 NoSQL API
深入理解 RDMA 的软硬件交互机制
本文深入分析了RDMA技术在数据中心高性能网络环境下的工作原理及软硬件交互机制,通过对比传统Kernel TCP,突出了RDMA在减少延迟、提高系统性能方面的优势,同时讨论了其在内存管理、软硬交互方面的关键技术和挑战,为读者提供了全面理解RDMA技术及其应用场景的视角。
8403 8
|
Linux KVM 虚拟化
系统虚拟化基本概念与发展历史和趋势展望
系统虚拟化基本概念与发展历史和趋势展望
1499 0