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 }. ...
随机推荐
- 让arclist标签也支持currentstyle属性 完美解决
1.查找到: $channelid = $ctag->GetAtt('channelid'); 在下面插入:$currentstyle = $ctag->GetAtt('currentst ...
- Dapper Sqlpara where in
Mark一下:string sql = "SELECT * FROM SomeTable WHERE id IN @ids" var results = conn.Query(sq ...
- SQLite创建表并加入数据
- (void)viewDidLoad { [super viewDidLoad]; //创建表 [self creatTable]; //插入数据 [self insertTable]; } // ...
- 【重点突破】——Drag&Drop拖动与释放
一.引言 在学习HTML5新特性的时候,学到了Drag&Drop这两种拖放API,这里根据拖动的是“源对象”还是“目标对象”做两个小练习,主要是为了理解与应用HTML5为拖放行为提供的7个事件 ...
- tcpdump命令使用详解
阅读(226) 一:命令介绍: tcpdump,用简单的语言概括就是dump the traffic on a network,是一个运行在linux平台可以根据使用者需求对网络上传输的数据包进行捕获 ...
- falsh,.swf文件修改z-index
<object style="z-index:-1;"> <param name="wmode" value="transparen ...
- shell脚本安装ntp server 服务
##############################Deploy ntp server ######################## echo "start deploy ntp ...
- vscode Python Pylint(代码检测插件)
暑假刚开始想了解一下Python,使用vscode进行编写,根据vscode 的提示安装了一些不知道干啥的插件,编写过程中提示说 "Linter pylint is not install ...
- 文件I/O之C标准库函数和系统库函数差别
1.首先C标准库函数是工作在系统库函数之上的.C标准库函数在读写文件时候都有一个文件流指针.FILE*fp=NULL;// fp=fopen(F_PATH,"r"); fp文件流指 ...
- msgsnd的一个小问题
今天写了一个System V消息队列的小样例.定义了一个例如以下的结构体: #define MSG_SIZE 8192 struct request { long mtype; int client_ ...