哈密顿回路
需要满足四个条件:
using namespace std;
const int N = 210;
bool g[N][N], st[N]; int nodes[N * 2]; int n, m;
bool check(int cnt) { if (cnt != (n + 1) || nodes[0] != nodes[cnt - 1]) { return false; }
memset(st, false, 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] == false) {
return false;
}
}
return true;
}
int main() {
#ifdef SUBMIT
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
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");
}
#ifdef SUBMIT
long _end_time = clock();
printf("\n\ntime = %ld ms", _end_time - _begin_time);
#endif
return 0;
} ```