pat甲级1122. Hamiltonian Cycle (25)
欢迎访问我的pat甲级题解目录哦https://blog.csdn.net/ri*qi/article/details/79958195
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 1Sample Output:
YES NO NO NO YES NO
算法设计:
一个序列是哈密顿环必须满足下列条件,缺一不可:
- 必须能构成一个环
- 必须经过图中所有节点
- 除了首尾这对相同结点可以出现2次外,其余节点在环中只能经过1次
邻接表版c++代码:
#include<bits/stdc++.h>
using namespace std;
int N,M,K;
vector<int>graph[205];//图
bool Hamiltonian(const vector<int>&v){//判断一个序列是否是哈密顿环
if(v.size()!=N+1||v.front()!=v.back())//判断结点个数是否为图中结点总数+1、判断首尾结点是否相同
return false;
unordered_set<int>s;
for(int i=0;i<v.size()-1;++i){//判断除尾节点外每个结点是否只出现一次
if(s.find(v[i])!=s.cend())
return false;
s.insert(v[i]);
}
for(int i=1;i<v.size();++i)//判断序列中每两个相邻数之间是否有路径
if(find(graph[v[i-1]].cbegin(),graph[v[i-1]].cend(),v[i])==graph[v[i-1]].cend())
return false;
return true;
}
int main(){
scanf("%d%d",&N,&M);
while(M--){
int a,b;
scanf("%d%d",&a,&b);
graph[a].push_back(b);
graph[b].push_back(a);
}
scanf("%d",&K);
while(K--){
scanf("%d",&M);
vector<int>A(M);
for(int i=0;i<M;++i)
scanf("%d",&A[i]);
printf("%s\n",Hamiltonian(A)?"YES":"NO");
}
return 0;
}
邻接矩阵版c++代码:
#include<bits/stdc++.h>
using namespace std;
int N,M,K;
bool graph[205][205];//图
bool Hamiltonian(const vector<int>&v){//判断一个序列是否是哈密顿环
if(v.size()!=N+1||v.front()!=v.back())//判断结点个数是否为图中结点总数+1、判断首尾结点是否相同
return false;
unordered_set<int>s;
for(int i=0;i<v.size()-1;++i){//判断除尾节点外每个结点是否只出现一次
if(s.find(v[i])!=s.cend())
return false;
s.insert(v[i]);
}
for(int i=1;i<v.size();++i)//判断序列中每两个相邻数之间是否有路径
if(!graph[v[i-1]][v[i]])
return false;
return true;
}
int main(){
scanf("%d%d",&N,&M);
while(M--){
int a,b;
scanf("%d%d",&a,&b);
graph[a][b]=graph[b][a]=true;
}
scanf("%d",&K);
while(K--){
scanf("%d",&M);
vector<int>A(M);
for(int i=0;i<M;++i)
scanf("%d",&A[i]);
printf("%s\n",Hamiltonian(A)?"YES":"NO");
}
return 0;
}
推荐阅读
-
PAT 甲级真题 1006 Sign In and Sign Out (25分) python实现
-
PAT 甲级 1122. Hamiltonian Cycle (25)
-
【PAT】1122. Hamiltonian Cycle (25)
-
PAT甲1122 Hamiltonian Cycle(25 分)
-
[Python](PAT)1122 Hamiltonian Cycle(25 分)
-
A1122 Hamiltonian Cycle (25 分| 图论,附详细注释,逻辑分析)
-
PAT 1122 Hamiltonian Cycle
-
PAT (Advanced Level) Practice - 1122 Hamiltonian Cycle(25 分)
-
PAT甲级 1122. Hamiltonian Cycle (25)
-
【PAT甲级】【1002】 A+B for Polynomials (25分)