#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N=250005*4;
char s1[15],s2[15],cnt;
int chd[N][26],v[N],ID[128],sz=1,du[N],fa[N],color=1;
void init(){
sz=1;
color=1;
memset(v,0,sizeof(v));
memset(chd,0,sizeof(chd));
for(int i=0;i<N;i++)fa[i]=i;
for(int i=0;i<26;i++)
ID[i+'a']=i;
}
int trie(char *s){
int cur=0;
for(int i=0;s[i];i++){
if(!chd[cur][ID[s[i]]])
chd[cur][ID[s[i]]]=++sz;
cur=chd[cur][ID[s[i]]];
}
if(!v[cur]) v[cur]=color++;
return v[cur];
}
int Find(int x){
while(x!=fa[x])
x=fa[x]=fa[fa[x]];
return x;
}
void Merge(int x,int y){
//if(Find(x)==Find(y)) return ;
fa[Find(y)]=Find(x);
}
int main()
{
init();
while(scanf(" %s %s",s1,s2)!=EOF){
int x=trie(s1),y=trie(s2);
//printf("%s:%d %s:%d\n",s1,x,s2,y);
Merge(x,y);
du[x]++;du[y]++;
}
int root=1,k=0,tmp=Find(1);
for(int i=1;i<color;i++){
//printf("%ddu:%d fa:%d\n",i,du[i],fa[i]);
if(tmp!=Find(i))root++;
if(du[i]&1)k++;//奇度
}
//printf("root:%d k:%d\n",root,k);
if(root>1 || k==1 || k>2)cout<<"Impossible"<<endl;
else cout<<"Possible"<<endl;
return 0;
}
poj2513Colored Sticks trie+merge_set+Eular
最新推荐文章于 2016-11-05 00:35:17 发布