Colored Sticks
Time Limit: 5000MS   Memory Limit: 128000K
Total Submissions: 40043   Accepted: 10406

Description

You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a straight line such that the colors of the endpoints that touch are of the same color?

Input

Input is a sequence of lines, each line contains two words, separated by spaces, giving the colors of the endpoints of one stick. A word is a sequence of lowercase letters no longer than 10 characters. There is no more than 250000 sticks.

Output

If the sticks can be aligned in the desired way, output a single line saying Possible, otherwise output Impossible.

Sample Input

blue red
red violet
cyan blue
blue magenta
magenta cyan

Sample Output

Possible

Hint

Huge input,scanf is recommended.
 
题目大意:
给定n根木棒,首尾各有一种颜色。求问有没有可能,将这些木棒连成一排,使得接头处颜色相同。
 
判断无向图是否存在欧拉路经典题。
一根木棒其实就是给两种颜色连上一条无向边(可能会有重边的)。那么一条欧拉路就相当于一种连接方式。
无向图存在欧拉图的条件:
1)联通。这个交给并查集就好了。
2)度数为奇数的点只能是0个或2个。这个建图的时候或建完图后都挺好处理的。
主要是如何建图呢。字符串太多了,要hash成数。用字典树比较合适。
每次遇见一种颜色,看字典树中有没有该颜色。如果有,返回该颜色的id,否则加入该颜色,id取全局变量++col。
 
#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树+并查集+欧拉路)的更多相关文章

  1. POJ 2513 Colored Sticks (离散化+并查集+欧拉通路)

    下面两个写得很清楚了,就不在赘述. http://blog.sina.com.cn/s/blog_5cd4cccf0100apd1.htmlhttp://www.cnblogs.com/lyy2890 ...

  2. POJ 2513 Colored Sticks (欧拉回路+并查集+字典树)

    题目链接 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with ...

  3. poj 2513 Colored Sticks trie树+欧拉图+并查集

    点击打开链接 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27955   Accepted ...

  4. poj 2513 Colored Sticks (trie 树)

    链接:poj 2513 题意:给定一些木棒.木棒两端都涂上颜色,不同木棒相接的一边必须是 同样的颜色.求能否将木棒首尾相接.连成一条直线. 分析:能够用欧拉路的思想来解,将木棒的每一端都看成一个结点 ...

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

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

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

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

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

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

  8. poj 2513 Colored Sticks(欧拉路径+并检查集合+特里)

    题目链接:poj 2513 Colored Sticks 题目大意:有N个木棍,每根木棍两端被涂上颜色.如今给定每一个木棍两端的颜色.不同木棍之间拼接须要颜色同样的 端才干够.问最后是否能将N个木棍拼 ...

  9. [欧拉] poj 2513 Colored Sticks

    主题链接: http://poj.org/problem? id=2513 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Tota ...

随机推荐

  1. Java内存模型与volatile关键字

    Java内存模型与volatile关键字 一).并发程序开发 并行程序的开发要涉及多线程.多任务间的协作和数据共享问题. 常用的并发控制:内部锁.重入锁.读写锁.信号量. 二).线程的特点 线程的特点 ...

  2. python+appium搭建的测试环境

    : 1,安装jdk JDK下载好jdk直接点下一步就可以了,然后开始配置变量classpath, path, Java_home:再运行cmd,并输入Java和javac看输出判断环境变量是否配好了. ...

  3. PHP中16个高危函数

    php中内置了许许多多的函数,在它们的帮助下可以使我们更加快速的进行开发和维护,但是这个函数中依然有许多的函数伴有高风险的,比如说一下的16个函数不到万不得已不尽量不要使用,因为许多“高手”可以通过这 ...

  4. python CGI编程---Apache服务安装(2)

    一.下载Apache 下载地址:https://www.apachehaus.com/cgi-bin/download.plx 我这里下载第一个,我电脑是window的64位. 下载完成后,解压到 我 ...

  5. javaScript——label语句

    第一次看见label语句是这样一个场景: function foo() {x: 1} 当时十分疑惑,为什么不报错呢?对象可以这样写? 后来知道这个是label语句,一般配合break和continue ...

  6. leetcode105 从前序与中序遍历序列构造二叉树

    如何遍历一棵树 有两种通用的遍历树的策略: 宽度优先搜索(BFS) 我们按照高度顺序一层一层的访问整棵树,高层次的节点将会比低层次的节点先被访问到. 深度优先搜索(DFS) 在这个策略中,我们采用深度 ...

  7. python数据分析三个重要方法之:numpy和pandas

    关于数据分析的组件之一:numpy ndarray的属性     4个必记参数:ndim:维度shape:形状(各维度的长度)size:总长度dtype:元素类型   一:np.array()产生n维 ...

  8. Android Binder机制介绍

    做过Android开发的同学可能有些体会,入门初期,工作内容主要是实现各式各样的UI界面,以及实现应用的业务逻辑.在这个阶段,我们会逐渐熟悉View系统,逐渐学会实现各种各样的界面以及动画效果.再往后 ...

  9. X86架构CPU常识(主频,外频,FSB,cpu位和字长,倍频系数,缓存,CPU扩展指令集,CPU内核和I/O工作电压,制造工艺,指令集,超流水线与超标量)

    1.主频 主频也叫时钟频率,单位是MHz,用来表示CPU的运算速度. CPU的主频=外频×倍频系数.很多人认为主频就决定着CPU的运行速度,这不仅是个片面的,而且对于服务器来讲,这个认识也出现了偏差. ...

  10. MySql计算字段的长度

    用户账号有用中文字符,查找所有含有中文字符的账号 SELECT member_name FROM table_member WHERE length(member_name)!=char_length ...