题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=11158
Time Limit: 5000MS | Memory Limit: 128000KB | 64bit IO Format: %I64d & %I64u |
Description
Input
Output
Sample Input
blue red red violet cyan blue blue magenta magenta cyan
Sample Output
Possible
Hint
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int maxn=5e5+5; struct node{ int num; bool is_word; node *next[26]; node(){ num=is_word=0; memset(next,NULL,sizeof(next)); } }; node *root; int n,c[maxn],f[maxn]; void insert(char s[]){ node *p=root; for(int i=0;s[i];i++){ int dex=s[i]-'a'; if(p->next[dex]==0) p->next[dex]=new node(); p=p->next[dex]; } if(p->is_word==0){ // important! because of num++ p->num=++n; //num is dex of string p->is_word=1; } } int find(int x){ if(x==f[x]) return x; f[x]=find(f[x]); return f[x]; } int getdex(char s[]){ node *p=root; for(int i=0;s[i];p=p->next[s[i]-'a'],i++); // not need length return p->num; } int main() { //freopen("cin.txt","r",stdin); n=0; memset(c,0,sizeof(c)); memset(f,0,sizeof(f)); root=new node(); char a[15],b[15]; int adex,bdex; while(~scanf("%s%s",a,b)){ insert(a); insert(b); adex=getdex(a); bdex=getdex(b); c[adex]++; //all count c[bdex]++; if(!f[adex]) f[adex]=adex; // U gather。 if(!f[bdex]) f[bdex]=bdex; f[find(adex)]=f[find(bdex)]; } int oddsum=0; bool judge=1; for(int i=1;i<=n;i++){ if(c[i]&1) oddsum++; if(find(i)!=f[1]){ judge=0; break; } } if((oddsum==0 || oddsum==2)&&judge) puts("Possible"); else puts("Impossible"); return 0; }