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 Kwhich 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
题意:判断是否为Hamiltonian Cycle:条件是简单回路(除最后一个结点以外只出现一次),并且相邻两点之间连通。
/** * Copyright(c) * All rights reserved. * Author : Mered1th * Date : 2019-02-28-00.13.48 * Description : A1122 */ #include<cstdio> #include<cstring> #include<iostream> #include<cmath> #include<algorithm> #include<string> #include<unordered_set> #include<map> #include<vector> #include<set> using namespace std; ; ; int G[maxn][maxn]; int n,m,k; int main(){ #ifdef ONLINE_JUDGE #else freopen("1.txt", "r", stdin); #endif scanf("%d%d",&n,&m); fill(G[],G[]+maxn*maxn,INF); int u,v; ;i<m;i++){ scanf("%d%d",&u,&v); G[u][v]=G[v][u]=; } scanf("%d",&k); int t,a; ;i<k;i++){ scanf("%d",&t); vector<int> temp; bool vis[maxn]={false}; bool flag=true; ;j<t;j++){ scanf("%d",&a); temp.push_back(a); } ]!=temp[temp.size()-]){ printf("NO\n"); continue; } ;x<temp.size()-;x++){ ]]!= || vis[temp[x]]==true){ printf("NO\n"); flag=false; break; } else{ vis[temp[x]]=true; } } if(flag==false) continue; ;j<n;j++){ if(vis[j]==false){ printf("NO\n"); flag=false; break; } } if(flag) printf("YES\n"); } ; }
1122 Hamiltonian Cycle (25 分)的更多相关文章
- 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 ...
- 1122. Hamiltonian Cycle (25)
The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...
- PAT甲题题解-1122. Hamiltonian Cycle (25)-判断路径是否是哈密顿回路
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789799.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- 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 ...
- 1122 Hamiltonian Cycle
题意:包含图中所有结点的简单环称为汉密尔顿环.给出无向图,然后给出k个查询,问每个查询是否是汉密尔顿环. 思路:根据题目可知,我们需要判断一下几个条件:(1).首先保证给定的环相邻两结点是连通的:(2 ...
- PAT1122: Hamiltonian Cycle
1122. Hamiltonian Cycle (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...
- PAT_A1122#Hamiltonian Cycle
Source: PAT A1122 Hamiltonian Cycle (25 分) Description: The "Hamilton cycle problem" is to ...
随机推荐
- 浅谈STM32L071硬件I2C挂死
STM32的IIC问题一直存在,在网上也被很多人吐槽,然而FAE告诉我,硬件IIC的问题在F1,F3,F4系列单片机存在,而在L0上已经解决了,然而这几天调试加密芯片和显示芯片,都是IIC芯片,却再一 ...
- java exception 01
问题:java.util.concurrentmodificationexception 背景:java thread 网上找到的出现的例子如下(项目中真实的code不便给出) public clas ...
- crash - JNI WARNING: input is not valid modified utf-8: illegal continuation byte
the key point is "Modified UTF-8" is not like "Regular UTF-8", a legal Rgular UT ...
- 每天进步一点点-写完睡觉-周一工作(java基本数据类型所占的字节和IO流读取的字符和字节)
- 开机或者安装系统时提示tsc: Fast TSC calibration failed解决方法
其实这个问题很简单,主要是时间不对的问题. 就拿我遇到这个问题来说.我是在vm虚拟机中安装了xenserver,然后在物理机安装xencenter连接xenserver,然后在安装新的虚拟机时遇到这个 ...
- [转]java虚拟机工作原理详解
一.类加载器 首先来看一下java程序的执行过程. 从这个框图很容易大体上了解java程序工作原理.首先,你写好java代码,保存到硬盘当中.然后你在命令行中输入 javac YourClassNam ...
- cpu怎么实现运算的
这么好玩的问题,没人回答说不过去.不懂电子元件的码农我要强答一发了.太学术的我不会,你们也听不懂.所以臭不要脸非常不严谨地科普一记.说错不许打我.另外关于这个问题,推荐<编码 (豆瓣)> ...
- java降低竞争锁的一些方法
序本文介绍一下提升并发可伸缩性的一些方式:减少锁的持有时间,降低锁的粒度,锁分段.避免热点域以及采用非独占的锁或非阻塞锁来代替独占锁. 减少锁的持有时间降低发生竞争可能性的一种有效方式就是尽可能缩短锁 ...
- eclipse配置maven后无法下载jar
1.检查网络拦截是否正常 2.进入maven依赖库根目录搜索出该目录下的*lastUpdated.properties文件并删除 然后就可以继续下载jar包了
- ML平台_饿了么实践
(转载至:https://zhuanlan.zhihu.com/p/28592540) 说到机器学习.大数据,大家听到的是 Hadoop 和 Spark 居多,它们跟 TensorFlow 是一个什么 ...