7-4 汉密尔顿回路(25 分) 【STL】
7-4 汉密尔顿回路(25 分)
著名的“汉密尔顿(Hamilton)回路问题”是要找一个能遍历图中所有顶点的简单回路(即每个顶点只访问 1 次)。本题就要求你判断任一给定的回路是否汉密尔顿回路。
输入格式:
首先第一行给出两个正整数:无向图中顶点数 N(2 < N ≤ 200)和边数 M。随后 M 行,每行给出一条边的两个端点,格式为“顶点1 顶点2”,其中顶点从 1 到N 编号。再下一行给出一个正整数 K,是待检验的回路的条数。随后 K 行,每行给出一条待检回路,格式为:
n V1 V2 ⋯ Vn
其中 n 是回路中的顶点数,Vi 是路径上的顶点编号。
输出格式:
对每条待检回路,如果是汉密尔顿回路,就在一行中输出”YES”,否则输出”NO”。
输入样例:
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
思路
对于每一条回路 它的要求是 要遍历到所有的点 并且 每个点 只能 访问一次 而且 相邻的两点之间 是有边的
然后 先判断 点的个数 是不是 满足 n + 1 (因为最后要回到原点) 如果不满足 直接就 NO 了
然后 再判断 点有没有重复 和 两点之间是否有边 是否有边 可以用二维数组 标记 Map[i][j] 用 bool 值表示 i 和 j 之间 是否有边
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
#define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss;
const double PI = 3.14159265358979323846264338327;
const double E = exp(1);
const double eps = 1e-30;
const int INF = 0x3f3f3f3f;
const int maxn = 2e2 + 5;
const int MOD = 1e9 + 7;
int Map[maxn][maxn];
int main()
{
int n, m;
scanf("%d%d", &n, &m);
int x, y;
CLR(Map);
for (int i = 1; i <= n; i++)
Map[i][i] = 1;
for (int i = 0; i < m; i++)
{
scanf("%d%d", &x, &y);
Map[x][y] = 1;
Map[y][x] = 1;
}
cin >> m;
int k;
map <int, int> q;
for (int i = 0; i < m; i++)
{
q.clear();
cin >> k;
int flag = 1;
int start, num;
cin >> start;
int vis = start;
if (k == 1)
{
if (Map[start][start] == 1 && n == 1)
printf("YES\n");
else
printf("NO\n");
}
else
{
for (int j = 1; j < k; j++)
{
scanf("%d", &num);
if (q[num])
flag = 0;
if (Map[vis][num] == 0)
flag = 0;
vis = num;
q[num] = 1;
}
if (k != n + 1)
flag = 0;
if (vis != start)
flag = 0;
if (flag)
printf("YES\n");
else
printf("NO\n");
}
}
}
7-4 汉密尔顿回路(25 分) 【STL】的更多相关文章
- 7-10 括号匹配(25 分) 【STL】
7-10 括号匹配(25 分) 给定一串字符,不超过100个字符,可能包括括号.数字.字母.标点符号.空格,编程检查这一串字符中的( ) ,[ ],{ }是否匹配. 输入格式: 输入在一行中给出一行字 ...
- L2-007 家庭房产 (25 分)
L2-007 家庭房产 (25 分) 给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产套数. 输入格式: 输入第一行给出一个正整数N(≤),随后N行,每行按下 ...
- L2-014 列车调度 (25 分)
L2-014 列车调度 (25 分) 火车站的列车调度铁轨的结构如下图所示. 两端分别是一条入口(Entrance)轨道和一条出口(Exit)轨道,它们之间有N条平行的轨道.每趟列车从入口可以选择 ...
- 1122 Hamiltonian Cycle (25 分)
1122 Hamiltonian Cycle (25 分) The "Hamilton cycle problem" is to find a simple cycle that ...
- 1085 Perfect Sequence (25 分)
1085 Perfect Sequence (25 分) Given a sequence of positive integers and another positive integer p. T ...
- 1036 Boys vs Girls (25分)(水)
1036 Boys vs Girls (25分) This time you are asked to tell the difference between the lowest grade o ...
- PTA数据结构与算法题目集(中文) 7-37 模拟EXCEL排序 (25 分)
PTA数据结构与算法题目集(中文) 7-37 模拟EXCEL排序 (25 分) 7-37 模拟EXCEL排序 (25 分) Excel可以对一组纪录按任意指定列排序.现请编写程序实现类似功能. ...
- PTA - - 06-图1 列出连通集 (25分)
06-图1 列出连通集 (25分) 给定一个有NN个顶点和EE条边的无向图,请用DFS和BFS分别列出其所有的连通集.假设顶点从0到N-1N−1编号.进行搜索时,假设我们总是从编号最小的顶点出发, ...
- 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)
01-复杂度2 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1,N2, ..., NK }. ...
随机推荐
- selinue引起的ssh连接错误
在客户端执行ssh依然报错: Permission denied (publickey,gssapi-keyex,gssapi-with-mic). 在这个页面不小心看到了原因: http://ser ...
- mac 配置sencha touch环境
1 安装 java 2 安装 node js 为使用npm作准备 3 用npm命令安装 cordova npm install -g cordova
- 如何从底层调试docker
How the docker container creation process works (from docker run to runc) Over the past few months I ...
- API接口管理工具postman等
国外 postman Swagger:国外比较流行的一款管理工具,英文配置,需要一定的英文基础和服务器搭建基础,学习成本较高. 国内 Apizza: 风格类似postman,熟悉postman的会比较 ...
- 【剑指Offer面试题】 九度OJ1368:二叉树中和为某一值的路径
题目链接地址: http://ac.jobdu.com/problem.php? pid=1368 题目1368:二叉树中和为某一值的路径 时间限制:1 秒内存限制:32 兆特殊判题:否提交:2252 ...
- vector(可变数组) 用于UDP通信
头文件: #include<vector.h> 然后,声明并初始化vctor数组. vector<char> str(len); 其中len可以是变量或者常量.(其实用常量就 ...
- 【转载】Asp.Net页面生命周期
一.什么是Asp.Net页面生命周期 当我们在浏览器地址栏中输入网址,回车查看页面时,这时会向服务器端(IIS)发送一个request请求,服务器就会判断发送过来的请求页面, 完全识别 HTTP 页 ...
- hdu 2814 Interesting Fibonacci
pid=2814">点击此处就可以传送 hdu 2814 题目大意:就是给你两个函数,一个是F(n) = F(n-1) + F(n-2), F(0) = 0, F(1) = 1; 还有 ...
- Hibernate学习一----------Hibernate初实现
© 版权声明:本文为博主原创文章,转载请注明出处 ORM(Object/Relationship Mapping):对象/关系映射 - 利用面向对象思想编写的数据库应用程序最终都是把对象信息保存在关系 ...
- 对‘TIFFReadDirectory@LIBTIFF_4.0’未定义的引用-------------- 解决办法
ABLE_DEPRECATED' is defined [-Winvalid-pch] //usr/lib/libvtkIO.so.5.10:对‘TIFFReadDirectory@LIBTIFF_4 ...