1 // accepted 248 Kb 125 MS 2 // Euler loop 3 // fixed point with 26 letters, A word is an edge 4 from the first letter to the end of the letter // below is the directed graph to determine the Euler Loop 5 // connectivity + node inbound degree and = outbound degree and or there is a pair of nodes 1 larger than the degree of exit, A small 1 6 # include <cstdio> 7 # include <cstring> 8 # include <iostream> 9 # include <queue> 10 using namespace STD; 11 const int imax_n = 30; 12 int a [imax_n] [imax_n]; 13 bool used [imax_n]; 14 bool vis [imax_n]; 15 int cnt_in [imax_n], cnt_out [imax_n]; 16 int N; 17 char s [2, 1005]; 18 Qu EUE <int> q; 19 void BFS (INT s) 20 {21 While (! Q. empty () Q. pop (); 22 // If (used [s] = 0) return 0; 23 Q. push (s); 24 vis [s] = 1; 25 while (! Q. empty () 26 {27 int x = Q. front (); 28 Q. pop (); 29 for (INT I = 1; I <imax_n; I ++) 30 {31 if (used [I] = 1 &&! Vis [I] & A [x] [I]) 32 {33 vis [I] = 1; 34 Q. push (I); 35} 36} 37} 38} 39 bool judge () 40 {41 int flag; 42 for (INT I = 1; I <= 26; I ++) 43 {44 memset (VIS, 0, sizeof (VIS); 45 BFS (I); 46 flag = 1; 47 for (Int J = 1; j <= 26; j ++) 48 if (used [J] = 1 &&! Vis [J]) Flag = 0; 49 If (flag = 1) return 1; 50} 51 return 0; 52} 53 bool slove () 54 {55 int P, ne; 56 p = ne = 0; 57 for (INT I = 1; I <= 26; I ++) 58 {59 int T = cnt_in [I]-cnt_out [I]; 60 if (t = 0) continue; 61 If (t = 1) 62 {63 P ++; 64 if (P> = 2) return 0; 65 continue; 66} 67 If (t =-1) 68 {69 ne ++; 70 If (ne> = 2) return 0; 71 continue; 72} 73 return 0; 74} 75 if (! (P = 1 & Ne = 1 | P = 0 & Ne = 0) return 0; 76 return 1; 77} 78 int main () 79 {80 int t; 81 scanf ("% d", & T); 82 while (t --) 83 {84 scanf ("% d", & N ); 85 int X, Y; 86 memset (A, 0, sizeof (a); 87 memset (used, 0, sizeof (used); 88 memset (cnt_in, 0, sizeof (cnt_in); 89 memset (cnt_out, 0, sizeof (cnt_out); 90 for (INT I = 0; I <n; I ++) 91 {92 scanf ("% s", S); 93 int L = strlen (s); 94 X = s [0]-'A' + 1; 95 y = s L-1]-'A' + 1; 96 used [s [0]-'A' + 1] = true; 97 used [L-1]-'A' + 1] = true; 98 A [x] [Y] = 1; 99 cnt_out [x] ++; 100 cnt_in [y] ++; 101} 102 If (Judge () = 1 & slove () = 1) 103 printf ("ordering is possible. \ n "); 104 else105 printf (" The door cannot be opened. \ n "); 106} 107 return 0; 108}View code
Hdu1116 Euler Loop