hdu 4337 King Arthur's Knights

King Arthur's Knights

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 248    Accepted Submission(s): 109
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.
 

 

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
 

AC代码:

#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
int map[200][200],rec[200],vis[200];
 int flag ,n,m;
 vector<int>v[200];
 void dfs( int x,int k)
 {
   if(flag) return;
 rec[k]=x;
 
 int i;
 if(k==n&&map[x][1])
 {
   flag=1;
   for( i=1;i<=n;i++)
   {
    printf(i==1?"%d":" %d",rec[i]);
   
   }
   puts("");
   return ;
    }
    int len=v[x].size( );
    for( i=0;i<len;i++)
    {
  if(vis[v[x][i]]==0)
  {
  vis[v[x][i]]=1;
  dfs(v[x][i],k+1);
  vis[v[x][i]]=0;
  }
    }
 }
int main( )
{
  int x,y;
  int i,j;
  while(scanf("%d%d",&n,&m)!=EOF)
  {
 memset(vis,0,sizeof(vis));
 memset(rec,0,sizeof(rec));
 memset(map,0,sizeof(map));
 for( i=0;i<m;i++)
 {
  scanf("%d%d",&x,&y);
  map[x][y]=map[y][x]=1;
  v[x].push_back(y);
  v[y].push_back(x);
    }
    flag=0;
    vis[1]=1;
    dfs(1,1);
    for( i=1;i<=n;i++)
    v[i].clear( );
  }
  return 0;
}

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4337

posted @ 2012-08-03 09:42  jiai  Views(597)  Comments(0Edit  收藏  举报