哈密顿回路问题是找到一个包含图中每个顶点的简单回路。
这样的回路称为“哈密顿回路”。
在本题中,你需要做的是判断给定路径是否为哈密顿回路。
输入格式
第一行包含一个整数 NN 表示顶点数,一个整数 MM 表示给定无向图中的边数。
接下来 MM 行,每行包含两个整数 a,ba,b,表示点 aa 和 bb 之间存在一条边。
所有顶点编号从 11 到 NN。
再一行给出整数 KK,表示询问次数。
接下来 KK 行,每行包含一个询问,格式如下:
nn V1V1 V2V2 … VnVn
nn 表示给定路径经过的点的数目,ViVi 是路径中经过的点。
输出格式
对于每个询问,如果是哈密顿回路则在一行输出 YES,否则输出 NO。
数据范围
2<N≤2002<N≤200,
N−1≤M≤N(N−1)2N−1≤M≤N(N−1)2,
1≤K≤10001≤K≤1000,
1≤n≤4101≤n≤410
输入样例:
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
输出样例:
YES
NO
NO
NO
YES
NO
#include <iostream>
#include <cstring>
using namespace std;
const int N = 210;
int n, m;
bool g[N][N], st[N];
int nodes[N * 2];
bool check(int cnt)
{
if (nodes[0] != nodes[cnt - 1] || cnt != n + 1) return false;//点的数量必须为n+1
memset(st, 0, sizeof st);
for (int i = 0; i < cnt - 1; i ++ )
{
st[nodes[i]] = true;
if (!g[nodes[i]][nodes[i + 1]])
return false;
}
for (int i = 1; i <= n; i ++ )
if (!st[i]) //必须每个点都包括
return false;
return true;
}
int main()
{
cin >> n >> m;
while (m -- )
{
int a, b;
cin >> a >> b;
g[a][b] = g[b][a] = true;
}
int k;
cin >> k;
while (k -- )
{
int cnt;
cin >> cnt;
for (int i = 0; i < cnt; i ++ ) cin >> nodes[i];
if (check(cnt)) puts("YES");
else puts("NO");
}
return 0;
}