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
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.
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.
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".
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
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
1 2 3
1 4 2 3
C/C++ (1):
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 }
C/C++(2):
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 MAX = 300; 14 15 int mp[MAX][MAX], n, m, a, b, ans[MAX], book[MAX], pos; 16 17 bool hamilton() 18 { 19 memset(book, 0, sizeof(book)); 20 memset(ans, 0, sizeof(ans)); 21 pos = 1, book[0] = 1; 22 while (~pos) 23 { 24 ans[pos] ++; 25 while (ans[pos] < n) 26 if (!book[ans[pos]] && mp[ans[pos - 1]][ans[pos]]) break; 27 else ans[pos] ++; 28 if (ans[pos] < n && pos == n - 1 && mp[ans[pos]][ans[0]]) return true; 29 else if (ans[pos] < n && pos < n - 1) book[ans[pos ++]] = 1; 30 else 31 { 32 ans[pos --] = 0; 33 book[ans[pos]] = 0; 34 } 35 } 36 return false; 37 } 38 39 int main() 40 { 41 while (~scanf("%d%d", &n, &m)) 42 { 43 memset(mp, 0, sizeof(mp)); 44 while (m --) 45 { 46 scanf("%d%d", &a, &b); 47 -- a, -- b; 48 mp[a][b] = mp[b][a] = 1; 49 } 50 51 if (hamilton()) 52 { 53 int temp = n - 1; 54 for (int i = 0; i < temp; ++ i) 55 printf("%d ", ans[i] + 1); 56 printf("%d ", ans[temp] + 1); 57 } 58 else 59 printf("no solution "); 60 } 61 return 0; 62 }