PAT 1122 Hamiltonian Cycle[比较一般]
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 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
题目大意:判断给出的路径是否是哈密顿回路,哈密顿回路是一个简单回路,包含图中的每一个点,
我的AC:
#include <iostream>
#include <vector>
#include<cstdio>
#include <map>
using namespace std;
#define inf 9999 int g[][];
int vis[];
int main() {
int n,m,f,t;
cin>>n>>m;
fill(g[],g[]+*,inf);
for(int i=;i<m;i++){
cin>>f>>t;
g[f][t]=;
g[t][f]=;
}
int k,ct;
cin>>k;
while(k--){
fill(vis,vis+,);
cin>>ct;
vector<int> path(ct);
for(int i=;i<ct;i++){
cin>>path[i];
}
if(path[]!=path[ct-]){//首先需要保证两者是相同的。
cout<<"NO\n";continue;
}
bool flag=false;
for(int i=;i<ct-;i++){
if(g[path[i]][path[i+]]==inf){//如果两点之间,没有路径。
cout<<"NO\n";
flag=true;
break;
}
if(vis[path[i+]]==){//如果重复访问那么就不是简单路径,
cout<<"NO\n";
flag=true;break;
}
vis[path[i+]]=;
// cout<<path[i+1]<<'\n';
}
if(!flag){//这里还需要判断是否是所有的点都已经访问过。
bool fg=false;
for(int i=;i<=n;i++){//这里是从1开始判断啊喂!!!
if(vis[i]==){
cout<<"NO\n";
fg=true;break;
}
}
if(!fg)cout<<"YES\n";
}
}
return ;
}
//本来很简单的一道题,两个周没打算法代码了,生疏了。
1.点标号是从1开始的所以 最后判断所有的点是否被遍历过,是从1开始循环的,
2.比较简单,就是几个判断情况,使用邻接矩阵存储图,不是邻接表。
PAT 1122 Hamiltonian Cycle[比较一般]的更多相关文章
- PAT 1122 Hamiltonian Cycle
The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...
- PAT甲级 1122. Hamiltonian Cycle (25)
1122. Hamiltonian Cycle (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...
- 1122 Hamiltonian Cycle (25 分)
1122 Hamiltonian Cycle (25 分) The "Hamilton cycle problem" is to find a simple cycle that ...
- 1122 Hamiltonian Cycle (25 分)
1122 Hamiltonian Cycle (25 分) The "Hamilton cycle problem" is to find a simple cycle that ...
- PAT A1122 Hamiltonian Cycle (25 分)——图遍历
The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...
- 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特别不喜欢那些随便转载别人的原创文章又不给 ...
- 1122 Hamiltonian Cycle
题意:包含图中所有结点的简单环称为汉密尔顿环.给出无向图,然后给出k个查询,问每个查询是否是汉密尔顿环. 思路:根据题目可知,我们需要判断一下几个条件:(1).首先保证给定的环相邻两结点是连通的:(2 ...
- PAT1122: Hamiltonian Cycle
1122. Hamiltonian Cycle (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...
随机推荐
- 向服务器发送josn字符串,服务器端解析
<script type="text/javascript"> $(function () { $("#btnsave").click(functi ...
- bootstrap基础学习五篇
bootstrap表格 Bootstrap 提供了一个清晰的创建表格的布局.下表列出了 Bootstrap 支持的一些表格元素: 标签 描述 <table> 为表格添加基础样式. < ...
- python3----scrapy(笔记)
import scrapy import sys # import io # sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='gb ...
- ipc 进程间通讯的AIDL
1.什么是aidl:aidl是 Android Interface definition language的缩写,一看就明白,它是一种android内部进程通信接口的描述语言,通过它我们可以定义进程间 ...
- Codeforces Round #359 (Div. 2) C. Robbers' watch 搜索
题目链接:http://codeforces.com/contest/686/problem/C题目大意:给你两个十进制的数n和m,选一个范围在[0,n)的整数a,选一个范围在[0,m)的整数b,要求 ...
- Firefox 新增容器标签:可同时登录多个用户
Mozilla昨天在Firefox夜间构建版50.0a1中增加了一个名为“容器标签Container Tabs”的实验性功能. Mozilla的工程师称,该功能可以将用户的浏览会话分到不同的容器中.这 ...
- <span>和<div>标签的隐藏和显示切换
<div class="axb"> <span id="tipStep1" class="fl" >第一步显示< ...
- 170428、centos6.5安装Subversion + Apache +Jsvnadmin
服务器地址:192.168.0.112 root用户操作 建议 安装前 更 新操作系统 # yum update 更新完成后重启 # reboot 安装 装 apache # yum install ...
- Java+selenium+Fitnesse
刚开始接触selenium是进公司后,老大给我们培训了一下UI自动化(其实也不叫培训啦,就是让我们知道有这么个东西吧,我这么说,老大看到得打人了,哈哈).要进行自动化测试,当然就得搭建一个自动化测试框 ...
- Spring 拦截器的使用
一.Web.xml配置 在Web.xml 配置Spring核心控制器DispatcherServlet接收所有请求 <servlet> <servlet-name>spring ...