http://poj.org/problem?id=2513
Time Limit: 5000MS | Memory Limit: 128000K | |
Total Submissions: 37812 | Accepted: 9907 |
Description
Input
Output
Sample Input
blue red red violet cyan blue blue magenta magenta cyan
Sample Output
Possible
Hint
Source
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<map> 5 using namespace std; 6 int N; 7 struct node 8 { 9 node *next[26]; 10 int v; 11 node(){v=0;memset(next,NULL,sizeof(next));} 12 }*root; 13 int gec(char *s) 14 { 15 int len=strlen(s); 16 node *p=root; 17 for(int i=0;i<len;++i) 18 { 19 if(p->next[s[i]-‘a‘]==NULL) p->next[s[i]-‘a‘]=new node(); 20 p=p->next[s[i]-‘a‘]; 21 } 22 if(p->v==0) p->v=++N; 23 return p->v; 24 } 25 int cnt[500005]; 26 int f[500005]; 27 int getf(int v){return f[v]==v?v:f[v]=getf(f[v]);} 28 int main() 29 { 30 char s1[15],s2[15]; 31 int i; 32 for(i=1;i<=500000;++i)f[i]=i; 33 root=new node(); 34 while(scanf("%s%s",s1,s2)!=EOF){ 35 int u=gec(s1); 36 int v=gec(s2); 37 int fu=getf(u),fv=getf(v); 38 if(fu!=fv) f[fv]=fu; 39 cnt[u]++; 40 cnt[v]++; 41 } 42 int s=0,x=0; 43 for(i=1;i<=N;++i) 44 { 45 if(cnt[i]%2==1) s++; 46 if(i==getf(i))x++; 47 } 48 if(x<=1&&(s==0||s==2)) puts("Possible"); 49 else puts("Impossible"); 50 return 0; 51 }
原文:http://www.cnblogs.com/zzqc/p/7528616.html