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. Dubbo+Zookeeper(一)Zookeeper初识

    前面花了一段时间去学习SpringCloud的相关知识,主要是理解微服务的概念并使用SpringCloud的一系列组件实现微服务落地.学习这些组件本身是简单的,跟着操作一遍基本就会了,这也得益于Spr ...

  2. 冷知识: 不会出现OutOfMemoryError的内存区域

    程序计数器(PC) 因为程序计数器只是记录当前线程正在执行的那条字节码指令的地址,即使出现死循环都不会内存溢出

  3. SpringData-Redis发布订阅自动重连分析

    SpringData-Redis发布订阅自动重连分析 RedisMessageListenerContainer 配置 @Bean @Autowired RedisMessageListenerCon ...

  4. 别说家庭组开不了,一定成功的方法|win7家庭组无法开启|win7如何共享打印机

    两台WIN7要开启家庭组并且共享打印机 这两台电脑必须打开以下服务:dnscache(任务管理器 简写).fdrespub(任务管理器 简写).(接下来下了都是服务里面的)SSDP Discovery ...

  5. 函数基础(二)(day11整理)

    目录 昨日内容 函数的定义 函数的三种定义方式 空函数 有参函数 无参函数 函数的调用 函数的返回值 函数的参数 形参 实参 今日内容 可变长参数 可变长形参 可变长实参(仅作了解) 函数对象 函数嵌 ...

  6. 封装自己通用的 增删改查的方法 By EF

    封装自己的通用CURD By EF using System; using System.Collections.Generic; using System.Data.Entity; using Sy ...

  7. C语言I博客作业07

    这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/SE2019-1/homework/9931 我在这个课程的目标 ...

  8. 【CF696D】Legen...(AC自动机)(矩阵快速幂)

    题目描述 Barney was hanging out with Nora for a while and now he thinks he may have feelings for her. Ba ...

  9. RobotFramework自动化测试框架-Selenium Web自动化(二)关于在RobotFramework中如何使用Selenium很全的总结(上)

    好久没有继续分享关于自动化测试相关的东西了,自动化在现今的测试领域已经越来越重要了,大部分公司在测试岗位招聘中都需要会相关的自动化测试知识.而 RobotFramework自动化测试框架 是自动化测试 ...

  10. [考试反思]0825NOIP模拟测试30:没落

    AB卷,15人. Lrefrain rank#1 179 skyh rank#2 122 116 108 54 42虽说还是不怎么样,但是有好转的迹象. 开卷审题,T1是个(假)期望,感觉也许还可做. ...