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();
}
}
Children's Dining
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4672   Accepted: 734   Special Judge

Description

Usually children in kindergarten like to quarrel with each other. This situation annoys the child-care women. For instant, when diner time comes, a fierce conflict may break out when a certain couple of children sitting side by side who are hostile with each other. Although there aren't too many children dining at the same round table, but the relationship of "enemy" or "friend" may be very complex. The child-care women do come across a big problem. Now it is time for you to help them to figure out a proper arrangement of sitting, with which no two "enemy" children is adjacent.

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

The input is consisted of several test blocks. For each block, the first line contains two integers n and m (1 <= n <= 200, 0 <= m <= n (n - 1)). We use positive integers from 1 to 2 * n to label the children dining round table. Then m lines followed. Each contains positive integers i and j ( i is not equal to j, 1 <= i, j <= 2 * n), which indicate that child i and child j consider each other as "enemy". In a input block, a same relationship isn't given more than once, which means that if "i j" has been given, "j i" will not be given.

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

For each test block, if the proper arrangement exist, you should print a line with a proper one; otherwise, print a line with "No solution!".

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的更多相关文章

  1. POJ 2438 Children’s Dining (哈密顿图模板题之巧妙建反图 )

    题目链接 Description Usually children in kindergarten like to quarrel with each other. This situation an ...

  2. POJ 2438 Children's Dining(哈密顿回路)

    题目链接:http://poj.org/problem?id=2438 本文链接:http://www.cnblogs.com/Ash-ly/p/5452615.html 题意: 有2*N个小朋友要坐 ...

  3. POJ 2438 哈密顿回路

    Children's Dining Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4730   Accepted: 754 ...

  4. POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE

    POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...

  5. poj 3083 Children of the Candy Corn

    点击打开链接 Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8288 ...

  6. poj 3083 Children of the Candy Corn(DFS+BFS)

    做了1天,总是各种错误,很无语 最后还是参考大神的方法 题目:http://poj.org/problem?id=3083 题意:从s到e找分别按照左侧优先和右侧优先的最短路径,和实际的最短路径 DF ...

  7. POJ 3083 Children of the Candy Corn bfs和dfs

      Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8102   Acc ...

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

  9. POJ 3083 Children of the Candy Corn (DFS + BFS + 模拟)

    题目链接:http://poj.org/problem?id=3083 题意: 这里有一个w * h的迷宫,给你入口和出口,让你分别求以下三种情况时,到达出口的步数(总步数包括入口和出口): 第一种: ...

随机推荐

  1. effective c++ 笔记 (41-44)

    //---------------------------15/04/25---------------------------- //#41   了解隐式接口和编译期多态 { //  1:面向对象编 ...

  2. NetBeans的(默认)快捷键

    NetBeans的(默认)快捷键 1.完成代码:ctrl+\ //任何地方按下此组合键,均会提示相应的参考字段:  2.错误提示:alt + enter //顾名思义,当系统报错时,按下此组合可以查看 ...

  3. Salesforce Apex学习 : 利用Schema命名空间中的DescribeSObjectResult类型来获取sObject对象的基本信息

    DescribeSObjectResult 对象的取得: //使用getDescribe方法和sObject token Schema.DescribeSObjectResult mySObjDesc ...

  4. HyperLedger Fabric 学习思路分享

    HyperLedger Fabric 学习思路分享 HyperLedger Fabric最初是由Digital Asset和IBM公司贡献的.由Linux基金会主办的一个超级账本项目,它是一个目前非常 ...

  5. PAT甲题题解-1096. Consecutive Factors(20)-(枚举)

    题意:一个正整数n可以分解成一系列因子的乘积,其中会存在连续的因子相乘,如630=3*5*6*7,5*6*7即为连续的因子.给定n,让你求最大的连续因子个数,并且输出其中最小的连续序列. 比如一个数可 ...

  6. M1事后分析报告

    在得到M1团队成绩之后,每个团队都需要编写一个事后分析报告,对于团队在M1阶段的工作做一个总结. 请在2015年11月24日上课之前根据下述博客中的模板总结前一阶段的工作,发表在团队博客上,并在课上的 ...

  7. Alpha版本冲刺(十)

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...

  8. Jmeter当获取正则表达式匹配数字为负数时获取所有匹配的值

    需求说明:如果http的bodyData中有类似于{"idList":["6505","6506","2222".... ...

  9. [转帖]Linux后端执行命令的方法

    Linux 后台执行命令的方法 http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=4241330&fromuid=212883 ...

  10. 在delphi中我用DBGrid选择多条记录,如何一次把选择的多条记录删掉

    procedure TForm1.btnDoSumClick(Sender: TObject);var  i: Integer;begin  if DBGrid1.SelectedRows.Count ...