poj 2513 Colored Sticks( 字典树哈希+ 欧拉回路 + 并查集)
2024-10-12 18:14:30
题目: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 ;
}
最新文章
- 解决 git 提交文件提示 Filename too long 问题
- C# 线程(六):定时器
- how to reset mac root password
- Floyd-Warshall算法详解(转)
- php yield
- JAVAEE学习路线分享
- BestCoder Round #32_1001 以及 hdu 5182
- [转]Linux 基本操作(RM 删除)
- spring生命周期流程图
- Go Revel - i18n(国际化)
- Bodymovin:Bodymovin和Lottie:把AE动画转换成HTML5/Android/iOS原生动画
- PyQt5--QPixmap
- MySQL中模拟oracle中的rownum列
- TensorFlow相关的一些技巧
- python 多线程删除MySQL表
- vc 使窗口置顶 在最前面
- AIMR 固定收益推荐读物
- CLR VIA C# 泛型的协变和逆变
- MySQL隐式转换测试
- 如何将查询到的数据显示在DataGridView中