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
题意:给你n(n<=250000)根小木棒,木棒两端染有一些颜色,两根木棒相连需要保证相连端点颜色一致.求这些木棒是否可以全部相连?
题解:可以将木棒理解为边,连接两种颜色,那么全部相连就变成了一笔画的问题,即判断欧拉回路.
欧拉回路需要满足两个条件:
1.度数为奇数的点为0个或2个
2.图联通
现在的问题是如何把字符串变成数字的点,用map是肯定会TLE的
hash应该可以,但tire好写,所以我选择了trie树,存入trie的值为该字符串的映射
图联通用并查集去判断.
代码如下:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; int cnt=,du[],fa[]; struct Trie
{
int sz,tr[][];
int time[]; void clear()
{
sz=;
memset(tr,,sizeof(tr));
} void insert(char* c,int x)
{
int rt=,len=strlen(c);
for(int i=;i<len;i++)
{
if(!tr[rt][c[i]-'a'])
{
tr[rt][c[i]-'a']=++sz;
}
rt=tr[rt][c[i]-'a'];
}
time[rt]=x;
} int search_t(char *c)
{
int rt=,len=strlen(c);
for(int i=;i<len;i++)
{
if(!tr[rt][c[i]-'a'])
{
return ;
}
rt=tr[rt][c[i]-'a'];
}
return time[rt];
} }trie; void mem(int n)
{
for(int i=;i<=n;i++)
{
fa[i]=i;
}
} int find(int x)
{
if(x!=fa[x])
{
fa[x]=find(fa[x]);
}
return fa[x];
} void union_(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
{
fa[fx]=fy;
}
} int euler()
{
int sum=,t=find();
for(int i=;i<=cnt;i++)
{
if(du[i]%==)
{
sum++;
}
}
if(sum!=&&sum!=)
{
return ;
}
for(int i=;i<=cnt;i++)
{
if(find(i)!=find(t))
{
return ;
}
}
return ;
} int main()
{
char s1[],s2[];
mem();
// freopen("1.in","r",stdin);
// freopen("1.out","w",stdout);
while(scanf("%s %s\n",s1,s2)!=EOF)
{
int from,to;
if(!trie.search_t(s1))
{
trie.insert(s1,++cnt);
}
from=trie.search_t(s1);
du[from]++;
if(!trie.search_t(s2))
{
trie.insert(s2,++cnt);
}
to=trie.search_t(s2);
du[to]++;
union_(from,to);
}
if(euler())
{
puts("Possible");
}
else
{
puts("Impossible");
}
}
 

POJ2513 Colored Sticks(Trie+欧拉回路)的更多相关文章

  1. POJ2513——Colored Sticks(Trie树+欧拉回路+并查集)

    Colored Sticks DescriptionYou are given a bunch of wooden sticks. Each endpoint of each stick is col ...

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

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

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

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27097   Accepted: 7175 ...

  4. poj 2513 Colored Sticks (trie树+并查集+欧拉路)

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

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

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

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

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

  7. POJ2513:Colored Sticks(字典树+欧拉路径+并查集)

    http://poj.org/problem?id=2513 Description You are given a bunch of wooden sticks. Each endpoint of ...

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

    题意:给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的.   无向图存在欧拉路的充要条件为: ①     图是连通的: ②     所有节 ...

  9. poj 2513 Colored Sticks (trie 树)

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

随机推荐

  1. 删除SVN被锁定文件

    svn的working copylocked这种情况大多是因为上次svn命令执行失败且被锁定了. 如果cleanup没有效果的话只好手动删除锁定文件. cd 到svn项目目录下,然后执行命令:del ...

  2. Linux 中断下半部

    为什么使用中断下半部? 中断执行的原则是要以最快的速度执行完,而且期间不能延时和休眠! 可是现实中,中断中可能没办法很快的处理完需要做的事,或者必须用到延时和休眠,因此引入了中断下半部. 中断中处理紧 ...

  3. PHP echo汉字出现乱码的情况之一

    当然首先要理清楚到底是web服务端造成,php造成的,还是数据库造成的. 需要在头文件里UTF-8 <head> <meta http-equiv="Content-Typ ...

  4. Win7旗舰版一直显示检查更新的问题

    最近部门机器从新安装win7 64位 旗舰版之后,每次检查更新,都会一直卡在检查更新的界面过不去,上网搜了一下,看到了网友提供的解决办法, 测试了一下,果然可以.记录下来,以备后用: 到微软官网去下载 ...

  5. C++的三大特性?C也可以做到

    C++的三大特性是什么?封装.继承与多态,那么今天这篇文章小编就来介绍一下,如何用C语言实现C++的这三个特性. 1.封装 何为封装? 在面向对象的思想中,将数据和对数据的操作封装在一起——即类. 类 ...

  6. GitFlow在命令行的使用

    gitflow安装 在命令行直接使用yum安装 yum install gitflow 如果本地的yum源中不存在gitflow,可以尝试添加EPEL源 CentOS6.5: # 下载 wget ht ...

  7. 【转】Jmeter之GUI运行原理

    一.一语道破jmeter 大家都知道我们在应用jmeter的图形化界面来进行操作,保存后生成的是一个.jmx文件. 那么这个.jmx文件中都是些什么呢. <?xml version=" ...

  8. java代码实现从键盘输入编号,输出价格,并且不再编号内的,无效输入!!!!

    总结:请给我更好的建议 package com.badu; import java.util.Scanner; //从键盘输入次数,通过输入的编号,输出对应的的商品价格: public class t ...

  9. 1046 Shortest Distance

    题意:给出一个环和结点之间的距离,求任意两结点之间的最近距离.如图: 思路:令数组dis[i]表示1号结点逆时针至i号结点的距离,初始化dis[1]=0,其他值在输入是确定,即 dis[i] 0 1 ...

  10. PHP中的精确计算bcadd,bcsub,bcmul,bcdiv

    引言:一定要确保数据的准确性.这是一个好的程序员的基本素养. <?php /** * 精确加法 * @param [type] $a [description] * @param [type] ...