题目链接: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. strspn 和strcspn

    1.strcspn头文件:#inclued<string.h>定义函数:size_t strcspn(const char *s, const char * reject);函数说明:st ...

  2. nginx错误记录

    症状: 安装phpBB3.1的最后一步完成安装之后,注册用户,浏览器崩溃.localhost的所有页面都打不开同时没有响应. Trace: 虽然打开了nginx.exe,但是进程中未发现服务. 重新电 ...

  3. 服务器证书安装配置指南(SLB)

    一.生成证书请求  1.下载CSR生成工具 您需要使用CSR生成工具来创建证书请求.  下载AutoCSR: http://www.itrus.cn/soft/autocsr.rar  2.生成服务器 ...

  4. python服务器环境搭建(3)——参数配置

    前面我们已安装好了python服务器运行所需要的相关软件,而最重要最繁琐的就是参数配置,写这篇就踩了好多坑,花了好多时间,遇到了各种各样的问题.好了费话少说,直接进入本篇话题. PS:本人不是专业的运 ...

  5. IOS开发创建开发证书及发布App应用(六)——打包应用

    6.打包应用 如下图,生成之后点击下面红框的按钮,按时间排序,然后点最新的一次生成 从右侧生成日志中找到如下图红框标识的部分,找到 -output ,把下面浅蓝色选中,这是app生成的文件夹路径 点击 ...

  6. 【Egret】里使用iframe标签达到内嵌多个web界面

    目的:Egret里使用iframe标签达到内嵌多个web界面,模式相当于主swf调用N个子swf的效果: 目前在做项目过程中,在使用iframe的时候,碰到了一些功能需求,以及解决方法如下: 一..在 ...

  7. 老李推荐:第5章7节《MonkeyRunner源码剖析》Monkey原理分析-启动运行: 循环获取并执行事件 - runMonkeyCycles

    老李推荐:第5章7节<MonkeyRunner源码剖析>Monkey原理分析-启动运行: 循环获取并执行事件 - runMonkeyCycles   poptest是国内唯一一家培养测试开 ...

  8. 手机自动化测试:Appium源码之api(1)

    手机自动化测试:Appium源码之api(1)   AppiumDriver getAppStrings() 默认系统语言对应的Strings.xml文件内的数据. driver.getAppStri ...

  9. CSS3实现一束光划过图片、和文字特效

    在打折图标里面 实现一道白光划过的动画效果 css: <!DOCTYPE html><html><head><meta charset="utf-8 ...

  10. JavaWeb项目实现文件上传动态显示进度

    很久没有更新博客了,这段时间实在的忙的不可开交,项目马上就要上线了,要修补的东西太多了.当我在学习JavaWeb文件上传的时候,我就一直有一个疑问,网站上那些博客的图片是怎么上传的,因为当提交了表单之 ...