King Arthur's Knights
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1403 Accepted Submission(s): 606
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 3 1 2 2 3 1 3 4 4 1 4 2 4 2 3 1 3
Sample Output
1 2 3 1 4 2 3
Source
Recommend
zhoujiaqi2010
思路:1:直接爆搜,发现时间也很快78MS 左右,巨无语,数据太弱了,
2:化成非递归。
非递归版。15MS
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int mm=200;
int g[mm][mm],ans[mm];
bool vis[mm];
int n,m,pos;
bool hamiton()
{
memset(vis,0,sizeof(vis));
memset(ans,-1,sizeof(ans));
pos=0;
ans[pos++]=0;vis[0]=1;
while(~pos)
{++ans[pos];
while(ans[pos]<n)///找的一条边
if(!vis[ans[pos]]&&g[ans[pos-1]][ans[pos]])break;
else ++ans[pos];
if(ans[pos]<n&&pos!=n-1)vis[ans[pos++]]=1;///判断
else if(ans[pos]<n&&pos==n-1&&g[ans[pos]][ans[0]])return 1;
else
{ ans[pos--]=-1;vis[ans[pos]]=0;///dot wrong
}
}
return 0;
}
int main()
{ int a,b;
while(~scanf("%d%d",&n,&m))
{memset(g,0,sizeof(g));
for(int i=0;i<m;++i)
{
scanf("%d%d",&a,&b);--a;--b;
g[a][b]=g[b][a]=1;
}
if(hamiton())
{
for(int i=0;i<n;++i)
printf("%d%c",ans[i]+1,i==n-1?'\n':' ');
}
else printf("no solution\n");
}
}
递归版78MS
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 155;
int b[maxn][maxn];
int route[maxn];
struct Edge
{
int next,v;
};
Edge e[maxn*maxn*2];
int eid,n,m;
int visit[maxn];
int head[maxn];
int flag;
void init()
{
memset(head,-1,sizeof(head));
eid = 0;
}
void addedge(int from,int to)
{
e[eid].next = head[from];
e[eid].v=to;
head[from]=eid++;
}
void dfs(int deep,int x)
{
if (flag) return;
if (deep>=n)
{
if (b[x][route[0]])
{
for (int i=0;i<n-1;i++)
printf("%d ",route[i]);
printf("%d\n",route[n-1]);
flag=1;
}
return;
}
for (int i=head[x];i!=-1;i=e[i].next)
{
if (visit[e[i].v]==0)
{
visit[e[i].v]=1;
route[deep]=e[i].v;
dfs(deep+1,e[i].v);
visit[e[i].v]=0;
}
}
}
int main()
{
int i,j,from,to;
while (~scanf("%d%d",&n,&m))
{
init();
memset(b,0,sizeof(b));
for (i=0;i<m;i++)
{
scanf("%d%d",&from,&to);
addedge(to,from);
addedge(from,to);
b[from][to]=b[to][from]=1;
}
memset(visit,0,sizeof(visit));
route[0]=1;
visit[1]=1;
flag=0;
dfs(1,1);
}
return 0;
}
郑翔的反转版本 15MS
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define N 155
int ans[N],n,m;
bool map[N][N];
inline 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 ansi=2;
int i,j;
int w;
int temp;
bool vis[N]={0};
for(i=1;i<=n;i++) if(map[s][i]) break;
t=i;
vis[s]=vis[t]=true;
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]=true;
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]=true;
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]=true;
}
}
int main()
{
int u,v;
while(~scanf("%d%d",&n,&m))
{
memset(map,0,sizeof(map));
for(int i=0;i<m;i++)
{
scanf("%d%d",&u,&v);
map[u][v]=map[v][u]=1;
}
Hamilton();
for(int i=0;i<n-1;i++)
printf("%d ",ans[i]);
printf("%d\n",ans[n-1]);
}
return 0;
}