Description
Input
Output
Sample Input
blue red red violet cyan blue blue magenta magenta cyan
Sample Output
Possible
Hint
#include<stdio.h> typedef struct nn { int flag; int indx; struct nn *next[26]; }node; node *builde() { node *p=new node; p->flag=0; for(int i=0;i<26;i++) p->next[i]=NULL; return p; } node *root; int jdn;//计算顶点为奇数的个数 int insert(char str[],int n) { node *p=root; for(int i=0;str[i]!='\0';i++) { if(p->next[str[i]-'a']==NULL) p->next[str[i]-'a']=builde(); p=p->next[str[i]-'a']; } if(p->flag==0) p->indx=n; p->flag++; if((p->flag)%2)jdn++;else jdn--; return p->indx; } int fath[5100000],dn;//dn为边数和点数之差 int findfather(int x) { if(x!=fath[x]) fath[x]=findfather(fath[x]); return fath[x]; } void set(int a,int b) { a=findfather(a); b=findfather(b); if(a!=b) { dn--; fath[a]=b; } } int main() { char c1[1000],c2[1000]; int n=0,a,b; dn=0; jdn=0; root=builde(); while(scanf("%s%s",c1,c2)>0) { a=insert(c1,n+1); if(a==n+1) { dn++; n++; fath[n]=n; } b=insert(c2,n+1); if(b==n+1) { dn++; n++; fath[n]=n; } set(a,b); } if((jdn==2||jdn==0)&&dn==1||n==0) printf("Possible\n"); else printf("Impossible\n"); }
poj2513Colored Sticks(欧拉通路+字典树+并查集),布布扣,bubuko.com
poj2513Colored Sticks(欧拉通路+字典树+并查集)
原文地址:http://blog.csdn.net/u010372095/article/details/38140173