King Arthur's Knights
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2752    Accepted Submission(s): 1086
Special Judge
Problem Description
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.
Given the relationship of these knights, the King Arthur request you to find an arrangement such that, for every knight, his two adjacent knights are both his close friends. And you should note that because the knights are very united, everyone has at least half of the group as his close friends. More specifically speaking, if there are N knights in total, every knight has at least (N + 1) / 2 other knights as his close friends.
 
Input
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.
 
Output
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".
 
Sample Input
3 3
1 2
2 3
1 3
4 4
1 4
2 4
2 3
1 3
 
Sample Output
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)的更多相关文章

  1. HDU 4337 King Arthur&#39;s Knights 它输出一个哈密顿电路

    n积分m文章无向边 它输出一个哈密顿电路 #include <cstdio> #include <cstring> #include <iostream> usin ...

  2. hdu4337 King Arthur's Knights

    King Arthur's Knights Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...

  3. 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 ...

  4. poj-3682 King Arthur's Birthday Celebration

    C - King Arthur's Birthday Celebration POJ - 3682 King Arthur is an narcissist who intends to spare ...

  5. HDU 5642 King's Order dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5642 King's Order  Accepts: 381  Submissions: 1361   ...

  6. HDU 5644 King's Pilots 费用流

    King's Pilots 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5644 Description The military parade w ...

  7. HDU 5643 King's Game 打表

    King's Game 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5643 Description In order to remember hi ...

  8. HDU 5642 King's Order 动态规划

    King's Order 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5642 Description After the king's speec ...

  9. 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 ...

随机推荐

  1. Spring Boot提供RESTful接口时的错误处理实践

    使用Spring Boot开发微服务的过程中,我们会使用别人提供的接口,也会设计接口给别人使用,这时候微服务应用之间的协作就需要有一定的规范. 基于rpc协议,我们一般有两种思路:(1)提供服务的应用 ...

  2. 阿里云 centos7.X mysql数据库安装、配置

    前言 1024阿里云搞活动,ecs云服务新用户促销活动,活动连接就不贴了,买了之后暂时不知道要做啥,想着先安装mysql数据库. 步骤 root用户登录进来 1.配置mysql的yum源,yum lo ...

  3. mysql 主从关系ERROR 1872 (HY000): Slave failed to initialize relay log info structure from the repository

    连接 amoeba-mysql出现Could not create a validated object, cause: ValidateObject failed mysql> start s ...

  4. 算法<初级> - 第一章 排序相关问题

    算法 - 第一章 时间复杂度: Big O 时间/空间复杂度计算一样,都是跟输入数据源的大小有关 n->∞ O(logn) 每次只使用数据源的一半,logn同理 最优解 先满足时间复杂度的情况最 ...

  5. PassWord控件

    <StackPanel Margin="> <Label>Text:</Label> <TextBox /> <Label>Pas ...

  6. jquery复习

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. R.Swift优雅加载资源文件

    在新的项目中,接触到了一个很不错的框架R.swift,可以帮助更方便安全的使用资源文件,相信已经使用过的或者还没有接触过的,一旦使用过了解过它,会爱上这个框架工具! 一.R.swift特点 当项目bu ...

  8. iOS 原生库对 https 的处理

    转载自:swift cafe 使用 NSURLSession NSURLSession 是 iOS 原生提供的网络处理库.它提供了丰富的接口以及配置选项,满足我们平时网络处理的大部分需求,同时它也支持 ...

  9. Unity3D图像后处理特效——Depth of Field 3.4

    Depth of Field 3.4 is a common postprocessing effect that simulates the properties of a camera lens. ...

  10. VBoxManage.exe: error: Failed to instantiate CLSID_VirtualBox w/ IVirtualBox, CL SID_VirtualBox w/ IUnknown works.

    我先把vagrantbox卸载了 重新装了一个 然后提示这个错误 当时我一脸蒙逼 后来经过百度 1, win+r 快捷键打开 “运行”,输入regedit 打开注册表 2,找到 HKEY_CLASSE ...