This question should be applied to the knowledge of Euler's road, and you should also check the set. The question is: give you n words and you need to determine whether these words can be connected at the beginning and end.
After understanding the meaning of a question, convert it, input a string, and extract the first letter as a subscript to indicate the emergence of two nodes, and increase the inbound and outbound degrees of the corresponding nodes,
You can convert it to an application that queries the set. You can imagine a graph composed of the first letter nodes. if and only when the graph is an Euler loop or Euler path,
In order to meet the requirements of the subject, the determination of Euler's circuit and Euler's path can be summarized as follows:
1) connect all points
2) the inbound and outbound values in the Euler's loop are the same.
3) The inbound-outbound degree of the start point in the Euler's path is 1, the initial degree of the end point is 1, and all other points are inbound;
With these knowledge points, I believe it is easier to understand. The following code is: 1 # include <stdio. h>
2 # include <string. h>
3 # include <math. h>
4 # define N 30
5 /*
6 Euler's loop. All vertices are connected, and the inbound degrees of all vertices are equal to the outbound degrees.
7. Euler's path. Start from origin S and go through all vertices.
8. All vertices have an even degree except the start and end points, and the exit degree is equal to the inbound level.
9. The starting point is 1 higher than the inbound level.
10. The inbound ratio of the endpoint is greater than the outbound value by 1.
11 */
12
13 int father [N], vis [N];
14 // father [I] indicates node I's BOSS! Vis [I] indicates that node I has appeared!
15 int findx (int x)
16 {// find the BOSS of node x!
17 if (father [x]! = X)
18 father [x] = findx (father [x]);
19 return father [x];
20}
21 void merge (int a, int B)
22 {// merge node a and Node B!
23 int x, y;
24 x = findx ();
25 y = findx (B );
26 if (x! = Y) father [x] = y;
27}
28 int main ()
29 {
30 int text, cnt, I, j, n, out [N], in [N], p [30], a, B;
31 char str [1001];
32 scanf ("% d", & text );
33 while (text --)
34 {
35 scanf ("% d", & n );
36 memset (out, 0, sizeof (out ));
37 memset (in, 0, sizeof (in ));
38 memset (vis, 0, sizeof (vis ));
39 for (I = 0; I <26; I ++)
40 father [I] = I; // initialize the Array
41 while (n --)
42 {// process the given information!
43 scanf ("% s", str );
44 a = str [0]-'A ';
45 B = str [strlen (str)-1]-'A ';
46 merge (a, B );
47 out [a] ++;
48 in [B] ++; // record the inbound and outbound degrees of nodes a and B
49 vis [a] = 1;
50 vis [B] = 1; // mark the appearance of nodes a and B
51}
52 for (I = 0; I <26; I ++)
53 father [I] = findx (I); // find the BOSS of each node
54 for (cnt = 0, I = 0; I <26; I ++)
55 if (vis [I] & father [I] = I)
56 cnt ++; // count the number of final BOSS nodes, that is, the root node.
57 if (cnt> 1) // the graph is not connected.
58 {
59 printf ("The door cannot be opened. \ n ");
60 continue;
61}
62
63 for (j = 0, I = 0; I <26; I ++)
64 if (vis [I] & out [I]! = In [I])
65 p [j ++] = I; // collects information about points with Different Inbound and Outbound degrees.
66 if (j = 0)
67 {// Euler loop, I .e. Ring
68 printf ("Ordering is possible. \ n ");
69 continue;
70}
71 if (j = 2 & (out [p [0]-in [p [0] = 1 & in [p [1]-out [p [1] = 1
72 | out [p [1]-in [p [1] = 1 & in [p [0]-out [p [0] = 1 ))
73 {// Euler's path
74 printf ("Ordering is possible. \ n ");
75 continue;
76}
77 printf ("The door cannot be opened. \ n ");
78}
79 return 0;
80}
81