hdu 4337 King Arthur's Knights (Hamilton)

Posted getcharzp

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 4337 King Arthur's Knights (Hamilton)相关的知识,希望对你有一定的参考价值。

King Arthur‘s Knights
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2752    Accepted Submission(s): 1086
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

 

C/C++:

 1 #include <map>
 2 #include <queue>
 3 #include <cmath>
 4 #include <vector>
 5 #include <string>
 6 #include <cstdio>
 7 #include <cstring>
 8 #include <climits>
 9 #include <iostream>
10 #include <algorithm>
11 #define INF 0x3f3f3f3f
12 using namespace std;
13 const int my_max = 200;
14 
15 int n, m, a, b, my_map[my_max][my_max], my_ans[my_max], my_book[my_max];
16 
17 bool my_hamilton()
18 {
19     int pos = 0;
20     my_ans[pos ++] = 0, my_book[0] = 1;
21     while (~pos)
22     {
23         my_ans[pos] ++;
24         while (my_ans[pos] < n)
25             if (!my_book[my_ans[pos]] && my_map[my_ans[pos - 1]][my_ans[pos]]) break;
26             else my_ans[pos] ++;
27         if (my_ans[pos] < n && pos == n - 1 && my_map[my_ans[pos]][my_ans[0]]) return 1;
28         else if (my_ans[pos] < n && pos < n - 1) my_book[my_ans[pos ++]] = 1;
29         else
30         {
31             my_ans[pos --] = -1;
32             my_book[my_ans[pos]] = 0;
33         }
34     }
35     return false;
36 }
37 
38 int main()
39 {
40     while(~scanf("%d%d", &n, &m))
41     {
42         memset(my_map, 0, sizeof(my_map));
43         memset(my_book, 0, sizeof(my_book));
44         memset(my_ans, -1, sizeof(my_ans));
45 
46         for (int i = 0; i < m; ++ i)
47         {
48             scanf("%d%d", &a, &b);
49             --a, --b;
50             my_map[a][b] = my_map[b][a] = 1;
51         }
52 
53         if (my_hamilton())
54             for (int i = 0; i < n; ++ i)
55                 printf("%d%c", my_ans[i] + 1, i == n - 1 ? 
 :  );
56         else
57             printf("no solution
");
58     }
59 
60     return 0;
61 }

 
























以上是关于hdu 4337 King Arthur's Knights (Hamilton)的主要内容,如果未能解决你的问题,请参考以下文章

POJ3682King Arthur's Birthday Celebration(数学期望||概率DP)

hdu 5640 King's Cake(模拟)

hdu-5641 King's Phone (水题)

hdu-5642 King's Order(数位dp)

hdu 5642 King's Order(数位dp)

hdu 5641 King's Phone(暴力模拟题)