标签:des style blog color 使用 os strong io
Description
Input
Output
Sample Input
blue red red violet cyan blue blue magenta magenta cyan
Sample Output
Possible
Hint
#include <cstdio> #include <cstring> int p[520000] , top , q[520000]; struct node{ int flag ; node *next[27] ; } *head; node *newnode() { node *p = new node ; p->flag = 0; for(int i = 0 ; i < 27 ; i++) p->next[i] = NULL ; return p; } int gettree(node *head,char *s) { int i , l = strlen(s) ; node *p = head ; for(i = 0 ; i < l ; i++) { int k = s[i] - 'a' ; if(p->next[k]==NULL) p->next[k] = newnode(); p = p->next[k] ; } if( p->flag == 0 ) p->flag = top++ ; return p->flag ; } int f(int x) { int r , k , l ; r = x ; while( r != p[r] ) r = p[r] ; k = x ; while( k != r ) { l = p[k] ; p[k] = r ; k = l ; } return r ; } void add(int u,int v) { u = f(u) ; v = f(v) ; if( u != v ) p[u] = v ; } char s1[11] , s2[11] ; int main() { int i , n , k1 , k2 ; memset(q,0,sizeof(q)); head = newnode() ; for(i = 0 ; i <= 520000 ; i++) p[i] = i ; top = 1 ; while( scanf("%s %s", s1, s2 ) !=EOF ) { k1 = gettree(head,s1); k2 = gettree(head,s2); q[k1]++ ; q[k2]++ ; add(k1,k2) ; } int k = f(1) ; for(i = 2 ; i < top ; i++) if( k != f(i) ) break; if( i < top ) printf("Impossible\n"); else { int ji = 0 ; for(i = 1 ; i < top ; i++) if( q[i]%2 ) ji++ ; if(ji ==0 || ji == 2) printf("Possible\n"); else printf("Impossible\n"); } return 0; }
测试赛A - Colored Sticks(并查集+字典树+欧拉回路),布布扣,bubuko.com
测试赛A - Colored Sticks(并查集+字典树+欧拉回路)
标签:des style blog color 使用 os strong io
原文地址:http://blog.csdn.net/winddreams/article/details/38300167