当前位置:编程学习 > C/C++ >>

hdu4337 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): 1415 Accepted Submission(s): 612
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
2012 Multi-University Training Contest 4
就是给一个图,找到一环就可以了,用个深搜就可以了,用个vector,来存领边就可以了!

#include <iostream>  
#include <stdio.h>  
#include <vector>  
#include <string.h>  
using namespace std;  
#define MAXN 155  
vector<int > vec[MAXN];  
int num[155],visit[155],n,map[155];  
int dfs(int node,int step)  
{  
   int i;  
   // printf("%dnode ",node);  
   if(step==n-1)  
   {  
       if(map[node]==1)  
       {  
           printf("%d",node);  
           return 1;  
       }  
  
       else  
       return -1;  
   }  
   for(i=0;i<vec[node].size();i++)  
   {  
       int temp=vec[node][i];  
       num[step]=temp;  
       if(visit[temp]==0)  
       {  
           visit[temp]=1;  
           if(dfs(vec[node][i],step+1)==1)  
           {  
               printf(" %d",node);  
                return 1;  
           }  
            visit[temp]=0;  
        }  
  
    }  
   return -1;  
}  
int main()  
{  
    int m,i,s,e;  
   while(scanf("%d%d",&n,&m)!=EOF)  
   {  
       memset(map,0,sizeof(map));  
       for(i=1;i<=n;i++)  
       {  
           vec[i].clear();  
       }  
       for(i=0;i<m;i++)  
       {  
           scanf("%d%d",&s,&e);  
            vec[s].push_back(e);  
           vec[e].push_back(s);  
           if(s==1)  
           {  
               map[e]=1;  
           }  
           if(e==1)  
           {  
               map[s]=1;  
           }  
       }  
       num[0]=1;  
       memset(visit,0,sizeof(visit));  
       visit[1]=1;  
       if(dfs(1,0)==-1)  
       {  
           printf("no solution\n");  
           continue;  
       }  
  
  
       printf("\n");  
   }  
  
    return 0;  
}  

#include <iostream>
#include <stdio.h>
#include <vector>
#include <string.h>
using namespace std;
#define MAXN 155
vector<int > vec[MAXN];
int num[155],visit[155],n,map[155];
int dfs(int node,int step)
{
   int i;
   // printf("%dnode ",node);
   if(step==n-1)
   {
       if(map[node]==1)
       {
           printf("%d",node);
           return 1;
       }

       else
       return -1;
   }
   for(i=0;i<vec[node].size();i++)
   {
       int temp=vec[node][i];
       num[step]=temp;
       if(visit[temp]==0)
       {
           visit[temp]=1;
           if(dfs(vec[node][i],step+1)==1)
           {
               printf(" %d",node);
                return 1;
           }
            visit[temp]=0;
        }

    }
   return -1;
}
int main()
{
    int m,i,s,e;
   while(scanf("%d%d",&n,&m)!=EOF)
   {
       memset(map,0,sizeof(map));
       for(i=1;i<=n;i++)
       {
           vec[i].clear();
       }
       for(i=0;i<m;i++)
       {
           scanf("%d%d",&s,&e);
			vec[s].push_back(e);
           vec[e].push_back(s);
           if(s==1)
           {
               map[e]=1;
           }
           if(e==1)
           {
               map[s]=1;
           }
       }
       num[0]=1;
       memset(visit,0,sizeof(visit));
       visit[1]=1;
       if(dfs(1,0)==-1)
       {
           printf("no solution\n");
           continue;
       }


       printf("\n");
   }

    return 0;
}


 

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,