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 V​1​​ V​2​​ ... V​n​​

where n is the number of vertices in the list, and V​i​​'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[比较一般]的更多相关文章

  1. PAT 1122 Hamiltonian Cycle

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

  2. PAT甲级 1122. Hamiltonian Cycle (25)

    1122. Hamiltonian Cycle (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...

  3. 1122 Hamiltonian Cycle (25 分)

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

  4. 1122 Hamiltonian Cycle (25 分)

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

  5. PAT A1122 Hamiltonian Cycle (25 分)——图遍历

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

  6. 1122. Hamiltonian Cycle (25)

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

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

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

  8. 1122 Hamiltonian Cycle

    题意:包含图中所有结点的简单环称为汉密尔顿环.给出无向图,然后给出k个查询,问每个查询是否是汉密尔顿环. 思路:根据题目可知,我们需要判断一下几个条件:(1).首先保证给定的环相邻两结点是连通的:(2 ...

  9. PAT1122: Hamiltonian Cycle

    1122. Hamiltonian Cycle (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...

随机推荐

  1. DELETE 语句用于删除表中的行。

    DELETE FROM 表名称 WHERE 列名称 = 值

  2. android DialogFragment 回调到 Fragment

    google 从3.0開始就引入了 Fragment 概念,用 Fragment 取代多 Activity,假设你还停留在 Activity 时代,那你就面壁去吧! Fragment 是好用,可是又几 ...

  3. 【转】Native Thread for Win32 C- Creating Processes(通俗易懂,非常好)

    http://www.bogotobogo.com/cplusplus/multithreading_win32C.php To create a new process, we need to ca ...

  4. 工作流JBPM_day01:1-说明_MyProcessDesigner_流程设计器

    工作流JBPM_day01:1-说明 先只做请假功能,怎么做? (请假可以和考勤整合到一起) 1,银行(拿号---叫号---办理) 2,餐馆(点菜---上菜---结账) 3,网购(下订单--配送--收 ...

  5. C++变量命名方案

    变量命名方案和函数命名方案一样,也有很多话题可供讨论.确实,该主题会引发一些最尖锐的反对意见.同样,和函数名称一样,只要变量名合法,C++编译器就不会介意,但是一致/精确的个人命名约定是很有帮助的.与 ...

  6. Http协议原理解析第一篇

    一:http的由来: OSI模型把网络通信分成七层:物理层.数据链路层.网络层.传输层.会话层.表示层和应用层,对于开发网络应用人员来说,一般把网络分成五层,这样比较容易理解.这五层为:物理层.数据链 ...

  7. ios 让两个tableView同时处于选中状态

    - (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath { [_l ...

  8. dbForge mysql数据库比对

    Comparison选项卡,新建一个表结构比较, (将source库的表结构变化应用到target库) 下面示例中,source用positec_uat,     target用positec_pro ...

  9. 有关弱类型意识、DOM、动态语言与函数式编程

    一.弱类型意识  js变量是没有类型的 var a =1;   //a 就是一个变量  不要提类型 变量可以赋予任何类型的值,类型仅仅是值得性质  与变量无关   js 的基本类型 变量未赋值时,其值 ...

  10. HQL的第一个程序

    使用HQL查询数据库: 分为以下几个步骤 1获取query对象 //1获取query对象 String hql="FROM Employee e where e.salary>?&qu ...