欧拉回路+并查集(判断图的联通)+Tire树(快速查找字符串,并且记录相应点的度数)
Tire树相关知识http://zh.wikipedia.org/wiki/Trie
#include <iostream> using namespace std; const int maxc = 26; const int maxn = 500001; struct TrieNode { int key; TrieNode * child[maxc]; TrieNode() { key=-1; memset(child,0,sizeof(child)); } }; int count[maxn]; TrieNode * root; int tot=-1; int insert(char * s) { if(root==NULL) root=new TrieNode; int did; TrieNode * tem=root; for(;*s!='\0';s++) { did=*s-'a'; if(tem->child[did]==NULL) tem->child[did]=new TrieNode; tem=tem->child[did]; } if(tem->key==-1) tem->key=++tot; count[tem->key]++; return tem->key; } struct UFSTree { int val; int parent; int rank; }t[maxn]; void init() { for(int i=0;i<maxn;i++) { t[i].val=i; t[i].parent=i; t[i].rank=0; } } int find(int k) { if(t[k].val!=t[k].parent) t[k].parent=find(t[k].parent); return t[k].parent; } void Union(int p1,int p2) { if(t[p1].rank>t[p2].rank) t[p2].parent=p1; else { t[p1].parent=p2; if(t[p1].rank==t[p2].rank) t[p2].rank++; } } int main() { char f[11],s[11]; init(); while(scanf("%s %s",f,s)!=EOF) { int k1=insert(f); int k2=insert(s); int p1=find(k1); int p2=find(k2); if(p1!=p2) Union(p1,p2); } if(root==NULL) { cout<<"Possible"<<endl; return 0; } int pa1=find(0); int flag=1,i; for(i=1;i<=tot;i++) { if(find(i)!=pa1) { flag=0; break; } } if(!flag) cout<<"Impossible"<<endl; else { int dam=0; for(i=0;i<=tot;i++) { if(count[i]%2!=0) dam++; } if(dam!=2&&dam!=0) cout<<"Impossible"<<endl; else cout<<"Possible"<<endl; } return 0; }