poj.org/problem?id=2513
欧拉路径裸体 唯一需要注意的是给每个颜色一个序号 这个可以使用哈希 但是我使用的是前缀树
#include <math.h>
#include <vector>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAX = 500010;
const int INF = 0x3f3f3f3f;
int cnt;
int deg[MAX],father[MAX];
struct trie{
bool Isword;
int next[26];
}node[MAX];
struct edge{
int v,next;
}E[MAX];
void Init(){
cnt = 1;
for(int i=0; i<MAX; i++){
memset(node[i].next, -1, sizeof(node[i].next));
node[i].Isword = false;
father[i] = i;
}
}
int Find(int x){
if(x != father[x])
father[x] = Find(father[x]);
return father[x];
}
int Insert(char *s){
int p = 0, n = strlen(s);
for(int i=0; i<n; i++){
if(node[p].next[s[i]-'a'] == -1)
node[p].next[s[i]-'a'] = cnt++;
p = node[p].next[s[i]-'a'];
}
node[p].Isword = true;
return p;
}
int main(){
char s1[15],s2[15];
Init();
memset(deg, 0, sizeof(deg));
while(scanf("%s%s",s1,s2) != EOF){
int u = Insert(s1), v = Insert(s2);
deg[u]++, deg[v]++;
father[Find(u)] = Find(v);
}
int num = 0;
for(int i=0; i<MAX; i++){
if(!deg[i]) continue;
if(father[i] == i) num++;
}
if(num > 1) puts("Impossible");
else{
int tmp = 0;
for(int i=0; i<MAX; i++){
if(!deg[i]) continue;
if(deg[i] & 1) tmp++;
}
if(!tmp || tmp == 2) puts("Possible");
else puts("Impossible");
}
return 0;
}