zoukankan      html  css  js  c++  java
  • 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): 1358    Accepted Submission(s): 583
    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

    好像是哈密顿回路,但是我暴搜过了,,侥幸ing

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    const int N=200;
    
    int res[N];
    int g[N][N],vis[N];
    int n,m,flag;
    
    void DFS(int x,int cnt){
        if(flag)
            return ;
        if(cnt==n){
            if(g[1][res[n-1]]){
                for(int i=0;i<cnt-1;i++)
                    printf("%d ",res[i]);
                printf("%d\n",res[cnt-1]);
                flag=1;
            }
        }else{
            for(int i=1;i<=n;i++)
                if(!vis[i] && g[i][x]){
                    vis[i]=1;
                    res[cnt]=i;
                    DFS(i,cnt+1);
                    vis[i]=0;
                }
        }
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        while(~scanf("%d%d",&n,&m)){
            memset(vis,0,sizeof(vis));
            memset(g,0,sizeof(g));
            int a,b;
            while(m--){
                scanf("%d%d",&a,&b);
                g[a][b]=g[b][a]=1;
            }
            flag=0;
            res[0]=1;
            vis[1]=1;
            DFS(1,1);
            if(!flag)
                printf("no solution\n");
        }
        return 0;
    }
  • 相关阅读:
    Java FileInputStream与FileReader的区别
    java 保存和读取本地json文件
    java写文件时往末尾追加文件(而不是覆盖原文件),的两种方法总结
    Java魔法堂:注解用法详解——@SuppressWarnings
    使用Restlet Client发送各种Get和Post请求
    postman VS restlet client基本使用
    Java获取请求客户端的真实IP地址
    dom4j解析xml字符串实例
    C++ Boost库简介(一些自己的感受)
    打仗其实最讲成本核算,大炮轰的都是黄金,日军在中国就是不断赔本
  • 原文地址:https://www.cnblogs.com/jackge/p/3053654.html
Copyright © 2011-2022 走看看