【2019暑期】【PAT甲级】1122 Hamiltonian Cycle (25 分)

48 篇文章 0 订阅

哈密顿路径也称作哈密顿链,指在一个图中沿边访问每个顶点恰好一次的路径。
欧拉回路是指不重复的走过所有路径的回路
哈密尔顿环是指不重复地走过所有的点,并且最后还能回到起点的回路。

题很简单
自己写的代码太啰嗦了,还不对
学习大佬的简洁写法,自己想的还是偏

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <string>
#include <cctype>
#include <vector>
#include <cstring>
#include <set>
using namespace std;

int n,m,k,p;
int node[210][210];

int main()
{
 	cin >> n >> m;
 	for(int i=1; i<=m; i++){
 		int a,b;
 		cin >> a >> b;
 		node[a][b] = node[b][a] = 1;
	}
	
	cin >> k;
 	for(int i=0; i<k; i++){
 		cin >> p;
 		vector<int> jud;
 		set<int> ans;
 		int key1 = 1,key2 = 1;
 		for(int j=0; j<p; j++){
 			int a;
 			cin >> a;
 			ans.insert(a);
 			jud.push_back(a);
		}
		if(ans.size()<n||jud[0]!=jud[p-1]||p-1>n) key1 = 0;
		for(int j=0; j<p-1; j++){
			if(node[jud[j]][jud[j+1]]==1) continue;
			else {
				key2 = 0;
				break;
			}
		}
		if(key1&&key2) cout << "YES" << endl;
		else cout << "NO" << endl;
	 }
 	
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值