Question: If the ending Letter of word A is the same as the first letter of word B, it can be considered as A to B. Give a series of words to determine whether these words can be colluded according to a certain arrangement.
Question:
If you build a model based on the meaning of the question, and use words as vertices and edges to represent two-to-one communication, the Hamilton loop model will be obtained. It is obviously hard to solve.
In another way, if we use a letter as the vertex and an edge to represent the transmitted word, we can obtain the Euler Loop Model diagram, which can be solved according to the Euler's theorem.
The following provides knowledge about Euler's graph:
Euler Loop: a loop in G that passes through each side once and only once
Euler path: Path in G that passes through each edge once and only once
An undirected graph has the Euler loop theorem: when it is a connected graph + the vertex degree is an even number
An undirected graph has the Euler's path theorem: when it is a connected graph + except two vertices whose degree is odd, the others are even
A directed graph has the Euler loop theorem: when it is a connected graph + vertex inbound degree = outbound degree
A directed graph has the Euler's path theorem: when it is a connected graph + the absolute value of the difference between the inbound and outbound degrees of a vertex is smaller than 1, the rest are equal.
Code: # include <stdio. h>
# Include <string. h>
Const int N = 30;
Class UnionSet
{
Private:
Int parent [N];
Int rank [N];
Int size;
Public:
UnionSet (int _ size): size (_ size)
{
Init ();
}
~ UnionSet ()
{
}
Void init ()
{
For (int I = 0; I <size; ++ I)
{
Parent [I] =-1;
Rank [I] = 1;
}
}
Int root (int _ x)
{
Int r = _ x;
While (parent [r]> = 0)
R = parent [r];
Int I = _ x;
Int j;
While (parent [I]> = 0)
{
J = parent [I];
Parent [I] = r;
I = j;
}
Return r;
}
Int Union (int _ r1, int _ r2)
{
If (_ r1 = _ r2)
Return _ r1;
Else
{
Int root1 = root (_ r1 );
Int root2 = root (_ r2 );
If (root1 = root2)
Return root1;
If (rank [root1]> rank [root2])
{
Parent [root2] = root1;
Rank [root1] + = rank [root2];
}
Else
{
Parent [root1] = root2;
Rank [root2] + = rank [root1];
}
}
}
Int getRank (int _ x)
{
Return rank [_ x];
}
};
Char buf1 [1024];
Void Test ()
{
Int In [30] = {0 };
Int Out [30] = {0 };
Bool visited [30] = {false };
UnionSet Set (28 );
Int n;
Scanf ("% d", & n );
Bool flag = false;
Int start = 0;
For (int I = 0; I <n; ++ I)
{
Scanf ("% s", buf1 );
Int len = strlen (buf1 );
Set. Union (buf1 [0]-a, buf1 [len-1]-);
In [buf1 [len-1]-a] ++;
Out [buf1 [0]-a] ++;
Visited [buf1 [0]-a] = true;
Visited [buf1 [len-1]-a] = true;
If (! Flag)
{
Start = buf1 [0]-;
Flag = true;
}
}
For (int I = 0; I <26; ++ I)
{
If (I! = Start)
{
If (visited [I] & (Set. root (start )! = Set. root (I )))
{
Printf ("The door cannot be opened .");
Return;
}
}
}
Int cntIn = 0;
Int cntOut = 0;
For (int I = 0; I <26; ++ I)
{
If (visited [I])
{
If (In [I]! = Out [I])
{
If (In [I]-Out [I] =-1)
{
CntIn ++;
}
Else if (In [I]-Out [I] = 1)
{
CntOut ++;
}
Else
& Nbs