poj2513- Colored Sticks 字典树+欧拉通路判断
题目链接: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 字典树+欧拉通路判断的更多相关文章
- poj 2513 连接火柴 字典树+欧拉通路 好题
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27134 Accepted: 7186 ...
- POJ2513:Colored Sticks(字典树+欧拉路径+并查集)
http://poj.org/problem?id=2513 Description You are given a bunch of wooden sticks. Each endpoint of ...
- poj2513 Colored Sticks —— 字典树 + 并查集 + 欧拉回路
题目链接:http://poj.org/problem?id=2513 题解:通过这题了解了字典树.用字典树存储颜色,并给颜色编上序号.这题为典型的欧拉回路问题:将每种颜色当成一个点.首先通过并查集判 ...
- hdu1116有向图判断欧拉通路判断
Play on Words Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- 欧拉回路&欧拉通路判断
欧拉回路:图G,若存在一条路,经过G中每条边有且仅有一次,称这条路为欧拉路,如果存在一条回路经过G每条边有且仅有一次, 称这条回路为欧拉回路.具有欧拉回路的图成为欧拉图. 判断欧拉通路是否存在的方法 ...
- Colored Sticks (字典树哈希+并查集+欧拉路)
Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27704 Accepted: 7336 Description You ...
- POJ2513——Colored Sticks(Trie树+欧拉回路+并查集)
Colored Sticks DescriptionYou are given a bunch of wooden sticks. Each endpoint of each stick is col ...
- POJ 2513 Colored Sticks 字典树、并查集、欧拉通路
Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some ...
- poj 2513 Colored Sticks( 字典树哈希+ 欧拉回路 + 并查集)
题目:http://poj.org/problem?id=2513 参考博客:http://blog.csdn.net/lyy289065406/article/details/6647445 htt ...
随机推荐
- MyBatis框架原理(三)
一.框架结构原理图 二.MyBatis框架执行流程 三.总结 原理详解: MyBatis应用程序根据XML配置文件创建SqlSessionFactory,SqlSessionFactory在根据配置, ...
- 走入PHP-declare、ticks、encoding、include
declare 结构用来设定一段代码的执行指令.declare 的语法和其它流程控制结构相似(该代码为语法格式,不是代码案例,无需敲打该代码): declare (directive) stateme ...
- Sql(in与exists)
1.如果查询的两表大小相当,那么用in和exists差别不大,如果两个表中一个较小一个较大,则子查询表小的用exists,子查询表大的用in. 2.not in逻辑上不完全等同于not exists, ...
- 在腾讯云上部署Hexo博客
推荐理由 ----搭建个人的空间博客目前深受个人开发者的追捧,然而博客的种类和平台有很多,Hexo是一个开源的静态博客生成器.相比于其他博客而言它只要是web容器就能用.除了闷头专研技术之外,程序员还 ...
- python 调取 shell 命令的几种方法
os.system()无法获得到输出和返回值 os.popen()output = os.popen('cat /proc/cpuinfo')print output.read()返回的是 file ...
- 微信小程序-实战巩固(二)
刚刚写了小程序入门没几天,小程序就开放个人开发者资格,感觉为我而来啊 \(≧▽≦)/.迫不及待的去注册,准备将之前的处女作传上去体验一把,结果卡在了服务器配置上:免费的果然不靠谱/(ㄒoㄒ)/~~,后 ...
- SSH整合(一)hibernate+spring
1.导入maven节点 <dependencies> //测试用的 <dependency> <groupId>junit</groupId> < ...
- Android多渠道打包
项目需要,简单2步实现(由于简单,所以对大量渠道打包不是最优的),比如1000个渠道同时打包的话可能花费的时间会过长.不过目前该方法能满足绝大需求了...根据截图一步一步走: 第一步 设置配置清单文件 ...
- 从零开始用 Flask 搭建一个网站(一)
前言 笔者之前未接触过 Python,只是略懂一点前端,所以说从零开始也相差无几吧.Flask 是一个轻量级的基于 Python 的框架,但是扩展性非常良好(Github 上 22000 多个 sta ...
- stick footer布局
需求: 将footer固定到底部.文章内容不足满屏时 footer在底部,超过满屏时footer在内容末尾. 方法一: <div id="wrap"> <div ...