Nyoj word concatenation (query set judgment connectivity + Euler path)
This question is still difficult.
First, in terms of graph creation, if words are simply used as vertices and the link that can be spliced as edges, It is a Hamilton Graph (each point can only go once), which is difficult.
In other words, each word is regarded as a directed edge and pointed to the last letter by the first letter of the word.
So this is the problem of ouyi.
Search is used in essence, but in order to quickly obtain the Euler path with the smallest Lexicographic Order, you must first sort the word set.
After sorting, save the graph with the number of edge sets, calculate the outbound and inbound degrees of each point, and judge the connectivity of the base graph (without considering the direction of the edge, determine whether there is an Euler loop or Euler path.
If there is an Euler loop, the search starts from 0;
If there is an Euler path, search from the "outbound = inbound + 1" point.
The reference code is as follows:
Number of edge set groups save graph + and check set to determine connectivity
# Include
Using namespace std; # include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Includestruct Enode {int u; int v; string s ;}; Enode edge [1010]; string s [1010]; int n, m, fa [1010]; int flag [1010]; int path [1010]; int visit [1010]; int root (int x) {if (fa [x] = x) return x; return fa [x] = root (fa [x]);} void uset (int x, int y) {int a = root (x ); int B = root (y); if (! = B) fa [B] = a;} // check the set to determine the connectivity int judge () {int t = 0; for (int I = 0; I <26; ++ I) if (flag [I] & fa [I] = I) t ++; return t = 1 ;} void find (int u) {for (int I = 0; I
> T; while (T --) {memset (visit, 0, sizeof (visit); memset (flag, 0, sizeof (flag); // memset (edge, 0, sizeof (edge); memset (path, 0, sizeof (path); memset (fa, 0, sizeof (fa); cin> n; int I; for (I = 0; I
> S [I];} sort (s, s + n); for (I = 0; I <26; ++ I) fa [I] = I; int in [30], out [30]; memset (in, 0, sizeof (in); memset (out, 0, sizeof (out )); for (I = 0; I
0; -- I) cout <edge [path [I]. s <". "; cout <edge [path [0]. s <endl;} else cout <"***" <endl;} return 0 ;}