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

思路很容易想到就是判断欧拉通路

预处理时用字典树将每个单词和数字对应即可

刚开始在并查集处理的时候出错了

代码:

 #include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
using namespace std;
int color;
#define maxn 26
#define MAX 500010
int parent[MAX];
int degree[MAX];
class Trie
{
public:
bool flag;
int id;
Trie *next[maxn];
};
Trie *root=new Trie;
void init()
{
memset(degree,,sizeof(degree));//存放各个点的度数
memset(parent,-,sizeof(parent));
Trie *q=root;//字典树初始化
for(int i=;i<maxn;i++)
q->next[i]=NULL;
q->flag=false;
q->id=;
}
/* 并查集构建并判断是否为连通图*/
int find(int x)
{
if(parent[x]==-) return x;
return parent[x]= find(parent[x]); } void Union(int u,int v)
{
int r1=find(u);
int r2=find(v); if(r1!=r2)
parent[r1]=r2;
}
int insert(char *s)
{
Trie *p =root; for(int i=; s[i]!='\0' ;i++)
{
int d=s[i]-'a'; if(p->next[d]==NULL)
{
Trie *temp=new Trie;
for(int j=;j<maxn;j++)
temp->next[j]=NULL;
temp->flag=;
temp->id=;
p->next[d]=temp;
}
p=p->next[d]; }
if(p->flag)
{
return p->id;
}
else
{
p->flag=;
p->id=color++;
return p->id;
} }
void del_trie(Trie * p)
{
for(int i=;i<maxn;i++)
if(p->next[i])
del_trie(p->next[i]); free(p);
}
int main()
{
char s1[],s2[];
init();
color=;
while(scanf("%s%s",s1,s2)!=EOF)
{
int num1=insert(s1);
int num2=insert(s2); degree[num1]++;
degree[num2]++;
Union(num1,num2); }
int cnt1=,cnt2=;
for(int i=;i<color;i++)//判断是佛否含欧拉通路
{
if(parent[i]==-) cnt1++;
if(degree[i]%==) cnt2++;
if(cnt1>) break;
if(cnt2>) break;
} if((cnt1== || cnt1==) &&(cnt2== || cnt2==))
printf("Possible\n");
else printf("Impossible\n");
// del_trie(root);
return ; }

poj2513- Colored Sticks 字典树+欧拉通路判断的更多相关文章

  1. poj 2513 连接火柴 字典树+欧拉通路 好题

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27134   Accepted: 7186 ...

  2. POJ2513:Colored Sticks(字典树+欧拉路径+并查集)

    http://poj.org/problem?id=2513 Description You are given a bunch of wooden sticks. Each endpoint of ...

  3. poj2513 Colored Sticks —— 字典树 + 并查集 + 欧拉回路

    题目链接:http://poj.org/problem?id=2513 题解:通过这题了解了字典树.用字典树存储颜色,并给颜色编上序号.这题为典型的欧拉回路问题:将每种颜色当成一个点.首先通过并查集判 ...

  4. hdu1116有向图判断欧拉通路判断

    Play on Words Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  5. 欧拉回路&欧拉通路判断

    欧拉回路:图G,若存在一条路,经过G中每条边有且仅有一次,称这条路为欧拉路,如果存在一条回路经过G每条边有且仅有一次, 称这条回路为欧拉回路.具有欧拉回路的图成为欧拉图. 判断欧拉通路是否存在的方法 ...

  6. Colored Sticks (字典树哈希+并查集+欧拉路)

    Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27704   Accepted: 7336 Description You ...

  7. POJ2513——Colored Sticks(Trie树+欧拉回路+并查集)

    Colored Sticks DescriptionYou are given a bunch of wooden sticks. Each endpoint of each stick is col ...

  8. POJ 2513 Colored Sticks 字典树、并查集、欧拉通路

    Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some ...

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

    题目:http://poj.org/problem?id=2513 参考博客:http://blog.csdn.net/lyy289065406/article/details/6647445 htt ...

随机推荐

  1. CentOs6系统安装mailx发邮件

    1. yum -y mail* sendmail* postfix* service sendmail start 2. cp /etc/mail.rc /etc/mail.rc.bak cat &g ...

  2. Jenkis Editable Email Notification Plugin 使用介绍

    Jenkis Editable Email Notification Plugin 使用介绍 前言 Jenkins本身提供的Email插件功能实在有限,只能提供当前Job的基本信息,比如成功.失败以及 ...

  3. Java EE基础之JSP(三)

         原来准备简单点,用一篇文章介绍完JSP的,没想到细节很多.这是第三篇也是最后一篇,这三篇文章介绍了基本的JSP知识,很多名词概念和用法,一定要在实践项目中使用之后才会有更加亲切的体会.第三篇 ...

  4. ArcEngine保存栅格数据至rastercatalog

    将栅格数据(IRasterDataset)直接保存到数据库中很常见,但是保存到栅格目录下就比较少见,好不容易才找到,在这里记录一下. public void saveRasterDs2Catalog( ...

  5. WSDL/WebService/SOAP/REST/AXIS/CXF 开放式服务

    WebService是一种数据交换标准.通过WebService标准,你可以把项目中的方法作为接口提供给其他项目使用. 有时候我们习惯性地将具体提供服务的某个方法称为WebService.比如图书系统 ...

  6. javascript执行顺序小结

    作为web开发人员,一定要对js的执行顺序,解析原理有一定了解,否则无法掌控这门小巧好用的语言 javascript是一门实现网页动态效果的语言,也是主要负责和服务端的交互,他抛弃了像java中类的束 ...

  7. Andorid自动读取短信验证码

    手机收到验证码短信后,程序自动识别验证码并填充验证码输入框. 思路是有了,实现的方式也有多种: 1.开启一个线程,隔一段时间就去查询收件箱是否有变化,有变化再读取出来做处理. 2.注册一个短信变化的广 ...

  8. a标签传值乱码问题怎么解?

  9. c#控制台实现post网站登录

    如题,首先我写了一个web页面来实现post登陆,只做演示,代码如下 public void ProcessRequest(HttpContext context) { context.Respons ...

  10. require.js入门

    小颖目前所在的公司在用require.js,小颖一只说要写个小demo,今天抽空把自己写的小demo分享出来,希望对初学者有一些帮助,嘻嘻 学习资料: CSDN上的一篇文章:使用RequireJS优化 ...