这题我才知道vector居然有v.front() 和v.back()就是首尾元素的值
题意简要介绍就是,先通过给一些顶点和边的信息给定一个图,然后再给出几组路径,要你判断这组路径是不是哈密顿回路
哈密顿回路是指能包含此图所有点的简单回路
有这么一些限制条件:
- 待查询的路径path的个数必须是 n+1
- 路径必须是回路,即首尾值相等
- 结点不能重复且路径联通
思路:用一个isInpath数组记录出现的结点,如果重复就return 0 ,否则结束时返回1
#include <cstdio>
#include <iostream>
#include <string>
#include <map>
#include <set>
#include <cstring>
#include <vector>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <ctype.h>
using namespace std;
const int maxn = 210;
int n, m, k;
int G[maxn][maxn] = { 0 };
int isInpath[maxn];
int judgeHamiltonianCycle(vector<int> &path) {
if (path.size() != n + 1 || path.front() != path.back()) {
return 0;//如果路径节点个数不等于n+1或者首尾节点不同,则非哈密顿回路
}
else {
memset(isInpath, 0, sizeof(isInpath));
for (int j = 1; j < path.size(); j++) {
if (isInpath[path[j]] || !G[path[j - 1]][path[j]]) {
return 0;//如果节点重复,或者边不存在,那么非哈密顿回路
}
isInpath[path[j]] = 1;
}
}
return 1;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v);
G[u][v] = G[v][u] = 1;
}
scanf("%d", &k);
for (int i = 0; i < k; i++) {
int num, tempNode;
scanf("%d", &num);
vector<int> path;//路径path
for (int j = 0; j < num; j++) {
scanf("%d", &tempNode);
path.push_back(tempNode);
}
if (judgeHamiltonianCycle(path)) {
printf("YES\n");
}
else {
printf("NO\n");
}
}
return 0;
}