1122 Hamiltonian Cycle(40行代码+详细注释)

news/2024/7/15 17:16:49 标签: c++, 算法, 图论

分数 25

全屏浏览题目

作者 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

代码长度限制

16 KB

时间限制

300 ms

内存限制

64 MB

哈密顿回路满足四个条件:

1.起点终点相等
2.路径每一步有边
3.所有点都访问到
4.总共n+1个点

#include<bits/stdc++.h>
using namespace std;
const int N=210;
int n,m;
int g[N][N];
int main(){
    cin>>n>>m;//v,e
    memset(g,0,sizeof g);
    for(int i=0;i<m;i++){
        int a,b;
        cin>>a>>b;
        g[a][b]=g[b][a]=1;
    }
    int k;
    cin>>k;//query
    while(k--){//k个查询 
        int num;
        cin>>num;
        vector<int>path;
        while(num--){//将num个点插入到path数组 
            int ver;
            cin>>ver;
            path.push_back(ver);    
        }
        int len=path.size();//获得点的个数 
        int flag=1;//用于判断是否为哈密顿回路,1表示是哈密顿回路,0则不是 
        bool st[N];//用于判断某结点是否被访问过 
        memset(st,0,sizeof st);
        if(len!=n+1)flag=0;//结点数不为n+1个则不是哈密顿回路 
        if(path[0]!=path[len-1])flag=0;//起点终点不等不是哈密顿回路 
        for(int i=0;i<len-1;i++){//遍历每个结点 
            st[path[i]]=true;//访问过的结点标记为true 
            if(!g[path[i]][path[i+1]])flag=0;//相邻的结点没有边则不是哈密顿回路 
        }
        for (int i = 1; i <= n; i ++ )if (!st[i])flag=0;//若有结点没被访问过就不是哈密顿回路    
        if(flag==0)puts("NO");//输出 
        else puts("YES");
    }
    return 0;
}


http://www.niftyadmin.cn/n/344846.html

相关文章

【华为OD机试c++】解压报文【2023 B卷 |200分】

题目描述 为了提升数据传输的效率&#xff0c;会对传输的报文进行压缩处理。 输入一个压缩后的报文&#xff0c;请返回它解压后的原始报文。 压缩规则&#xff1a;n[str]&#xff0c;表示方括号内部的 str 正好重复 n 次。 注意 n 为正整数&#xff08;0 < n < 100&a…

AI绘画Stable Diffusion安装、使用教程 整合包下载

安装Stable Diffusion webui 效果图&#xff1a; 1.准备工作 在下载 AI 绘画工具前&#xff0c;电脑上需要提前下载一些运行的环境 1.下载python、git 首先本地机器最好是英伟达的 N 卡&#xff0c;并且至少需要 4GB 显存才能在本地运行&#xff0c;当然&#xff0c;A 卡也是…

为什么WordPress这么难用?(以及如何让它变得简单点)

WordPress 是世界上最受欢迎的网站构建器&#xff0c;为互联网上超过 43% 的网站提供支持。然而&#xff0c;有些人抱怨说 WordPress 比 Squarespace 和 Wix 等解决方案更难使用。 在本文中&#xff0c;我们将解决为何WordPress这么难用的神话&#xff0c;并分享您可以用来毫无…

Flink 流处理API

目录 一、环境 1.1getExecutionEnvironment 1.2createLocalEnvironment 1.3createRemoteEnvironment 二、从集合中读取数据 三、从文件中读取数据 四、从KafKa中读取数据 1.导入依赖 2.启动KafKa 3.java代码 一、环境 1.1getExecutionEnvironment 创建一个执行环境&…

HTML、PHP实战:搭建一个网页登录页面。

一、实验环境。 MySQL5.7.26 FTP0.9.60 Apache2.4.39 我这里用的是PHPstudy小皮一键搭建的。 数据库 二、登录页面。 登录页面前端代码 文件名&#xff1a;denglu.html <html> <head> <meta charset"UTF-8"> <title>登录界面</ti…

tinymce富文本编辑器使用到二开

tinymce tinymce 一款现代化的富文本编辑器&#xff0c;有专门团队维护&#xff0c;是目前主流的富文本编辑器选择。 安装注意事项&#xff1a; 有两种方案分别是安装对应的vue/react组件&#xff0c;然后直接用组件&#xff0c;或者直接使用tinymce去按原生操作会报找不到文…

安卓与串口通信-实践篇

前言 在上一篇文章中我们讲解了关于串口的基础知识&#xff0c;没有看过的同学推荐先看一下&#xff0c;否则你可能会不太理解这篇文章所述的某些内容。 这篇文章我们将讲解安卓端的串口通信实践&#xff0c;即如何使用串口通信实现安卓设备与其他设备例如PLC主板之间数据交互…

基于 SpringBoot + VUE 【爱音乐管理系统】 平台设计与实现

免费领取源码参考论文 基于SpringBoot VUE 【爱音乐管理系统】 博主介绍&#xff1a; &#x1f680;自媒体 JavaPub 独立维护人&#xff0c;全网粉丝25w&#xff0c;csdn博客专家、java领域优质创作者&#xff0c;前51ctoTOP10博主&#xff0c;知乎/掘金/华为云/阿里云/InfoQ等…