Colored Sticks
Time Limit: 5000MS   Memory Limit: 128000K
Total Submissions: 28036   Accepted: 7428

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.

Source

The UofA Local 2000.10.14

无向图欧拉通路:连通图+两个点的度数是奇数。。。。。

 #include <iostream>
#include <cstdio>
#include <cstring> using namespace std; const int maxnode=;
int worldnum=,Father[],USize[],degree[]; struct Trie
{
int tot,root,child[maxnode][];
int flag[maxnode];
Trie()
{
memset(child[],,sizeof(child[]));
memset(flag,-,sizeof(flag));
root=tot=;
}
void Insert(const char* str)
{
int *cur=&root;
for(const char* p=str;*p;*p++)
{
cur=&child[*cur][*p-'a'];
if(*cur==)
{
*cur=tot++;
memset(child[tot],,sizeof(child[tot]));
flag[tot]=-;
}
}
flag[*cur]=worldnum++;
} int Query(const char*str)
{
int* cur=&root;
for(const char* p=str;*p&&*cur;p++)
{
cur=&child[*cur][*p-'a'];
}
if(*cur&&~flag[*cur])
{
return flag[*cur];
}
else
{
Insert(str);
return worldnum-;
}
}
}T; int Findfather(int x)
{
if(Father[x]==x)
{
return x;
}
else
{
Father[x]=Father[Father[x]];
return Findfather(Father[x]);
}
} void Disjoin()
{
for(int i=;i<;i++)
{
Father[i]=i; USize[i]=;
}
} void Unionset(int a,int b)
{
int Fa=Findfather(a);
int Fb=Findfather(b);
if(Fa==Fb) return ;
if(USize[Fa]>=USize[Fb])
{
Father[Fa]=Fb;
USize[Fb]+=USize[Fa];
}
else
{
Father[Fb]=Fa;
USize[Fa]+=USize[Fb];
}
} int main()
{
char str1[],str2[];
Disjoin();
while(scanf("%s %s",str1,str2)!=EOF)
{
int a=T.Query(str1);
int b=T.Query(str2);
degree[a]++; degree[b]++;
Unionset(a,b);
}
int sum=;
for(int i=;i<worldnum;i++)
{
if(degree[i]%==) sum++;
}
if(sum>) { puts("Impossible"); return ;}
for(int i=;i<worldnum;i++)
{
if(Findfather()!=Findfather(i))
{ puts("Impossible"); return ;}
}
puts("Possible");
return ;
}

POJ 2513 Colored Sticks的更多相关文章

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

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

  2. [欧拉] poj 2513 Colored Sticks

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

  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. POJ 2513 Colored Sticks (欧拉回路 + 字典树 +并查集)

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

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

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

  7. POJ 2513 - Colored Sticks - [欧拉路][图的连通性][字典树]

    题目链接: http://poj.org/problem?id=2513 http://bailian.openjudge.cn/practice/2513?lang=en_US Time Limit ...

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

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

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

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

随机推荐

  1. 如何区分 OpenStack Neutron Extension 和 Plugin

    Neutron 里面的 extension 和 plugin 是非常相似的两个概念,我花了好久才貌似搞懂了两者的区别,还不一定完全正确. 在OpenStack 的官网wiki中,可以找到它们两个的定义 ...

  2. jquery设置元素的readonly与diabled属性方法

    cppy from : http://www.cnblogs.com/RascallySnake/archive/2010/08/03/1791365.html Jquery的api中提供了对元素应用 ...

  3. DOM系列---进阶篇

    内容提纲: 1.DOM类型 2.DOM扩展 3.DOM操作内容 一.DOM类型 DOM基础篇中,我们了解了DOM的节点并且了解怎样查询和操作节点,而本身这些不同的节点,又有着不同的类型.   DOM类 ...

  4. 改善C#程序的建议3:在C#中选择正确的集合进行编码

    要选择正确的集合,我们首先要了解一些数据结构的知识.所谓数据结构,就是相互之间存在一种或多种特定关系的数据元素的集合.结合下图,我们看一下对集合的分类. 集合分类 在上图中,可以看到,集合总体上分为线 ...

  5. 【Moqui业务逻辑翻译系列】--UBPL Introduction同意的商业处理文库介绍

    h1. UBPL Introduction 通用的商业处理文库介绍h4. Why a Universal Business Process Library? 为什么需要通用的商业处理文库? The g ...

  6. Html-Css-li标签增加图片

    制作一个需要显示颜色的圆点. <style> .item1 ul{list-style:none} .item1 li{padding-left:20px;background:url(圆 ...

  7. Java基础-常量

    常量是一种标识符,它的值在运行期间恒定不变.并且常量在程序中只能被引用,而不能被重新赋值. 以下,我们在Math类中就定义了一个全局常量PI被final关键字修饰的变量名最好要大写. public c ...

  8. BZOJ-1800 飞行棋 数学+乱搞

    这道题感觉就是乱搞,O(n^4)都毫无问题 1800: [Ahoi2009]fly 飞行棋 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1172 So ...

  9. BZOJ2121 字符串游戏

    Description BX正在进行一个字符串游戏,他手上有一个字符串L,以及其 他一些字符串的集合S,然后他可以进行以下操作:对于一个在集合S中的字符串p,如果p在L中出现,BX就可以选择是否将其删 ...

  10. poj 3463 最短路与次短路&&统计个数

    题意:求最短路和比最短路长度多1的次短路的个数 本来想图(有)方(模)便(版)用spfa的,结果妹纸要我看看dijkstra怎么解.... 写了三遍orz Ver1.0:堆优化+邻接表,WA //不能 ...