Question: given some sticks, both ends of the sticks are painted with color, to determine whether they can be connected to the beginning and end of the sticks, a straight line, requiring different sticks to be connected to one side must be the same color.
Go to: kuangbing
The necessary and sufficient condition for an undirected graph to have an Euler's path is:
① The graph is connected;
② All nodes have an even degree or only two nodes with an odd degree.
Graph connectivity can be determined by querying sets.
It is easier to calculate the degree.
View code // trie is written with a pointer for the first time. It was originally a two-dimensional array and found that the array could not be opened, so we had to delete it and change it to a pointer. // to solve this problem, we knew the Euler's loop determination, trie # include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <queue> using namespace STD; const int n = 500010; char S1 [15], S2 [15]; int Fa [N], U, V, in [N], color = 1; struct trie {trie * Ch [26]; int val; void Init () {for (INT I = 0; I <26; I ++) CH [I] = NULL; val = 0;} trie () {for (INT I = 0; I <26; I ++) CH [I] = NULL; val = 0;} int idx (char c) {return C-'A';} int ins_find (char * s, trie * t) {int n = strlen (s); For (INT I = 0; I <n; I ++) {int c = idx (s [I]); If (! T-> CH [c]) {T-> CH [c] = new trie ();} t = T-> CH [c];} If (! T-> Val) T-> val = color ++; return T-> Val ;}} * TR; int find (int x) {If (Fa [x] <0) return X; return Fa [x] = find (Fa [x]);} int main () {// freopen ("in.txt", "r", stdin); memset (FA,-1, sizeof (FA); Tr = new trie (); while (scanf ("% S % s", S1, S2)> 0) {u = tr-> ins_find (S1, TR), V = tr-> ins_find (S2, tr); in [u] ++, in [v] ++; u = find (u), V = find (V); If (u! = V) Fa [u] = V;} int num1 = 0, num2 = 0; For (INT I = 1; I <color; I ++) {If (in [I] & 1) num1 ++; If (Fa [I] =-1) num2 ++ ;} if (num1 = 0 | num1 = 2) & num2 <2) puts ("possible"); else puts ("impossible"); Return 0 ;}