# 7-4 汉密尔顿回路

### 输入样例：

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>
#include<vector>

using namespace std;

bool judge(int N, int flag[]) {
for (int i = 1; i <= N; i++) {
if (flag[i] == 0) {
return false;
}
}
return true;
}

int main() {
int g[210][210];
int flag[210];
int N, m;
cin >> N >> m;
memset(g, 0, sizeof(g));
memset(flag, 0, sizeof(flag));
int x, y;
for (int i = 1; i <= N; i++) {
g[i][i] = 1;
}
for (int i = 1; i <= m; i++) {
cin >> x >> y;
g[x][y] = 1;
g[y][x] = 1;
}
int k;
cin >> k;
int n, num;
vector<int> v;//用vector存放这条路线经过的顶点
for (int i = 0; i < k; i++) {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> num;
v.push_back(num);
}
if (n != N + 1) {
cout << "NO" << endl;
}
else {
for (int i = 0; i < n-1; i++) {
flag[v[i]] = 1;
if (g[v[i]][v[i + 1]] != 1) {
cout << "NO" << endl;
break;
}
}
if (judge(N, flag)&&v[0]==v[n-1]) {//因为要形成回路，所以开头的节点和结尾的节点必须一样
cout << "YES" << endl;
}
else {
cout << "NO" << endl;
}
}
memset(flag, 0, sizeof(flag));//每一条路线结束之后要清空vector和flag[]
for (int i = 0; i < n; i++) {
v.pop_back();
}
}
return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120