7-4 汉密尔顿回路(25 分)

著名的“汉密尔顿(Hamilton)回路问题”是要找一个能遍历图中所有顶点的简单回路(即每个顶点只访问 1 次)。本题就要求你判断任一给定的回路是否汉密尔顿回路。

输入格式:

首先第一行给出两个正整数:无向图中顶点数 N(2 < N ≤ 200)和边数 M。随后 M 行,每行给出一条边的两个端点,格式为“顶点1 顶点2”,其中顶点从 1 到N 编号。再下一行给出一个正整数 K,是待检验的回路的条数。随后 K 行,每行给出一条待检回路,格式为:

n V​1​​ V​2​​ ⋯ V​n​​

其中 n 是回路中的顶点数,V​i​​ 是路径上的顶点编号。

输出格式:

对每条待检回路,如果是汉密尔顿回路,就在一行中输出”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】的更多相关文章

  1. 7-10 括号匹配(25 分) 【STL】

    7-10 括号匹配(25 分) 给定一串字符,不超过100个字符,可能包括括号.数字.字母.标点符号.空格,编程检查这一串字符中的( ) ,[ ],{ }是否匹配. 输入格式: 输入在一行中给出一行字 ...

  2. L2-007 家庭房产 (25 分)

    L2-007 家庭房产 (25 分)   给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产套数. 输入格式: 输入第一行给出一个正整数N(≤),随后N行,每行按下 ...

  3. L2-014 列车调度 (25 分)

    L2-014 列车调度 (25 分)   火车站的列车调度铁轨的结构如下图所示. 两端分别是一条入口(Entrance)轨道和一条出口(Exit)轨道,它们之间有N条平行的轨道.每趟列车从入口可以选择 ...

  4. 1122 Hamiltonian Cycle (25 分)

    1122 Hamiltonian Cycle (25 分) The "Hamilton cycle problem" is to find a simple cycle that ...

  5. 1085 Perfect Sequence (25 分)

    1085 Perfect Sequence (25 分) Given a sequence of positive integers and another positive integer p. T ...

  6. 1036 Boys vs Girls (25分)(水)

    1036 Boys vs Girls (25分)   This time you are asked to tell the difference between the lowest grade o ...

  7. PTA数据结构与算法题目集(中文) 7-37 模拟EXCEL排序 (25 分)

    PTA数据结构与算法题目集(中文)  7-37 模拟EXCEL排序 (25 分) 7-37 模拟EXCEL排序 (25 分)   Excel可以对一组纪录按任意指定列排序.现请编写程序实现类似功能. ...

  8. PTA - - 06-图1 列出连通集 (25分)

    06-图1 列出连通集   (25分) 给定一个有NN个顶点和EE条边的无向图,请用DFS和BFS分别列出其所有的连通集.假设顶点从0到N-1N−1编号.进行搜索时,假设我们总是从编号最小的顶点出发, ...

  9. 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)

    01-复杂度2 Maximum Subsequence Sum   (25分) Given a sequence of K integers { N​1​​,N​2​​, ..., N​K​​ }. ...

随机推荐

  1. mysql if()一个奇怪的问题

    看起来一切正常......... 但是当使用不等于时 SELECT IF(1!=NULL,1,2) 居然返回2 SELECT IF(1!=NULL,1,2) >2 -- ------------ ...

  2. C# 调用API接口处理公共类 自带JSON实体互转类

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net ...

  3. Spring 小记

    本作品由Man_华创作,采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可.基于http://www.cnblogs.com/manhua/上的作品创作. 使用STS新建spr ...

  4. 获取URL的name值 getUrl(url,name) 传入url和key 得到key对应的value

    <body> <script type="text/javascript"> var url = "http://192.168.1.82:802 ...

  5. VueJS事件处理器v-on:事件修饰符&按键修饰符

    事件修饰符 Vue.js 为 v-on 提供了事件修饰符来处理 DOM 事件细节,如:event.preventDefault() 或 event.stopPropagation(). Vue.js通 ...

  6. 获取input光标的x和y轴

    http://blog.csdn.net/kingwolfofsky/article/details/6586029 index.html <!DOCTYPE html> <html ...

  7. 【转】AngularJs 弹出框 model(模态框)

    原文转至 http://blog.csdn.net/violet_day/article/details/17170585 $modal是一个可以迅速创建模态窗口的服务,创建部分页,控制器,并关联他们 ...

  8. 简单手机端头部设置 及css代码

    <html> <head> <title>今日报表</title> <meta http-equiv="Content-Type&quo ...

  9. 请实现一个函数用来匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但是与"aa.a"和"ab*a"均不匹配

    // test20.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...

  10. Spark Streaming和Kafka整合开发指南(二)

    在本博客的<Spark Streaming和Kafka整合开发指南(一)>文章中介绍了如何使用基于Receiver的方法使用Spark Streaming从Kafka中接收数据.本文将介绍 ...