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 ...
随机推荐
- Vue框架构造
Vue 程序结构框架 Vue.js是典型的MVVM框架,什么是MVVM框架,介绍之前我们先介绍下什么是MVC框架 MVC 即 Model-View-Controller 的缩写,就是 模型-视图-控制 ...
- 关于用gulp合并压缩seaJs模块
现在很多人都在用seaJs来开发项目,seaJs上手容易,操作简单.但在后期做合并压缩的时候却中了个巨大无比的坑,但坑也总得有人来填.于是花了将近一个星期的时间来填了这坑,现将填坑的一些心得与大家分享 ...
- NOIP2009 Hankson 的趣味题 : 数论
题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫 Hankson.现在,刚刚放学回家的 Hankson 正在思考一个有趣的问题. 今天在课堂上,老师讲解 ...
- [Luogu3112] [USACO14DEC]后卫马克Guard Mark
题意翻译 FJ将飞盘抛向身高为H(1 <= H <= 1,000,000,000)的Mark,但是Mark被N(2 <= N <= 20)头牛包围.牛们可以叠成一个牛塔,如果叠 ...
- Cocos2d-x 学习笔记(9) Action 运行原理
1. 从一个Action开始 1.1 创建 在Scene里写一个Sprite,并添加Action: Sprite *sp = Sprite::create("m1.png"); M ...
- vue 详情跳转至列表页 实现列表页缓存
甲爸爸提了一个需求,希望公众号内的商城能够像app一样,从商品详情页跳转至列表页及其他列表页时,可以实现列表页缓存(数据不刷新.位置固定到之前点的商品的位置) 本来想着scrollBehavior应该 ...
- mysql数据库limit分页,排序操作
看到网上很多朋友在问,limit分页之后按照字段属性排序的问题,在这里分享一下我的用法: 1.网上答案: 每页显示5个,显示第三页信息,按照年龄从小到大排序 select * from student ...
- Catalan数的理解
Catalan数的理解 f(0)=1 f(1)=1 f(2)=2 f(3)=5 f(4)=14 f(5)=42 f(2)=f(1)+f(1) f(3)=f(2)+f(1)*f(1)*f(2 ...
- mysql的一些常用操作(一)
1.启动Mysql服务 net start mysql 2.进入mysql环境中,由于自己没有设置密码,直接回车进入即可(要将bin加入到环境变量path中) mysql -u root -p 3.创 ...
- 详细讲解IPython
ipython是一个python的交互式shell,比默认的python shell好用得多,支持变量自动补全,自动缩进,支持bash shell命令,内置了许多很有用的功能和函数.学习ipython ...