A1122 Hamiltonian Cycle [哈密顿环]

在这里插入图片描述

#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<set>
using namespace std;
int main()
{
	int n, m, cnt, k, a[210][210] = { 0 };
	cin >> n >> m; int t1, t2;
	for (int i = 0; i < m; i++)
	{
		cin >> t1 >> t2;
		a[t1][t2] = a[t2][t1] = 1;
	}
	cin >> cnt;
	while (cnt--)
	{
		cin >> k;
		vector<int>v(k);
		set<int>s;
		int flag1 = 1, flag2 = 1;
		for (int i = 0; i < k; i++)
		{
			cin >> v[i];
			s.insert(v[i]);
		}
		if (s.size() != n || k - 1 != n || v[0] != v[k - 1]) flag1 = 0;
		for (int i = 0; i < k - 1; i++)
		{
			if (a[v[i]][v[i + 1]] == 0) flag2 = 0;
		}
		if (flag1 && flag2)
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}
}
posted @ 2020-08-11 08:26  _Hsiung  阅读(58)  评论(0编辑  收藏  举报