hdu 4337 (哈密顿回路模板)

本文探讨如何通过哈密顿回路解决圆桌骑士的排列问题,确保每位骑士与其相邻的两位都为好友,并且至少有一半的骑士是其好友。文章提供了解题思路及代码实现。

King Arthur's Knights

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

Source
 

Recommend
zhoujiaqi2010
 
这个题很容易看出是找一个哈密顿回路,关键是哈密顿回路怎么求?其实我不会,直接找模板,结果就搜到这题的解题报告了,囧,可见哈密顿回路的题还是挺少的。这题比较特殊的是题中给了条件every knight has at least (N + 1) / 2 other knights as his close friends.这句话其实是保证了哈密顿回路的存在性,所以不会有no solution。http://www.cnblogs.com/markliu/archive/2012/08/03/2621906.html代码稍加修改,模板留存。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <algorithm>
#include <map>
#include <vector>
#include<queue>
#include<set>
#include<cmath>
using namespace std;
typedef long long LL;
typedef pair<double,int> P;
const int maxn = 200 + 5;
const int INF = 1000000000;

int n,m;
bool G[maxn][maxn];
int x[maxn];//记录回路中的第i个点
//bool flag[maxn];

void hamilton(){
    int k;
    bool s[maxn];
    for(int i = 0; i < n; i++){
        x[i] = -1;
        s[i] = false;
    }
    k = 1;
    s[0] = true;
    x[0] = 0;
    while(k >= 0){
        x[k]++;
        while(x[k] < n){
            if(!s[x[k]] && G[x[k - 1]][x[k]])
                break;
            else
                x[k]++;
        }
        if((x[k] < n) && (k != n - 1)){
            s[x[k++]] = true;
        }
        else if((x[k] < n) && k == n - 1 && G[x[k]][x[0]])
            break;
        else{
            x[k] = -1;
            k--;
            s[x[k]] = false;
        }
    }
}

int main(){
    while(scanf("%d%d",&n,&m) != EOF){
        memset(G,0,sizeof(G));
        //memset(flag,0,sizeof(flag));
        memset(x,0,sizeof(x));
        for(int i = 0;i < m;i++){
            int a,b;
            scanf("%d%d",&a,&b);a--;b--;//hamilton函数中下标从0开始
            G[a][b]=G[b][a]=true;
        }

        hamilton();
        /*bool f=0;
        for(int i=0;i<n;i++){
            flag[x[i]]=1;
        }
        for(int i=0;i<n;i++){
            if(!flag[i]) f=1;
        }
        if(f) cout <<"no solution" <<endl;*/
        //else{
        for(int i = 0;i < n;i++){
            if(i == n-1) printf("%d\n",x[i]+1);
            else printf("%d ",x[i]+1);
        }
        //}
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值