The two ends of a wooden stick are painted with color. Now there are a series of wooden sticks, asking if they can be connected into a straight line, so that the combination points of the two wooden sticks are in the same color.
Solution: The color is the node and the wooden stick is the edge. The color is numbered by the dictionary tree, and the connectivity of the graph is verified by the query set. The degree of each color is calculated to determine whether the color is a Euclidean or Euler's half.
# Include <iostream> using namespace STD; const int kind = 26; const int max = 500005; int color [Max], Father [Max]; int node_num = 0, color_id = 0; struct tree {int key; tree * Next [kind];} node [1000005], root; int insert (char * Word) {tree * location = & root; int I = 0, ID; while (word [I]) {id = word [I]-'A'; If (location-> next [ID] = NULL) location-> next [ID] = & node [++ node_num]; location = Location-> next [ID]; ++ I;} If (location-> key = 0) location-> key = ++ color_id; return location-> key;} int find_set (INT X) /* it took more than an hour to change! Compare the code in the next section with the query set. */{If (father [x]> = 0) Father [x] = find_set (father [x]); if (father [x] <0) return X; elsereturn father [X];} void Union (int x, int y) {int Tx = find_set (X ); int ty = find_set (y); If (Tx = ty) return;/* Number of root node storage nodes, other child nodes store their parent nodes */If (father [TX] <father [ty]) {FATHER [TX] + = Father [ty]; father [ty] = TX ;} else {FATHER [ty] + = Father [TX]; father [TX] = ty ;}} bool check () {int cnt1 = 0, cnt2 = 0; (INT I = 1; I <= color_id; ++ I) {If (father [I] <0) ++ cnt1; // cnt1 = 1 ensure that only one Unicom branch if (color [I] % 2! = 0) ++ cnt2; // cnt2 = Ensure that there is an Euler's path or Euler's circuit} If (cnt2! = 2 & cnt2! = 0) | cnt1> 1) return false; return true;} int main () {int A, B; char str1 [11], str2 [11]; memset (father,-1, sizeof (father); memset (color, 0, sizeof (color); memset (node, null, sizeof (node )); root = node [0]; while (scanf ("% S % s", str1, str2 )! = EOF) {A = insert (str1); B = insert (str2); color [a] ++; color [B] ++; Union (a, B );} if (check () printf ("Possible \ n"); elseprintf ("impossible \ n"); Return 0 ;}
Below are the differences and query sets:
# Include <iostream> using namespace STD; const int kind = 26; const int max = 500005; int color [Max], Father [Max], R [Max]; int node_num = 0, color_id = 0; struct tree {int key; tree * Next [kind]; tree () {key = 0; For (INT I = 0; I <kind; ++ I) next [I] = NULL ;}} node [1000005], root; int insert (char * Word) {tree * location = & root; int I = 0, ID; while (word [I]) {id = word [I]-'A'; If (location-> next [ID] = NUL L) location-> next [ID] = & node [++ node_num]; location = Location-> next [ID]; ++ I ;} if (location-> key = 0) {++ color_id; location-> key = color_id; return location-> key;} else return location-> key ;} int find_set (int x)/* Put This function directly in the previous code at the beginning, and the result is incorrect */{If (father [x]! = X) Father [x] = find_set (father [x]); Return father [X];} void Union (int x, int y) {int Tx = find_set (x); int ty = find_set (y); If (Tx = ty) return; If (R [TX]> r [ty]) father [ty] = TX; elsefather [TX] = ty; If (R [TX] = R [ty]) R [ty] ++;} bool check () {int CNT = 0; int mark = find_set (1); For (INT I = 1; I <= color_id; ++ I) {If (find_set (I )! = Mark) return false; If (color [I] % 2! = 0) ++ CNT;} If (CNT! = 2 & CNT! = 0) return false; else return true;} int main () {int A, B; char str1 [11], str2 [11]; for (INT I = 0; I <Max; ++ I) {FATHER [I] = I; color [I] = R [I] = 0;} while (scanf ("% S % s ", str1, str2 )! = EOF) {A = insert (str1); B = insert (str2); color [a] ++; color [B] ++; Union (a, B );} if (check () printf ("Possible \ n"); elseprintf ("impossible \ n"); Return 0 ;}