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

————————————————————————————————

题目的意思是给出n个点m条边,对于每次查询判断是否是哈密顿路径

思路:根据查询,分别判断点数,首尾是否一致,是否每个点都遍历,路径是否联通

若均符合则YES否则NO

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std; #define LL long long
const int INF = 0x3f3f3f3f; int mp[500][500];
int a[500];
int main()
{
int n,m,x,y,q,k;
scanf("%d%d",&n,&m);
memset(mp,0,sizeof mp);
for(int i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
mp[x][y]=mp[y][x]=1;
}
scanf("%d",&q);
while(q--)
{
scanf("%d",&k);
for(int i=0;i<k;i++)
scanf("%d",&a[i]);
if(n==1&&k==1)
{
printf("YES\n");
continue;
}
if(k!=n+1||a[0]!=a[k-1])
{
printf("NO\n");
continue;
}
int flag=0;
set<int>s;
s.insert(a[0]);
for(int i=1;i<k;i++)
{
if(mp[a[i]][a[i-1]]==0)
{
flag=1;
break;
}
s.insert(a[i]);
}
if(s.size()!=n)
flag=1;
printf("%s\n",flag==0?"YES":"NO"); }
return 0;
}

  

PAT甲级 1122. Hamiltonian Cycle (25)的更多相关文章

  1. 1122 Hamiltonian Cycle (25 分)

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

  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)

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

  4. PAT甲题题解-1122. Hamiltonian Cycle (25)-判断路径是否是哈密顿回路

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789799.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  5. 1122 Hamiltonian Cycle (25 分)

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

  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甲级】1070 Mooncake (25 分)(贪心水中水)

    题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全 ...

  9. PAT甲级 1121. Damn Single (25)

    1121. Damn Single (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue "Dam ...

随机推荐

  1. SqlServer添加触发器死锁的原因

    之前遇到过SqlServer添加触发器死锁的情况,纠结了很长时间 最近发现原来是因为我在建表的时候,把id设成主键后,系统默认了加一个聚集的索引 就是聚集索引把表锁住了

  2. L1-006 连续因子(20)(思路+测试点分析)

    L1-006 连续因子(20 分) 一个正整数 N 的因子中可能存在若干连续的数字.例如 630 可以分解为 3×5×6×7,其中 5.6.7 就是 3 个连续的数字.给定任一正整数 N,要求编写程序 ...

  3. Eclipse中的maven项目搭建

    一.eclipse中的maven设置 1.打开“首选项”----> "maven"---->"Installations".用来查看maven的使用 ...

  4. mysql官网

    https://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/

  5. 客户端服务端web问题

    情况是这样: 我这边是C++socket/TCP服务器,然后有几万台客户端长连接我的服务器,这时,web端想对客户端进行参数设置,于是web端连到我的TCP服务器发来它想设置的客户端的ID和设置参数, ...

  6. IOS初级:UIScrollView & UIPageControl

    UIScrollView其实构建的就像一列很长的火车,每滑动一个屏幕,展示一节车厢. //主屏幕高度 #define kScreenHeight [UIScreen mainScreen].bound ...

  7. CentOS7安装ms8可能出现的错误

    参照<在Centos上安装MS8.0的详细过程>:http://muchong.com/html/201507/9145663.html 安装过程中如果库文件不全,会异常终止.报错情况如下 ...

  8. mybatis学习五 log4j

    1.  log4j(log for java)由 apache 推出的开源免费日志处理的类库.2. 为什么需要日志: 2.1 在项目中编写 System.out.println();输出到控制台,当项 ...

  9. 三连击(NOIP1998)

    题目链接:三连击 典型的打表题,但cgg今天不是教你怎么打表的,而是教你正解. 这题方法多样,比如递归求解也行,反正数据也不大. 在这里我提供另一种思路,我们枚举第一个数,即最小的一个数,然后分解它以 ...

  10. Django框架之Ajax和form组件

    一.Django框架之查漏补缺 1)models,字段概况 name = models.CharField(max_length=) age = models.IntegerField() price ...