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".
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.
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
题意:给出小朋友的敌对关系,要求小朋友围着桌子坐一圈,有敌对关系的小朋友不能挨着坐,输出这样的座次
小朋友有敌对关系表示两个点之间有边,求小朋友围着桌子的座次就是求图中的一个环,但要求这个环不能包含所给出的每一条边,没有给出的边是可以用的
包含所有点的环一定是一条汉密尔顿回路
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int M=205*2;
int ans[M],map[M][M],vis[M];
int n,m;
int ansi;
void reverse(int s,int t)
{
int temp;
while(s<t)
{
temp=ans[s];
ans[s]=ans[t];
ans[t]=temp;
s++;
t--;
}
}
void Hamilton()
{
int s=1,t;
int i,j;
int w,temp;
ansi=2;
memset(vis,0,sizeof(vis));
for(i=1;i<=n;i++)
{
if(map[s][i])
break;
}
t=i;
vis[s]=vis[t]=1;
ans[0]=s;
ans[1]=t;
while(true)
{
while(true)
{
for(i=1;i<=n;i++)
{
if(map[t][i]&&!vis[i])
{
ans[ansi++]=i;
vis[i]=1;
t=i;
break;
}
}
if(i>n)
break;
}
w=ansi-1;
i=0;
reverse(i,w);
temp=s;
s=t;
t=temp;
while(true)
{
for(i=1;i<=n;i++)
{
if(map[t][i]&&!vis[i])
{
ans[ansi++]=i;
vis[i]=1;
t=i;
break;
}
}
if(i>n)
break;
}
if(!map[s][t])
{
for(i=1;i<ansi-2;i++)
if(map[ans[i]][t]&&map[s][ans[i+1]])
break;
w=ansi-1;
i++;
t=ans[i];
reverse(i,w);
}
if(ansi==n)
return;
for(j=1;j<=n;j++)
{
if(vis[j])
continue;
for(i=1;i<ansi-2;i++)
if(map[ans[i]][j])
break;
if(map[ans[i]][j])
break;
}
s=ans[i-1];
t=j;
reverse(0,i-1);
reverse(i,ansi-1);
ans[ansi++]=j;
vis[j]=1;
}
}
int main()
{
while(cin>>n>>m)
{
if(m+n==0)
return 0;
if(n==1)
{
cout<<"1 2"<<endl;
continue;
}
n*=2;
for(int i=0;i<=n;i++)
{
for(int j=0;j<=n;j++)
if(i!=j)
map[i][j]=1;
else
map[i][j]=0;
}
for(int i=0;i<m;i++)
{
int a,b;
cin>>a>>b;
map[a][b]=map[b][a]=0;
}
ansi=2;
Hamilton();
cout<<ans[0];
for(int i=1;i<ansi;i++)
cout<<" "<<ans[i];
cout<<endl;
}
return 0;
}