HDU 4337 King Arthur's Knights (graph theory + dfs) =

Source: Internet
Author: User

King Arthur's Knights
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 1518 Accepted Submission (s): 645
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 shoshould 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
1 4
2 4
2 3
1 3

Sample Output
1 2 3
1 4 2 3

Source
2012 Multi-University Training Contest 4


Recommend
Zhoujiaqi2010


A round table with seats. Everyone is a friend of their own. It is known that more than half of everyone is his friend. This question should be made using the Hamilton loop, but that premise makes no
There may be no solution, so the deep search with backtracing can be used, so it becomes the same as the classic prime ring.
Thoughts:
1. First, build an undirected graph !!! I didn't pay attention to it at the beginning. Re [a] [B] = true, re [B] [a] = true;
2. You missed it! = EOF. It is not surprising that no timeout occurs .... It's no wonder that, except for the starting position of the subscript, the report for searching and solving problems in the competition is almost identical. You can't go through it ....


Code:

#include<iostream>#include<cmath>#include<cstdio>#include<vector>#include<cstring>#include<algorithm>using namespace std;bool re[155][155];int ok[155];bool vis[155];int n,m;int flag;void dfs(int a,int pos){    if(flag==1)        return;    if(pos==n+1)    {        if(re[ok[1]][ok[n]])        {            for(int i=1;i<=n;i++)                printf("%d%c",ok[i],i==n?'\n':' ');            flag=1;        }    }    else    {        for(int i=2;i<=n;i++)        {            if(re[a][i]&&!vis[i])            {                vis[i]=true;                ok[pos]=i;                //printf("test: %d %d\n",pos,i);                dfs(i,pos+1);                //if(flag==1)                    //break;                vis[i]=false;            }        }    }}int main(){    int a,b;    while(scanf("%d%d",&n,&m)!=EOF)    {        memset(vis,0,sizeof(vis));        memset(re,0,sizeof(re));        memset(ok,0,sizeof(ok));        flag=0;        for(int i=1;i<=m;i++)        {            scanf("%d%d",&a,&b);            re[a][b]=true;            re[b][a]=true;        }        ok[1]=1;        dfs(1,2);        if(!flag)            printf("no solution\n");    }    return 0;}
Another code that traces back to the primary function and then outputs the result:
#include<iostream>#include<cmath>#include<cstdio>#include<vector>#include<cstring>#include<algorithm>using namespace std;bool re[155][155];int ok[155];bool vis[155];int n,m;int flag;void dfs(int a,int pos){    if(flag==1)        return;    if(pos==n+1)    {        if(re[ok[1]][ok[n]])            flag=1;        //return;    }    else    {        for(int i=2;i<=n;i++)        {            if(re[a][i]&&!vis[i]&&flag==0)            {                vis[i]=true;                ok[pos]=i;                //printf("test: %d %d\n",pos,i);                dfs(i,pos+1);                //if(flag==1)                   //break;                vis[i]=false;            }        }           }}int main(){    int a,b;    while(scanf("%d%d",&n,&m)!=EOF)    {        memset(vis,0,sizeof(vis));        memset(re,0,sizeof(re));        memset(ok,0,sizeof(ok));        flag=0;        for(int i=1;i<=m;i++)        {            scanf("%d%d",&a,&b);            re[a][b]=true;            re[b][a]=true;        }        ok[1]=1;        dfs(1,2);        if(flag)        {            for(int i=1;i<=n;i++)                printf("%d%c",ok[i],i==n?'\n':' ');        }        else printf("no solution\n");    }    return 0;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.