poj 2513 Colored Sticks( 字典树哈希+ 欧拉回路 + 并查集)

时间:2022-06-05 10:20:20

题目:http://poj.org/problem?id=2513

参考博客:http://blog.csdn.net/lyy289065406/article/details/6647445

http://www.cnblogs.com/LK1994/p/3263462.html

 #include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<queue>
#include<iomanip>
#include<cmath>
#include<map>
#include<vector>
#include<algorithm>
#define N 500010
using namespace std; int degree[N],bin[N],id = ; struct tree
{
int flag,id;
struct tree* next[];
}*root;
struct tree* creat()
{
struct tree *p=(struct tree*)malloc(sizeof(struct tree));
p->flag =;
for(int i = ; i < ; i++)
p->next[i] = NULL;
return p;
} int find(int x)
{
if(bin[x] != x)
bin[x] = find(bin[x]);
return bin[x];
} int hash(char s[])
{
struct tree *p = root;
for(int i = ; s[i]; i++)
{
if(p->next[s[i]-'a'] == NULL)
p->next[s[i]-'a'] = creat();
p = p->next[s[i]-'a'];
}
if(p->flag != )
{
p->flag = ;
p->id = id++;
}
return p->id;
} int check()
{
int sum = ;
int x = find();
for(int i = ; i < id; i++)
if(find(i) != x)
return ;
for(int i = ; i < id; i++)
{
if(degree[i]%)
sum++;
}
if(sum == || sum == )
return ;
return ;
} int main()
{
int u,v,x,y;
char s1[],s2[];
memset(degree,,sizeof(degree));
for(int i=; i<=; i++)
bin[i]=i;
root=creat();
while(scanf("%s %s",s1,s2)!=EOF)
{
u =hash(s1); v =hash(s2);
degree[u]++; degree[v]++;
x=find(u);
y=find(v);
if(x!=y)
bin[x]=y;
}
if(check()) printf("Possible\n");
else printf("Impossible\n");
return ;
}