POJ 2438 Children's Dining(哈密顿回路)
题目链接:http://poj.org/problem?id=2438
本文链接:http://www.cnblogs.com/Ash-ly/p/5452615.html
题意:
有2*N个小朋友要坐在一张圆桌上吃饭,但是每两个小朋友之间存在一种关系,即敌人或者朋友,然后需要让你安排一个座位次序,使得相邻的两个小朋友都不会是敌人.假设每个人最多有N-1个敌人.如果没有输出"No solution!".
思路:
如果按照题意直接建图,每个点表示一个小朋友,小朋友之间的敌对关系表示两个点之间有边.问题是求小朋友围着桌子的座次就是求图中的一个环,但是要求这个环不能包含所给出的每条边,所有没给出的边却是可以用的,也就是说本题实际上是在上面建的图的反图上求解一个环,使得该环包含所有点.包含所有点的环一定是一条哈密顿回路,所以题目就是求所给图的反图上的一条哈密顿回路.
题目中给了一个特殊条件,就是一共有2*N个小朋友,但是每个人最多有N-1个敌人,也就是说,每个小朋友可以选择坐在身边的小朋友数大于n + 1,这就意味着在建立的反图中,每个点的度数大于N+1,由Dirac定理可知,此图一定存在哈密顿回路,所以答案不会出现"No solution!",即直接构造哈密顿回路就可以了.
代码:
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector> using namespace std;
typedef long long LL;
const int maxN = ; inline void reverse(int arv[maxN + ], int s, int t){
int temp;
while(s < t){
temp = arv[s];
arv[s] = arv[t];
arv[t] = temp;
s++;
t--;
}
} void Hamilton(int ans[maxN + ], bool map[maxN + ][maxN + ], int n){
int s = , t;
int ansi = ;
int i, j;
int w;
int temp;
bool visit[maxN + ] = {false};
for(i = ; i <= n; i++) if(map[s][i]) break;
t = i;
visit[s] = visit[t] = true;
ans[] = s;
ans[] = t;
while(true){
while(true){
for(i = ; i <= n; i++){
if(map[t][i] && !visit[i]){
ans[ansi++] = i;
visit[i] = true;
t = i;
break;
}
}
if(i > n) break;
}
w = ansi - ;
i = ;
reverse(ans, i, w);
temp = s;
s = t;
t = temp;
while(true){
for(i = ; i <= n; i++){
if(map[t][i] && !visit[i]){
ans[ansi++] = i;
visit[i] = true;
t = i;
break;
}
}
if(i > n) break;
}
if(!map[s][t]){
for(i = ; i < ansi - ; i++)
if(map[ans[i]][t] && map[s][ans[i + ]])break;
w = ansi - ;
i++;
t = ans[i];
reverse(ans, i, w);
}
if(ansi == n) return;
for(j = ; j <= n; j++){
if(visit[j]) continue;
for(i = ; i < ansi - ; i++)if(map[ans[i]][j])break;
if(map[ans[i]][j]) break;
}
s = ans[i - ];
t = j;
reverse(ans, , i - );
reverse(ans, i, ansi - );
ans[ansi++] = j;
visit[j] = true;
}
} int main(){
//freopen("input.txt", "r", stdin);
int N, M;
while(~scanf("%d%d", &N, &M) && (N || M)){
N *= ;
bool map[maxN + ][maxN + ] = {};
int ans[maxN + ] = {};
int ansi = ;
for(int i = ; i <= N; i++)
for(int j = ; j <= N; j++)
i == j ? map[i][j] = map[j][i] = : map[i][j] = map[j][i] = ;
memset(ans, , sizeof(ans));
for(int i = ; i <= M; i++){
int u, v;
scanf("%d%d", &u, &v);
map[u][v] = map[v][u]= ;
}
Hamilton(ans, map, N);
for(int i = ; i < N; i++)
printf(i == ? "%d":" %d", ans[i]);
printf("\n"); }
return ;
}
POJ 2438 Children's Dining(哈密顿回路)的更多相关文章
- poj 2438 Children's Dining
http://poj.org/problem?id=2438 题意: 有2*N个人要坐在一张圆桌上吃饭,有的人之间存在敌对关系,安排一个座位次序,使得敌对的人不相邻. 假设每个人最多有N-1个敌人.如 ...
- POJ 2438 Children’s Dining (哈密顿图模板题之巧妙建反图 )
题目链接 Description Usually children in kindergarten like to quarrel with each other. This situation an ...
- 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 2438 (哈密顿回路)
分析: 2*n个小朋友,每个最多有n-1个"敌人",显然是存在哈密顿回路的. 预处理边,然后找哈密顿回路. code #include <iostream> #incl ...
- 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 ...
随机推荐
- jquery/js iframe 元素操作
1.判断id/ class 是否存在? <script> $(function(){ if(("#id_name").length()>0){ //如果id 存在 ...
- BZOJ4591 SHOI2015超能粒子炮·改(卢卡斯定理+数位dp)
注意到模数很小,容易想到使用卢卡斯定理,即变成一个2333进制数各位组合数的乘积.对于k的限制容易想到数位dp.可以预处理一发2333以内的组合数及组合数前缀和,然后设f[i][0/1]为前i位是否卡 ...
- 防恶意解析,禁止用IP访问网站的Apache设置
一般来说,网站可以用域名和IP来访问.你的网站可以通过IP直接访问,本来这没什么问题,但是会有些隐患: 由于搜索引擎也会收录你的IP地址的页面,所以同一个页面搜索引擎会重复收录,造成页面的权重不如单个 ...
- POJ 3179 Corral the Cows
Corral the Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1352 Accepted: 565 De ...
- eclipse web(Spring+SpringMVC+Hibernate)项目迁移至intellij idea
1.导入Eclipseweb项目 跟着导航一直下一步 出现警告不要担心,先点击确认,到后面再进行设置jdk 成功导入项目后如下图 2.对导入的项目进行配置按Ctrl+shift+alt+s(或下图中的 ...
- dbcp基本配置和重连配置
转载自:http://agapple.iteye.com/blog/772507 最近在看一些dbcp的相关内容,顺便做一下记录,免得自己给忘记了. 1. 引入dbcp (选择1.4) Java代码 ...
- supervisor提高nodejs调试效率
1.NodeJS环境安装 2.安装supervisor npm install supervisor -g (表示安装到全局路径下) 开发nodejs程序,调试的时候,无论你修改了代码的哪一部分,都 ...
- 转:增强学习(二)----- 马尔可夫决策过程MDP
1. 马尔可夫模型的几类子模型 大家应该还记得马尔科夫链(Markov Chain),了解机器学习的也都知道隐马尔可夫模型(Hidden Markov Model,HMM).它们具有的一个共同性质就是 ...
- Intellij IDEA创建spring MVC项目
相信各位未来的Java工程师已经接触到了spring MVC这个框架的强大之处,看了很多的教程,都是eclipse的,在intellij IDEA这个强大的工具面前居然不能很顺畅的,今天我就带领大家用 ...
- hdu 2899 Strange fuction (二分)
题目链接:http://acm.hdu.edu.cn/showproblem.pihp?pid=2899 题目大意:找出满足F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x ( ...