Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4337
King Arthur's knights
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Special Judge
Problem descriptioni 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.
Inputthe 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.
Outputfor 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 31 22 31 34 41 42 42 31 3
Sample output
1 2 31 4 2 3
Source2012 multi-university training contest 4
Recommendzhoujiaqi2010
I have read the final report of other Daniel.
Http://www.cnblogs.com/jiai/archive/2012/08/03/2621040.html
Http://blog.sina.com.cn/s/blog_6ffc3bde01012lyj.html
I read the conclusion report of Daniel, and said it was a deep search. After reading it, I knocked on it myself. On the way, I did not see the correct conclusion report. I knocked it all the way. The sample has passed, submitted several times, and Wa several times. The reason is also found.
I think this is not the case when the question is done. A deep search is a hmap.
Happy!
Come on (^ ω ^ )!
//King Arthur's Knights#include <cstdio>#include <cstring>#include <vector>#include <iostream>#include <cstdio>using namespace std;vector <vector <int> >g;int a[200];int pos;int n, m;bool use[200];void init(){ g.clear(); g.resize(n + 1); pos = 0; memset(use, false, sizeof (use));}bool f(int x){ if (pos == n) { for (int j = g[1].size() - 1; j >= 0; j--) if (g[1][j] == x) return true; return false; } else { for (int j = g[x].size() - 1; j >= 0; j--) { if ( !use[ g[x][j] ]) { use[ g[x][j]] = true; a[pos++] = g[x][j]; if (f( g[x][j] )) return true; else { use[ g[x][j]] = false; pos--; } } } return false; }}int main(){ while (cin >> n >> m) { init(); { int u, v; for (int j = 0; j < m; j++) { scanf("%d%d", &u, &v); g[u].push_back(v); g[v].push_back(u); } pos = 1; use[1] = true; if (f(1)) { printf("1"); for(int j = 1; j < n; j++) printf(" %d", a[j]); printf("\n"); } else printf("no solution\n"); } } return 0;}