King Arthur's Knights hdu 4337 汉密尔顿图(模板)

来源:互联网 发布:linux怎么用vim.tiny 编辑:程序博客网 时间:2024/04/28 12:32

King Arthur's Knights

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1690    Accepted Submission(s): 718
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 31 22 31 34 41 42 42 31 3
 

Sample Output
1 2 31 4 2 3
 

Source
2012 Multi-University Training Contest 4
 

Recommend
zhoujiaqi2010


这个题目是汉密尔顿的题目,对于这个题目来说,每个节点都有超过n/2个度,也就是说任意两个点之间的度数之和
可以满足>=n,也就是说肯定是汉密尔顿图了啊,所以带入汉密尔顿题目的模板就可以了,这个题目一组数据有很多答案,
所以是special judge,明白模板很重要,也很关键!!!!



#include<iostream>#include<cstdio>#include<cstring>using namespace std;int n,m;int ans[202];//记住路径int map[202][202];//地图int vis[202];//标记void rev(int s,int t){    while(s<t)    {        swap(ans[s],ans[t]);        s++;        t--;    }}int extend(int &num){    int i;    while(1)    {       for(i=1;i<=n;i++)       if(map[ans[num]][i]&&!vis[i])       {           ans[++num]=i;           vis[i]=1;       }       if(i>n)       break;    }    rev(0,num);     while(1)    {       for(i=1;i<=n;i++)       if(map[ans[num]][i]&&!vis[i])       {           ans[++num]=i;           vis[i]=1;       }       if(i>n)       break;    }}void cir(int &num){    for(int i=1;i<num;i++)    {        if(map[ans[0]][ans[i+1]]&&map[ans[i]][ans[num]])        {            rev(0,i);            break;        }    }}void addone(int &num){    int i,j;    for(i=1;i<=n;i++)if(!vis[i])    {        for(j=1;j<num;j++)if(map[ans[j]][i])        {            rev(0,j-1);            rev(j,num);            vis[i]=1;        }        if(vis[i])        {            ans[++num]=i;            break;        }    }}void solve(int s,int t){    int num=1;    int i,j;    ans[0]=s;    ans[1]=t;    vis[s]=1;    vis[t]=1;    while(1)    {      extend(num);      if(!map[ans[0]][ans[num]])      cir(num);      if(num==n-1)      break;      addone(num);    }    for(i=0;i<num;i++)    {        printf("%d ",ans[i]);    }    printf("%d\n",ans[num]);}int main(){    int i,j,k;    int a,b;    while(scanf("%d%d",&n,&m)!=EOF)    {        memset(map,0,sizeof(map));        memset(vis,0,sizeof(vis));        for(i=0;i<m;i++)        {            scanf("%d%d",&a,&b);            map[a][b]=map[b][a]=1;        }        for(i=2;i<=n;i++)        {            if(map[1][i])            {                solve(1,i);                break;            }        }    }    return 0;}



0 0
原创粉丝点击