King Arthur's Knights hdu 4337 汉密尔顿图(模板)
来源:互联网 发布:linux怎么用vim.tiny 编辑:程序博客网 时间:2024/04/28 12:32
King Arthur's Knights
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1690 Accepted Submission(s): 718
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.
- 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 31 22 31 34 41 42 42 31 3
Sample Output
1 2 31 4 2 3
Source
2012 Multi-University Training Contest 4
Recommend
zhoujiaqi2010
这个题目是汉密尔顿的题目,对于这个题目来说,每个节点都有超过n/2个度,也就是说任意两个点之间的度数之和
可以满足>=n,也就是说肯定是汉密尔顿图了啊,所以带入汉密尔顿题目的模板就可以了,这个题目一组数据有很多答案,
所以是special judge,明白模板很重要,也很关键!!!!
可以满足>=n,也就是说肯定是汉密尔顿图了啊,所以带入汉密尔顿题目的模板就可以了,这个题目一组数据有很多答案,
所以是special judge,明白模板很重要,也很关键!!!!
#include<iostream>#include<cstdio>#include<cstring>using namespace std;int n,m;int ans[202];//记住路径int map[202][202];//地图int vis[202];//标记void rev(int s,int t){ while(s<t) { swap(ans[s],ans[t]); s++; t--; }}int extend(int &num){ int i; while(1) { for(i=1;i<=n;i++) if(map[ans[num]][i]&&!vis[i]) { ans[++num]=i; vis[i]=1; } if(i>n) break; } rev(0,num); while(1) { for(i=1;i<=n;i++) if(map[ans[num]][i]&&!vis[i]) { ans[++num]=i; vis[i]=1; } if(i>n) break; }}void cir(int &num){ for(int i=1;i<num;i++) { if(map[ans[0]][ans[i+1]]&&map[ans[i]][ans[num]]) { rev(0,i); break; } }}void addone(int &num){ int i,j; for(i=1;i<=n;i++)if(!vis[i]) { for(j=1;j<num;j++)if(map[ans[j]][i]) { rev(0,j-1); rev(j,num); vis[i]=1; } if(vis[i]) { ans[++num]=i; break; } }}void solve(int s,int t){ int num=1; int i,j; ans[0]=s; ans[1]=t; vis[s]=1; vis[t]=1; while(1) { extend(num); if(!map[ans[0]][ans[num]]) cir(num); if(num==n-1) break; addone(num); } for(i=0;i<num;i++) { printf("%d ",ans[i]); } printf("%d\n",ans[num]);}int main(){ int i,j,k; int a,b; while(scanf("%d%d",&n,&m)!=EOF) { memset(map,0,sizeof(map)); memset(vis,0,sizeof(vis)); for(i=0;i<m;i++) { scanf("%d%d",&a,&b); map[a][b]=map[b][a]=1; } for(i=2;i<=n;i++) { if(map[1][i]) { solve(1,i); break; } } } return 0;}
0 0
- King Arthur's Knights hdu 4337 汉密尔顿图(模板)
- HDU 4337 King Arthur's Knights 稠密图构造汉密尔顿回路
- HDU 4337 King Arthur's Knights 【哈密顿模板题】
- hdu 4337 King Arthur's Knights#DFS
- hdu 4337 King Arthur's Knights
- HDU 4337 King Arthur's Knights(暴力dfs)
- hdu 4337 King Arthur's Knights(dfs)
- hdu - 4337 - King Arthur's Knights - 哈密顿回路
- HDU 4337 King Arthur's Knights (图论+dfs) = =
- HDU 4337 King Arthur's Knights——回溯法
- hdu4337 King Arthur's Knights
- hdu4337 King Arthur's Knights
- HDU 4337 King Arthur's Knights(…
- hdu 4337 King Arthur's Knights(哈密顿回路,4级)
- HDU 4337 King Arthur's Knights 输出一条哈密顿回路
- hdu4337 King Arthur's Knights(dfs回溯)
- King Arthur's Knights —— 结题报告!
- HDU4337 King Arthur's Knights【哈密顿回路】
- [原创]Yaffs2移植全过程
- 小明传奇1
- Ubuntu12.04 快速安装FTP Server
- Windows 用来定位 DLL 的搜索路径
- javascript匿名函数小结
- King Arthur's Knights hdu 4337 汉密尔顿图(模板)
- 彩色空间及cvtColor解析
- linux中与Oracle有关的内核参数详解
- “VC++ 目录”属性页
- 希尔排序
- Sort Colors 颜色排序
- uisearchbar中自定义 放大镜图标 placeolder字体颜色 文本框背景色 兼容ios7
- android 点击效果动画增强
- http://www.qyer.com/pictrip/3403180/album_1036403