HDU 4337

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

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
题目要求排一个座次,每个骑士必须和他的好友左右相邻,围成一个圆圈。。抽象出数学模型就是有一个图,在图中找一个环路,包括所有的点,每个点当然只能经过一次喽~说到这里当然就能想到这不就是一个哈密顿回路的题吗???由于题目说没个骑士都至少有(n+1)/2个好友,满足哈密顿回路的充分条件(如果图中任意两个不同顶点的度数之和大于等于n,则图就具有哈密顿回路)因此,直接套模板就好了。。。

#include <iostream>
#include <string.h>
#include <cstdio>
#include <cmath>
using namespace std;
const int N=410;
int mp[N][N];
int ans[N];
int vis[N];
int start;
int end;
int n,m;
int cnt;
void init()//预处理
{
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
            if(i==j)
                mp[i][j]=0;
            else
                mp[i][j]=0;
    memset(vis,0,sizeof(vis));
    memset(ans,0,sizeof(ans));
    cnt=0;
}
void reverse(int s,int e)//将ans数组中s到t的部分倒置
{
    while(s<e)
    {
        swap(ans[s],ans[e]);
        s++;
        e--;
    }
}
void kuozhan()//从end点开始向外扩展
{
    while(1)
    {
        int flag=0;
        for(int i=1; i<=n; i++)
        {
            if(!vis[i]&&mp[end][i])
            {
                ans[cnt++]=i;
                end=i;
                vis[i]=1;
                flag=1;
                break;
            }
        }
        if(!flag)//此时i=n+1,此时已经遍历全部的点
            break;
    }
}
void hamiltun()//汉密顿回路主体算法
{
    start=1;
    for(int i=1; i<=n; i++)//取任意邻接于s的点为end
        if(mp[1][i])
        {
            end=i;
            break;
        }
    vis[start]=1;
    vis[end]=1;
    ans[0]=start;
    ans[1]=end;
    cnt=2;
    while(1)
    {
        kuozhan();//从end开始向外扩展
        reverse(0,cnt-1);//将当前得到的序列倒置
        swap(start,end);// start和end互换,
        kuozhan();//从end继续扩展,相当于在原来的序列上从start向外扩展
//-------------------------------------------------------------------------------
   //情况一:如果start和end不相邻,进行调整
        int mid=0;
        if(!mp[start][end])
        {
            for(int i=1; i<cnt-2; i++)//取序列中的一点i,使得ans[i]与end相连,ans[i+1]和start相连(一定有这种情况)
            {
                if(mp[ans[i]][end]&&mp[ans[i+1]][start])
                {
                    mid=i+1;
                    break;
                }
            }
            reverse(mid,cnt-1);//将ans[i+1]到end部分的ans[]倒置
            end=ans[cnt-1];
        }
//------------------------------------------------------------------------
   //情况二:如果当前start和end相连
        if(cnt==n)//如果当前序列中已经包含n个元素,算法结束
            break;
        for(int i=1; i<=n; i++)//如果当前序列中元素的个数小于n,寻找点ans[i],使得ans[i]与ans[]外的一点相连
        {
            if(!vis[i])
            {
                int j;
                for(j=1; j<cnt-1; j++)
                    if(mp[ans[j]][i])
                    {
                        mid=j;
                        break;
                    }
                if(mp[ans[mid]][i])
                {
                    end=i;
                    mid=j;
                    break;
                }
            }
        }
        start=ans[mid-1];
        reverse(0,mid-1);//将ans[]中s到ans[i-1] 部分的ans[]倒置
        reverse(mid,cnt-1);//将ans[]中ans[i]到end的部分倒置
        ans[cnt++]=end;//将新找到的这一点i,加入到ans[]尾部
        vis[end]=1;//标记
        //继续进行循环
//-------------------------------------------------------------------------------------
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(!n&&!m)
            break;
        //n*=2;
        int u,v;
        init();
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d",&u,&v);//输出的两点是二者之间没有连通的路
            mp[u][v]=mp[v][u]=1;
        }
        hamiltun();
        cout<<ans[0];
        for(int i=1; i<cnt; i++)//输出哈密顿回路的访问路径
            printf(" %d",ans[i]);
        cout << endl;
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值