hdu 4337 King Arthur's Knights (Hamilton)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2752 Accepted Submission(s): 1086
Special Judge
I am the bone of my sword. Steel is my body, and the fire is my blood.
- from Fate / Stay Night
You must have known the legend of King Arthur and his knights of the round table. The round table has no head, implying that everyone has equal status. Some knights are close friends with each other, so they prefer to sit next to each other.
The first line of each test case contains two integers N (3 <= N <= 150) and M, indicating that there are N knights and M relationships in total. Then M lines followed, each of which contains two integers ai and bi (1 <= ai, bi <= n, ai != bi), indicating that knight ai and knight bi are close friends.
For each test case, output one line containing N integers X1, X2, ..., XN separated by spaces, which indicating an round table arrangement. Please note that XN and X1 are also considered adjacent. The answer may be not unique, and any correct answer will be OK. If there is no solution exists, just output "no solution".
3 3
1 2
2 3
1 3
4 4
1 4
2 4
2 3
1 3
1 2 3
1 4 2 3
C/C++ (1):
#include <map>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <climits>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
const int my_max = ; int n, m, a, b, my_map[my_max][my_max], my_ans[my_max], my_book[my_max]; bool my_hamilton()
{
int pos = ;
my_ans[pos ++] = , my_book[] = ;
while (~pos)
{
my_ans[pos] ++;
while (my_ans[pos] < n)
if (!my_book[my_ans[pos]] && my_map[my_ans[pos - ]][my_ans[pos]]) break;
else my_ans[pos] ++;
if (my_ans[pos] < n && pos == n - && my_map[my_ans[pos]][my_ans[]]) return ;
else if (my_ans[pos] < n && pos < n - ) my_book[my_ans[pos ++]] = ;
else
{
my_ans[pos --] = -;
my_book[my_ans[pos]] = ;
}
}
return false;
} int main()
{
while(~scanf("%d%d", &n, &m))
{
memset(my_map, , sizeof(my_map));
memset(my_book, , sizeof(my_book));
memset(my_ans, -, sizeof(my_ans)); for (int i = ; i < m; ++ i)
{
scanf("%d%d", &a, &b);
--a, --b;
my_map[a][b] = my_map[b][a] = ;
} if (my_hamilton())
for (int i = ; i < n; ++ i)
printf("%d%c", my_ans[i] + , i == n - ? '\n' : ' ');
else
printf("no solution\n");
} return ;
}
C/C++(2):
#include <map>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <climits>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
const int MAX = ; int mp[MAX][MAX], n, m, a, b, ans[MAX], book[MAX], pos; bool hamilton()
{
memset(book, , sizeof(book));
memset(ans, , sizeof(ans));
pos = , book[] = ;
while (~pos)
{
ans[pos] ++;
while (ans[pos] < n)
if (!book[ans[pos]] && mp[ans[pos - ]][ans[pos]]) break;
else ans[pos] ++;
if (ans[pos] < n && pos == n - && mp[ans[pos]][ans[]]) return true;
else if (ans[pos] < n && pos < n - ) book[ans[pos ++]] = ;
else
{
ans[pos --] = ;
book[ans[pos]] = ;
}
}
return false;
} int main()
{
while (~scanf("%d%d", &n, &m))
{
memset(mp, , sizeof(mp));
while (m --)
{
scanf("%d%d", &a, &b);
-- a, -- b;
mp[a][b] = mp[b][a] = ;
} if (hamilton())
{
int temp = n - ;
for (int i = ; i < temp; ++ i)
printf("%d ", ans[i] + );
printf("%d\n", ans[temp] + );
}
else
printf("no solution\n");
}
return ;
}
hdu 4337 King Arthur's Knights (Hamilton)的更多相关文章
- HDU 4337 King Arthur's Knights 它输出一个哈密顿电路
n积分m文章无向边 它输出一个哈密顿电路 #include <cstdio> #include <cstring> #include <iostream> usin ...
- hdu4337 King Arthur's Knights
King Arthur's Knights Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- POJ3682 King Arthur's Birthday Celebration
King Arthur is an narcissist who intends to spare no coins to celebrate his coming K-th birthday. Th ...
- poj-3682 King Arthur's Birthday Celebration
C - King Arthur's Birthday Celebration POJ - 3682 King Arthur is an narcissist who intends to spare ...
- HDU 5642 King's Order dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5642 King's Order Accepts: 381 Submissions: 1361 ...
- HDU 5644 King's Pilots 费用流
King's Pilots 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5644 Description The military parade w ...
- HDU 5643 King's Game 打表
King's Game 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5643 Description In order to remember hi ...
- HDU 5642 King's Order 动态规划
King's Order 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5642 Description After the king's speec ...
- HDU 5640 King's Cake GCD
King's Cake 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5640 Description It is the king's birthd ...
随机推荐
- 《Java并发编程实战》读书笔记-第4章 对象的组合
设计线程安全的类 三个基本要素: 找出构成对象状态的所有变量 找出约束状态变量的不变性条件 建立对象状态的并发访问管理策略 实例封闭 将数据封装在对象内部,可以将数据的访问限制在对象的方法上,从而更容 ...
- Java Stream函数式编程图文详解(二):管道数据处理
一.Java Stream管道数据处理操作 在本号之前发布的文章<Java Stream函数式编程?用过都说好,案例图文详解送给你>中,笔者对Java Stream的介绍以及简单的使用方法 ...
- win32API多线程编程
win32线程API 在Windows平台下可以通过Windows的线程库来实现多线程编程. 对于多线程程序可以使用Visual Studio调试工具进行调试,也可以使用多核芯片厂家的线程分析调试工具 ...
- Jedis运用scan删除正则匹配的key
jedis运用scan删除正则匹配的key 我们都知道用keys *进行查询key的时候会进行堵塞,导致redis整体不可用,而使用scan命令则不会. RedisServiceImpl中sca ...
- VUE图片剪辑插件 React图片剪辑插件
React图片剪辑插件: https://github.com/roadmanfong/react-cropper React图片剪辑插件: https://github.com/xyxiao001/ ...
- 不同的phper该如何区别使用swoole和workerman?
那么我们该怎样去区别应用swoole和workerman? workerman workerman纯php写的,swoole是php的c扩展,性能肯定更高,百度.腾 ...
- webpack 4.x 初级学习记录
首先声明下,本人不擅长文字表达,文采不行,所以文章中文字较少,请看代码,初级学习,不足之处请多多指教 GitHub链接 看云kancloud [TOC] webpack 4.x 安装 首先需要在全局中 ...
- 基于appium实现的线性代码引用unittest单元测试框架
在前段时间,分享了几篇关于appium基础的博文,再加上期间也有讲到unittest测试框架,所以今天就来一个专题,在appium+python实现的线性代码基础上,引入unittest框架,使代码更 ...
- (记录)Ubuntu系统中运行需要导入jar包的Java程序
在学习Redis的过程中,在学到Redis客户端Jedis的时候,考虑到能不能在ubuntu下用Vim编写Java程序并且能够运行呢? 于是,首先在网上调研了一番用Vim写Java程序的可实现性. 相 ...
- 设计模式C++描述----03.工厂(Factory)模式
工厂模式属于创建型模式,大致可以分为三类,简单工厂模式.工厂方法模式.抽象工厂模式. 一. 简单工厂模式 简单工厂模式,它的主要特点是需要在工厂类中做判断,从而创造相应的产品.当增加新的产品时,就需要 ...