Many errors have been handed over dozens of times. Haha ~
Summary:
RE: array out of bounds
WA: spelling of letters
Create a trie tree and add a query set to query graph connectivity and easily record the degree of each vertex.
View Code
// Runtime error cause: values are assigned from 1 to MAX during initialization. The array is out of bounds. Remember this !!!
// WA cause: Impossible misspelling .. Success!
# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
# Include <iostream>
Using namespace std;
Const int MAX = 500010;
Class trie {
Public:
Bool flag;
Int id;
Trie * child [27];
Trie ()
{
Flag = false;
Id = 0;
Memset (child, 0, sizeof (child ));
}
} Root;
Int color = 0;
Int degree [MAX];
Int p [MAX];
Int find (int x) {return p [x] = x? X: p [x] = find (p [x]);}
Void unio (int a, int B)
{
Int x = find ();
Int y = find (B );
If (x! = Y)
P [x] = y;
}
Int insert (char * s)
{
Class trie * cur = & root;
Int len = strlen (s );
For (int I = 0; I <len; I ++)
{
Int id = s [I]-'A ';
If (! Cur-> child [id])
Cur-> child [id] = new trie;
Cur = cur-> child [id];
}
If (cur-> flag) return cur-> id;
Else
{
Cur-> flag = true;
Cur-> id = ++ color;
Return cur-> id;
}
}
Int main ()
{
Int I, j;
Char s1 [11], s2 [11];
For (I = 1; I <= 500000; I ++)
{
P [I] = I;
Degree [I] = 0;
}
While (scanf ("% s", s1, s2 )! = EOF)
{
I = insert (s1 );
J = insert (s2 );
Degree [I] ++;
Degree [j] ++;
Unio (I, j );
}
Int r = find (1 );
Int num = 0;
For (j = 1; j <= color; j ++)
{
If (degree [j] % 2 = 1) num ++;
If (num> 2)
{
Printf ("Impossible \ n ");
Return 0;
}
If (find (j )! = R)
{
Printf ("Impossible \ n ");
Return 0;
}
}
If (num = 1) printf ("Impossible \ n ");
Else printf ("Possible \ n ");
Return 0;
}