poj 2438 Children's Dining
http://poj.org/problem?id=2438
题意:
有2*N个人要坐在一张圆桌上吃饭,有的人之间存在敌对关系,安排一个座位次序,使得敌对的人不相邻.
假设每个人最多有N-1个敌人.如果没有输出"No solution!".
如果i和j可以相邻,之间连一条边
每个人最多有N-1个敌人,所以每个人至少会连出去N+1条边
根据狄拉克定理,图一定是哈密顿图
所以本题不存在无解的情况
然后输出一条哈密顿回路就好了
有关哈密顿图与哈密顿回路的问题 参见文章
http://www.cnblogs.com/TheRoadToTheGold/p/8439160.html
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm> using namespace std; #define N 401 int n,m; bool e[N][N]; int cnt,s,t;
bool vis[N];
int ans[N]; void read(int &x)
{
x=; char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) { x=x*+c-''; c=getchar(); }
} void Reverse(int i,int j)
{
while(i<j) swap(ans[i++],ans[j--]);
} void expand()
{
while()
{
int i;
for(i=;i<=n;++i)
if(e[t][i] && !vis[i])
{
ans[++cnt]=t=i;
vis[i]=true;
break;
}
if(i>n) return;
}
} void Hamilton()
{
memset(vis,false,sizeof(vis));
cnt=;
s=;
for(t=;t<=n;++t)
if(e[s][t]) break;
vis[s]=vis[t]=true;
cnt=;
ans[]=s;
ans[]=t;
while()
{
expand();
Reverse(,cnt);
swap(s,t);
expand();
if(!e[s][t])
{
int i;
for(i=;i<cnt;++i)
if(e[ans[i]][t] && e[s][ans[i+]]) break;
t=ans[i+];
Reverse(i+,cnt);
}
if(cnt==n) break;
int j,i;
for(j=;j<=n;++j)
if(!vis[j])
{
for(i=;i<cnt;++i)
if(e[ans[i]][j]) break;
if(e[ans[i]][j]) break;
}
s=ans[i-];
t=j;
Reverse(,i-);
Reverse(i,cnt);
ans[++cnt]=j;
vis[j]=true;
}
for(int i=;i<cnt;++i) printf("%d ",ans[i]);
printf("%d\n",ans[cnt]);
} int main()
{
int u,v;
while()
{
read(n); read(m);
if(!n) return ;
memset(e,true,sizeof(e));
n<<=;
while(m--)
{
read(u); read(v);
e[u][v]=e[v][u]=false;
}
for(int i=;i<=n;++i) e[i][i]=false;
Hamilton();
}
}
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 4672 | Accepted: 734 | Special Judge |
Description
Now we assume that there are 2 * n children who sit around a big table, and that none has more than n - 1 "enemies".
Input
There will be a blank line between input blocks. And m = n = 0 indicates the end of input and this case shouldn't be processed.
Output
Sample Input
1 0 2 2
1 2
3 4 3 6
1 2
1 3
2 4
3 5
4 6
5 6 4 12
1 2
1 3
1 4
2 5
2 6
3 7
3 8
4 8
4 7
5 6
5 7
6 8 0 0
Sample Output
1 2
4 2 3 1
1 6 3 2 5 4
1 6 7 2 3 4 5 8
poj 2438 Children's Dining的更多相关文章
- POJ 2438 Children’s Dining (哈密顿图模板题之巧妙建反图 )
题目链接 Description Usually children in kindergarten like to quarrel with each other. This situation an ...
- POJ 2438 Children's Dining(哈密顿回路)
题目链接:http://poj.org/problem?id=2438 本文链接:http://www.cnblogs.com/Ash-ly/p/5452615.html 题意: 有2*N个小朋友要坐 ...
- POJ 2438 哈密顿回路
Children's Dining Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4730 Accepted: 754 ...
- POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE
POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...
- poj 3083 Children of the Candy Corn
点击打开链接 Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8288 ...
- poj 3083 Children of the Candy Corn(DFS+BFS)
做了1天,总是各种错误,很无语 最后还是参考大神的方法 题目:http://poj.org/problem?id=3083 题意:从s到e找分别按照左侧优先和右侧优先的最短路径,和实际的最短路径 DF ...
- POJ 3083 Children of the Candy Corn bfs和dfs
Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8102 Acc ...
- POJ:3083 Children of the Candy Corn(bfs+dfs)
http://poj.org/problem?id=3083 Description The cornfield maze is a popular Halloween treat. Visitors ...
- POJ 3083 Children of the Candy Corn (DFS + BFS + 模拟)
题目链接:http://poj.org/problem?id=3083 题意: 这里有一个w * h的迷宫,给你入口和出口,让你分别求以下三种情况时,到达出口的步数(总步数包括入口和出口): 第一种: ...
随机推荐
- Asp.Net_Wcf跟Wpf的区别
摘要:WCF,你就先把它想成WebService的下一代也没什么问题.WCF为WindowsCommunicationFoundation,是Microsoft为构建面向服务的应用提供的分布式通信编程 ...
- Js_字体滚动换颜色
<html><head><meta http-equiv="Content-Type" content="text/html; charse ...
- EOS开发基础之四:使用cleos命令行客户端操作EOS——智能合约之eosio.bios和eosio.token
现实世界中的合约,简单地说,是一个参与活动的所有人都需要遵循的协议.合约可以是正式的法律合同(例如,金融交易),或者是简单的游戏规则.典型的活动可以是诸如资金转移(在金融合约的情况下)或游戏动作(在游 ...
- web安全入门课程笔记——网站基础与信息搜集
2-1 网站的基本概念 URL统一资源定位符 这是一个动态页面 ?ID 查询条件 后台数据库最有可能:ACCESS Web容器(web容器是一种服务程序,在服务器一个端口就有一个提供相应服务的程序,而 ...
- 初次接触OSSEC
OSSEC是一款开源的系统监控平台.它集成了HIDS(主机入侵检测).日志监控.安全事件管理(SIM).安全信息和事件管理(SIEM)于一身,结构简单.功能强大的开源解决方案. 主要优点 满足合规性 ...
- RN热更新
说白了集成RN业务,就是集成RN离线包,解析并渲染.所以,RN热更新的根本原理就是更换js bundle文件和资源文件,并重新加载,新的内容就完美的展示出来了. 目前市场上出现的3种热更新模式如下:仅 ...
- PAT甲题题解-1033. To Fill or Not to Fill (25)-模拟
模拟先说一下例子,最后为方便起见,在目的地安增加一个费用为0的加油站0 1 2 3 4 5 6 7 87.1 7.0 7.2 6.85 7.5 7.0 7.3 6.0 00 150 200 300 4 ...
- 1084. Broken Keyboard (20)-水题
#include <iostream> #include <cstdio> #include <string.h> #include <algorithm&g ...
- 【Alpha】第九次Scrum meeting
今日任务一览: 姓名 今日完成任务 所耗时间 刘乾 由于发现之前版本的模板引擎存在致命bug,所以重新更换,现在使用jinja2作为模板渲染引擎. 3 鲁聃 卤蛋今天为了给编译老师做视频,没有做软工项 ...
- 【读书笔记】Linux内核设计与实现(第一章&第二章)
http://pan.baidu.com/s/1hqYAZNQ OneNote做的笔记没法儿带着格式一起导进来.所以上传到百度云,麻烦老师下载一下了. 下次不再用OneNote.