King Arthur's Knights

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1415 Accepted Submission(s): 612
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
 
Source
就是给一个图,找到一环就可以了,用个深搜就可以了,用个vector,来存领边就可以了!
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string.h>
using namespace std;
#define MAXN 155
vector<int > vec[MAXN];
int num[155],visit[155],n,map[155];
int dfs(int node,int step)
{
int i;
// printf("%dnode ",node);
if(step==n-1)
{
if(map[node]==1)
{
printf("%d",node);
return 1;
} else
return -1;
}
for(i=0;i<vec[node].size();i++)
{
int temp=vec[node][i];
num[step]=temp;
if(visit[temp]==0)
{
visit[temp]=1;
if(dfs(vec[node][i],step+1)==1)
{
printf(" %d",node);
return 1;
}
visit[temp]=0;
} }
return -1;
}
int main()
{
int m,i,s,e;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(map,0,sizeof(map));
for(i=1;i<=n;i++)
{
vec[i].clear();
}
for(i=0;i<m;i++)
{
scanf("%d%d",&s,&e);
vec[s].push_back(e);
vec[e].push_back(s);
if(s==1)
{
map[e]=1;
}
if(e==1)
{
map[s]=1;
}
}
num[0]=1;
memset(visit,0,sizeof(visit));
visit[1]=1;
if(dfs(1,0)==-1)
{
printf("no solution\n");
continue;
} printf("\n");
} return 0;
}

hdu4337 King Arthur's Knights的更多相关文章

  1. hdu 4337 King Arthur's Knights (Hamilton)

    King Arthur's KnightsTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

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

  3. poj-3682 King Arthur's Birthday Celebration

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

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

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

  5. 【概率论】【POJ 3682】【King Arthur's Birthday Celebration】

    题意:进行翻硬币实验,若k次向上则结束,进行第n次实验需花费2*n-1的费用,询问期望结束次数及期望结束费用 设F[i]为第i次结束时的概率 F[i]=  c(i-1,k-1)*p^k*(1-p)^( ...

  6. King Arthur's Birthday Celebration

    每天抛一个硬币,硬币正面朝上的几率是p,直到抛出k次正面为止结束,第一天抛硬币需花费1,第二天花费3,然后是5,7,9……以此类推,让我们求出抛硬币的天数的期望和花费的期望. 天数期望: A.投出了k ...

  7. POJ3682;King Arthur's Birthday Celebration(期望)

    传送门 题意 进行翻硬币实验,若k次向上则结束,进行第n次实验需花费2*n-1的费用,询问期望结束次数及期望结束费用 分析 我们令f[i]为结束概率 \[f[i]=C_{i-1}^{k-1}*p^k* ...

  8. [POJ3682]King Arthur's Birthday Celebration[期望DP]

    也许更好的阅读体验 \(\mathcal{Description}\) 每天抛一个硬币,硬币正面朝上的几率是p,直到抛出k次正面为止结束,第\(i\)天抛硬币的花费为\(2i-1\),求出抛硬币的天数 ...

  9. poj 3682 King Arthur's Birthday Celebration (期望dp)

    传送门 解题思路 第一问比较简单,设$f[i]​$表示扔了$i​$次正面向上的硬币的期望,那么有转移方程 : $f[i]=f[i]*(1-p)+f[i-1]*p+1​$,意思就是$i​$次正面向上可以 ...

随机推荐

  1. 64_p1

    PEGTL-devel-1.3.1-2.fc26.i686.rpm 13-Feb-2017 22:10 64086 PEGTL-devel-1.3.1-2.fc26.x86_64.rpm 13-Feb ...

  2. Java Web 远程调试

    Java Web 远程 调试 Tomcat 下载压缩版服务器 环境:Tomcat.Eclipse,做远程调试我们并不需要其他特殊插件 1.配置Tomcat/bin/startup.bat 在前面增加代 ...

  3. webstrom 里面使用github

    1.输入github的账号和密码,点击登录 2.复制github的项目地址,现在clone就行了

  4. redis常见数据操作

    redis中有5种常见的数据类型,针对这5种数据类型有着相应的数据操作. 1.String(键值对为String - String) set k1 v1 get k1 getset k1 v1 - h ...

  5. 转:google测试分享-SET和TE

    原文:  http://blog.sina.com.cn/s/blog_6cf812be0102vbnb.html 前端时间看了google测试之道,收获了一些,在此总结下并打算写一个系列blog,顺 ...

  6. Effective C++学习进阶版

    记得前段时间又一次拿起<Effective C++>的时候,有种豁然开朗的感觉,所以翻出了我第一遍读时做的笔记.只做参考以及查阅之用.如有需要请参阅<Effective C++> ...

  7. 洛谷P1482 Cantor表(升级版) 题解

    题目传送门 此题zha一看非常简单. 再一看特别简单. 最后瞟一眼,还是很简单. 所以在此就唠一下GCD大法吧: int gcd(int x,int y){ if(x<y) return gcd ...

  8. IEEEXtreme 10.0 - Goldbach's Second Conjecture

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Goldbach's Second Conjecture 题目来源 第10届IEEE极限编程大赛 https ...

  9. python正则表达式教程

    原文这里,非常实用,转载一下 再来一篇,两篇一起看,美滋滋 本文介绍了Python对于正则表达式的支持,包括正则表达式基础以及Python正则表达式标准库的完整介绍及使用示例.本文的内容不包括如何编写 ...

  10. [原创] 基于RDP的桌面广播

    之前写过一篇使用C# UDP 组播技术做的桌面广播实现, C# 使用UDP组播实现局域网桌面共享.最终效果差强人意,UDP包在不同的交换机上发送还会出现发送失败的情况,所以又重新研究了一些新的方法,包 ...