poj-2513(字典树+欧拉通路)
题意:给你n个火柴棍,每个火柴棍头和尾两种颜色,问你是否存在能够把这些火柴棍摆成一行的情况,两个相连的火柴棍的颜色需要一样;
解题思路:最初的思路是用map标记颜色,然后把每种颜色看作点,每根火柴棍看作边,求欧拉路径,然后超时了。。。看了别人的写法,想起来了自己还学过字典树来着。。。然后用字典树找就行了,快很多
代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=500050;
struct tnode
{
int pos;
bool exsit;
tnode *next[30];
tnode()
{
for(int i=0;i<26;i++)
next[i]=NULL;
pos=0;exsit=false;
}
};
tnode *root;
int x,y;
int flag;
int dfn;
int degree[maxn];
int f[maxn];
char s[15],t[15];
tnode* newnode()
{
tnode *p=new tnode;
for(int i=0;i<26;i++)
p->next[i]=NULL;
p->pos=0;p->exsit=false;
return p;
}
void build_trie(char *s)
{
tnode *p=root;
int slen=strlen(s);
for(int i=0;i<slen;i++)
{
int id=s[i]-'a';
if(p->next[id]==NULL)
{
p->next[id]=newnode();
}
p=p->next[id];
}
p->exsit=true;
p->pos=++dfn;
}
int query(char *s)
{
tnode *p=root;
int slen=strlen(s);
for(int i=0;i<slen;i++)
{
int id=s[i]-'a';
p=p->next[id];
if(p==NULL)
return 0;
}
if(p->exsit)
return p->pos;
}
int build(char *str)
{
int zz=query(str);
if(zz!=0)
return zz;
else
{
build_trie(str);
return dfn;
}
}
int findf(int u)
{
if(u==f[u])
return u;
else
{
f[u]=findf(f[u]);
return f[u];
}
}
void join(int x,int y)
{
int t1=findf(x);
int t2=findf(y);
if(t1!=t2)
f[t2]=t1;
}
int main()
{
root=newnode();
for(int i=1;i<=maxn;i++)
f[i]=i;
while(scanf("%s %s",s,t)!=EOF)
{
int x=build(s);
int y=build(t);
degree[x]++;degree[y]++;
join(x,y);
}
// cout<<dfn<<endl;
int cnt=0;
int fa=findf(1);
for(int i=2;i<=dfn;i++)
if(findf(i)!=fa)
flag=1;
if(flag)
{
printf("Impossible\n");return 0;
}
for(int i=1;i<=dfn;i++)
{
if(degree[i]!=0)
{
if((degree[i]&1)==1)
cnt++;
}
}
// cout<<cnt<<endl;
if(cnt==1||cnt>=3)
flag=1;
if(flag)
{
printf("Impossible\n");
}
else
printf("Possible\n");
}
poj-2513(字典树+欧拉通路)的更多相关文章
- poj 2513 连接火柴 字典树+欧拉通路 好题
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27134 Accepted: 7186 ...
- POJ 2513 无向欧拉通路+字典树+并查集
题目大意: 有一堆头尾均有颜色的木条,要让它们拼接在一起,拼接处颜色要保证相同,问是否能够实现 这道题我一开始利用map<string,int>来对颜色进行赋值,好进行后面的并查操作以及欧 ...
- poj2513- Colored Sticks 字典树+欧拉通路判断
题目链接:http://poj.org/problem?id=2513 思路很容易想到就是判断欧拉通路 预处理时用字典树将每个单词和数字对应即可 刚开始在并查集处理的时候出错了 代码: #includ ...
- POJ 2513 字典树+并查集+欧拉路径
Description: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 解题思路: 可以用图论中欧拉路的知识来解这道题,首先可以把木 ...
- Colored Sticks POJ - 2513 并查集+欧拉通路+字典树hash
题意:给出很多很多很多很多个棒子 左右各有颜色(给出的是单词) 相同颜色的可以接在一起,问是否存在一种 方法可以使得所以棒子连在一起 思路:就是一个判欧拉通路的题目,欧拉通路存在:没奇度顶点 或者 ...
- POJ2513Colored Sticks(欧拉通路)(字典树)(并查集)
Colored Sticks Time Limit: 5000MS Memory ...
- POJ 1300 欧拉通路&欧拉回路
系统的学习一遍图论!从这篇博客开始! 先介绍一些概念. 无向图: G为连通的无向图,称经过G的每条边一次并且仅一次的路径为欧拉通路. 如果欧拉通路是回路(起点和终点相同),则称此回路为欧拉回路. 具有 ...
- POJ 1386 Play on Words(有向欧拉通路 连通图)
题意 见下方中文翻译 每一个单词能够看成首尾两个字母相连的一条边 然后就是输入m条边 推断是否能构成有向欧拉通路了 有向图存在欧拉通路的充要条件: 1. 有向图的基图连通: 2. 全部点的出度和 ...
- ACM/ICPC 之 DFS求解欧拉通路路径(POJ2337)
判断是欧拉通路后,DFS简单剪枝求解字典序最小的欧拉通路路径 //Time:16Ms Memory:228K #include<iostream> #include<cstring& ...
- hdu1116有向图判断欧拉通路判断
Play on Words Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
随机推荐
- face recognition[MobileFaceNet]
本文来自<MobileFaceNets: Efficient CNNs for Accurate Real-Time Face Verification on Mobile Devices> ...
- zookeepeer使用java api
一.引入依赖 <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper --> <dependen ...
- Jenkins - Extended E-mail配置教程
前言: 在Jenkins的使用中邮件提醒是一个常用功能,Extended E-mail Notification是一个功能更为齐全,使用也更为复杂的插件,本文即将为大家详细讲解如何配置相关内容,感兴趣 ...
- Linux常用软件启动、停止、重启命令
一.PHP 启动命令: /usr/local/php5/sbin/php-fpm 停止命令: pkill php-fpm 二.MySQL 启动命令: /etc/init.d/mysqld start ...
- Python迭代器与格式化
三元运算 对if-else判断的简写 >>> age = 18 >>> res = "You are so young!" if age < ...
- Day8 Python基础之遗漏知识点(六)
1. 遗漏知识点 深.浅拷贝: http://www.cnblogs.com/yuanchenqi/articles/5782764.html a=b: 浅拷贝: 深拷贝 集合(set) 集合的定 ...
- [2017BUAA软工助教]案例分析小结
BUAA案例分析小结 一.作业要求 http://www.cnblogs.com/jiel/p/7631784.html 二.统计数据 总人数 神策数据 博客园博客 必应词典 30 1 12 17 三 ...
- VO和DO转换(三) Dozer
VO和DO转换(一) 工具汇总 VO和DO转换(二) BeanUtils VO和DO转换(三) Dozer VO和DO转换(四) MapStruct 可参考的资料: dozer官网 Dozer(Jav ...
- UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-25: ordinal not in range(128)
python报错:UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-25: ordinal not in ...
- [转帖] BMC安全隐患
BMC再现漏洞,裸金属云服务器岌岌可危 https://zhuanlan.kanxue.com/article-8006.htm 之前有vt-x 可能有隐患 现在看起来BMC 也就是IPMI 也有隐患 ...