poj 2513 Colored Sticks (trie树+并查集+欧拉路)
| Time Limit: 5000MS | Memory Limit: 128000K | |
| Total Submissions: 40043 | Accepted: 10406 |
Description
Input
Output
Sample Input
blue red
red violet
cyan blue
blue magenta
magenta cyan
Sample Output
Possible
Hint
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<stack>
#include<deque>
typedef long long ll;
const int maxn=; int col;
struct ttrie
{
bool isword;
int id;
ttrie* next[];
ttrie()
{
isword=false;
for(int i=;i<;i++)
next[i]=NULL;
}
}; char str1[],str2[]; int add(ttrie* root,char str[])
{
ttrie *p=root,*q;
for(int i=;str[i]!='\0';i++)
{
int temp=str[i]-'a';
if(p->next[temp]==NULL)
{
q=new ttrie;
p->next[temp]=q;
p=q;
}
else
p=p->next[temp];
}
if(p->isword)
return p->id;
else
{
p->isword=true;
p->id=++col;
return p->id;
}
} int degree[maxn*+];
int cnt;
struct tedge
{
int u,v;
};
tedge edge[maxn+]; int fa[maxn*+]; int getfa(int x)
{
if(fa[x]==x)
return x;
else
return fa[x]=getfa(fa[x]);
} int main()
{
col=;
ttrie* root=new ttrie; memset(degree,,sizeof(degree));
cnt=;
while(scanf("%s%s",str1,str2)!=EOF)
{
int u=add(root,str1);
int v=add(root,str2);
degree[u]++;
degree[v]++;
edge[++cnt]=(tedge){u,v};
} bool flag=true;
for(int i=;i<=col;i++)
fa[i]=i;
int tot=col;
for(int i=;i<=cnt;i++)
{
int fx=getfa(edge[i].u);
int fy=getfa(edge[i].v);
if(fx!=fy)
{
fa[fx]=fy;
tot--;
}
}
if(tot>)
flag=false;
if(flag)
{
int odd=;
for(int i=;i<=col;i++)
{
if(degree[i]%)
odd++;
}
if(odd!=&&odd!=)
flag=false;
}
if(flag)
printf("Possible\n");
else
printf("Impossible\n"); return ;
}
poj 2513 Colored Sticks (trie树+并查集+欧拉路)的更多相关文章
- POJ 2513 Colored Sticks (离散化+并查集+欧拉通路)
下面两个写得很清楚了,就不在赘述. http://blog.sina.com.cn/s/blog_5cd4cccf0100apd1.htmlhttp://www.cnblogs.com/lyy2890 ...
- POJ 2513 Colored Sticks (欧拉回路+并查集+字典树)
题目链接 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with ...
- poj 2513 Colored Sticks trie树+欧拉图+并查集
点击打开链接 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27955 Accepted ...
- poj 2513 Colored Sticks (trie 树)
链接:poj 2513 题意:给定一些木棒.木棒两端都涂上颜色,不同木棒相接的一边必须是 同样的颜色.求能否将木棒首尾相接.连成一条直线. 分析:能够用欧拉路的思想来解,将木棒的每一端都看成一个结点 ...
- poj2513 Colored Sticks —— 字典树 + 并查集 + 欧拉回路
题目链接:http://poj.org/problem?id=2513 题解:通过这题了解了字典树.用字典树存储颜色,并给颜色编上序号.这题为典型的欧拉回路问题:将每种颜色当成一个点.首先通过并查集判 ...
- poj 2513 Colored Sticks( 字典树哈希+ 欧拉回路 + 并查集)
题目:http://poj.org/problem?id=2513 参考博客:http://blog.csdn.net/lyy289065406/article/details/6647445 htt ...
- 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(欧拉路径+并检查集合+特里)
题目链接:poj 2513 Colored Sticks 题目大意:有N个木棍,每根木棍两端被涂上颜色.如今给定每一个木棍两端的颜色.不同木棍之间拼接须要颜色同样的 端才干够.问最后是否能将N个木棍拼 ...
- [欧拉] poj 2513 Colored Sticks
主题链接: http://poj.org/problem? id=2513 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Tota ...
随机推荐
- SpringBoot学习(一)—— idea 快速搭建 Spring boot 框架
简介 优点 Spring Boot 可以以jar包的形式独立运行,运行一个Spring Boot 项目只需要通过 java -jar xx.jar 来运行. Spring Boot 可以选择内嵌Tom ...
- 使用idea来部署git项目
使用idea来部署git项目 一).将项目交由git管理 VCS ---->import into Version Cntorl ------>create Git Repository ...
- 2019-10-8:渗透测试,基础学习,php基础,会话,文件包含,笔记
php面向对象基础->调用符号构造函数construct,主要用来创建对象时初始化对象,为成员变量赋初始值,总与new运算符一起使用在创建对象的语句中 析构函数destructor,与构造函数相 ...
- 有效的减少代码中太多的if、else?-策略模式
写这篇文章的目的和上一篇单例模式一样,策略模式也是一种常用的设计模式,太多的if-else不仅看着不太美观而且不好维护,对于自己来说也等于复习了一遍策略模式.先说一下策略 模式的定义: 策略模式封装了 ...
- ElasticSearch如何一次查询出全部数据—基于Scroll
Elasticsearch 查询结果默认只显示10条,可以通过设置from及size来达到分页的效果(详见附3),但是 from + size <= 10,000,因为index.max_res ...
- python拆包
如果需要把原组A赋值给args 字典B 赋值给kwargs 在调用函数的时候需要给参数 加上星号*
- JavaScript实现返回顶部效果
仿淘宝回到顶部效果 需求:当滚动条到一定位置时侧边栏固定在某个位置,再往下滑动到某一位置时显示回到顶部按钮.点击按钮后页面会动态滑到顶部,速度由快到慢向上滑. 思路: 1.页面加载完毕才能执行js代码 ...
- PHP命令空间namespace及use的用法
使用namespace的目的 命名空间将代码划分出不同的空间(区域),每个空间的常量.函数.类(为了偷懒,我下边都将它们称为元素)的名字互不影响, 这个有点类似我们常常提到的'封装'的概念. 团队合作 ...
- 【并发编程】ThreadLocal的兄弟InheritableThreadLocal
本博客系列是学习并发编程过程中的记录总结.由于文章比较多,写的时间也比较散,所以我整理了个目录贴(传送门),方便查阅. 并发编程系列博客传送门 引子 public class InheritableT ...
- 在华为云ECS上手工通过Docker部署tomcat
本文介绍了如何在华为云上ECS上手工通过Docker部署tomcat,并提供了Docker常用操作 一.环境准备 ECS:操作系统版本: CentOS Linux release 7.6.181 ...