HDU 4337 King Arthur's knights (graph theory + DFS) =

Source: Internet
Author: User
Tags cmath
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 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 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 done using the Hamilton loop, but that premise makes it possible that no is unsolvable, so the deep search with backtracing can pass, this is the same as the classic prime ring. feelings: 1. First, create an undirected graph. Thank you !!! 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 \ n", POs, I ); DFS (I, POS + 1); // If (flag = 1) // break; vis [I] = false ;}}} int main () {int A, B; while (scanf ("% 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", & A, & B ); re [a] [B] = true; Re [B] [a] = true;} OK [1] = 1; DFS (1, 2); If (! Flag) printf ("no solution \ n");} return 0 ;}

8870377

2013-08-08 10:41:14

Accepted

4337

31 Ms

252 K

1272 B

C ++

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 \ n ", POs, I); DFS (I, POS + 1); // If (flag = 1) // break; vis [I] = false ;}}} int main () {int A, B; while (scanf ("% 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", & 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 ;}

8872033

13:20:07

accepted

4337

31 Ms

252 k

1198 B

C ++

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.