HDU 4337 King Arthur's Knights 【哈密顿模板题】

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
 


超级简单

    #include<stdio.h>
    #include<string.h>
    #define M 407
using namespace std;
    int ans[M],g[M][M];
    bool map[M][M],vis[M];
    int n,m;

    void init()
    {
        for(int i=0;i<=n;i++)
            for(int j=0;j<=n;j++)
                if(i==j)
                    g[i][j]=0;
                else
                    g[i][j]=0;
        memset(vis,0,sizeof(vis));
        memset(ans,0,sizeof(ans));
    }

    void revese(int ans[M],int s,int t)//将ans数组中s到t的部分倒置
    {
        int temp;
        while(s<t)
        {
            temp=ans[s];
            ans[s]=ans[t];
            ans[t]=temp;
            s++;
            t--;
        }
    }

    void Hamilton()
    {
        int s=1,t;//初始化s取1号点
        int ansi=2;
        int i,j,w,temp;
        for(i=1;i<=n;i++)
            if(g[s][i])break;
        t=i;//取任意连接s的点为t
        vis[s]=vis[i]=true;
        ans[0]=s;
        ans[1]=t;
        while(true)
        {
            while(true)//从t向外扩展
            {
                for(i=1;i<=n;i++)
                    if(g[t][i]&&!vis[i])
                    {
                        ans[ansi++]=i;
                        vis[i]=true;
                        t=i;
                        break;
                    }
                if(i>n)break;
            }
            //将当前得到的序列倒置,s和t互换,从t继续扩展,相当于在原来的序列上从s扩展
            w=ansi-1;
            i=0;
            revese(ans,i,w);
            temp=s;
            s=t;
            t=temp;
            //从新的t向外扩展,相当于在原来的序列上从s向外扩展
            while(true)
            {
                for(i=1;i<=n;i++)
                    if(g[t][i]&&!vis[i])
                    {
                        ans[ansi++]=i;
                        vis[i]=true;
                        t=i;
                        break;
                    }
                if(i>n)break;
            }
            //如果s和t不相邻,进行调整
            if(!g[s][t])
            {
                for(i=1;i<ansi-2;i++)
                    if(g[ans[i]][t]&&g[s][ans[i+1]])//取序列中一点i,使得ans[i]与t相连接且ans[i+1]与s相连
                        break;
                //将从ans[i+1]到t部分的ans[]倒置
                w=ansi-1;
                i++;
                t=ans[i];
                revese(ans,i,w);
            }
            //如果当前s和t相连
            if(ansi==n)return;//如果当前序列中包含n个元素,算法结束
            //当前序列中的元素个数小于n,寻找点ans[i],使得ans[i]与ans[]外一点相连
            for(j=1;j<=n;j++)
            {
                if(vis[j])continue;
                for(i=1;i<ansi-2;i++)
                    if(g[ans[i]][j])
                        break;
                if(g[ans[i]][j])
                    break;
            }
            s=ans[i-1];
            t=j;
            revese(ans,0,i-1);//将ans[]中s到ans[i-1]部分的ans[]倒置
            revese(ans,i,ansi-1);//将ans[]中ans[i]到t的部分倒置
            ans[ansi++]=j;//将点j加入到ans[]的尾部
            vis[j]=true;
        }
    }

    int main()
    {
       // freopen("cin.txt","r",stdin);
        while(~scanf("%d%d",&n,&m))
        {
         //   n*=2;
            init();
            int a,b;
            for(int i=1;i<=m;i++)
            {
                scanf("%d%d",&a,&b);
                g[a][b]=g[b][a]=1;//巧妙的建立反图
            }
            Hamilton();
            printf("%d",ans[0]);
            for(int i=1;i<n;i++)
                printf(" %d",ans[i]);
            printf("\n");
        }
        return 0;
    }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值