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 ...
随机推荐
- PHP array_replace
1.函数的作用:替换数组的元素 2.函数的参数: @params array $array1 被替换的数组 @params array $array2 替换的数组 @params array $a ...
- .NET实时2D渲染入门·动态时钟
.NET实时2D渲染入门·动态时钟 从小以来"坦克大战"."魂斗罗"等游戏总令我魂牵梦绕.这些游戏的基础就是2D实时渲染,以前没意识,直到后来找到了Direct ...
- MFC::使用mysql
下载mysql-installer-community-5.7.16.0.msi,安装 mysql server即可. 创建工程包含头文件 #include "winsock.h" ...
- Halcon C# 联合编程问题(三)
因为之前遇到的那个halcon处理的图片要转换成ImageSource的问题,迟迟没有找到好的解决方案, 于是决定直接在wpf中使用halcon提供的HWindowControlWPF,用于显示图片. ...
- python soket服务和客户端Demo
#服务端from socket import * s=socket(AF_INET,SOCK_STREAM)#IVP4 寻址 tcp协议 s.bind(('',6666))#补丁端口 s.listen ...
- JVM学习记录2--垃圾回收算法
首先要明确,垃圾回收管理jvm的堆内存,方法区是堆内存的一部分,所以也是. 而本地方法栈,虚拟机栈,程序计数器随着线程开始而产生,线程的结束而消亡,是不需要垃圾回收的. 1. 判断对象是否可以被回收 ...
- 史上最骚最全最详细的IO流教程,没有之一!
目录 1.告白IO流的四点明确 2.File类 1.1 File概述 1.2 构造方法 1.3 常用方法 1.3.1 获取功能的方法 1.3.2 绝对路径和相对路径 1.3.3判断功能的方法 1.3. ...
- 设计模式(二十三)Interpreter模式
在Interpreter模式中,程序要解决的问题会被用非常简单的“迷你语言”表述出来,即用“迷你语言”编写的“迷你程序”把具体的问题表述出来.迷你程序是无法单独工作的,还需要用java语言编写一个负责 ...
- SpringBoot的Banner
一 官方文档介绍 1 自定义横幅 通过在 classpath 中添加banner.txt文件或将banner.location设置为此类文件的位置,可以更改启动时打印的横幅.如果文件具有异常编码,则可 ...
- 设计模式C++描述----18.中介者(Mediator)模式
一. 举例 比如,现在中图和日本在关于钓鱼岛问题上存在争端.这时,联合国就会站出来,做为调解者,其实也没什么好调解的,钓鱼岛本来就是中国的,这是不争的事实!联合国也就是个传话者.发言人. 结构图如下: ...