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. 记录毕业论文 LanguageTool 二次开发时用到的网站

    LanguageTool Development LanguageTool Supported Languages Share your knowledge about LT - LanguageTo ...

  2. storm如何保证at least once语义?

    背景 前期收到的问题: 1.在Topology中我们可以指定spout.bolt的并行度,在提交Topology时Storm如何将spout.bolt自动发布到每个服务器并且控制服务的CPU.磁盘等资 ...

  3. 云计算之路-阿里云上:2014年6月12日14:40出现CPU 100%

    这是今天遇到的第2次故障,主站1台云服务器在14:40:33-14:41:09期间出现CPU突然跑满的状况,而CPU跑满之前,请求量并没有明显增长. 向阿里云提交之后,说晚上会对这台云服务器的虚拟机进 ...

  4. VC6.0中MFC界面换肤简例

    利用VC中的MFC进行界面设计时,发现界面上的各控件无法简易地进行调整,比如字体大小.颜色.格式等. 为了改变外观,小小地美化一下,今天决定动手一试. 网上提供的库和方法不计其数,我选择了SkinMa ...

  5. 字符串string类型转换成DateTime或DateTime?类型

    常用的Convert.ToDateTime方法 //将含有正确日期格式的string类型转换成DateTime类型 string strDate = "2014-08-01"; D ...

  6. [BZOJ2753][SCOI2012]滑雪与时间胶囊(特殊的有向树形图)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2753 分析: 第一问:直接BFS扩展知道无法扩展 第二问: 看似就是最小树形图啊= = ...

  7. Linq表达式开窍

    static IQueryable<T> GetPageList<T,TKey>(Expression<Func<T,bool>> whereLambd ...

  8. nginx 日志怎么实现显示真实客户端IP

    这篇文章页不错: http://www.tuicool.com/articles/E32mYf 假如说我们现在的架构是,nginx做反向代理,apache做web服务器.那么我们怎么让我的web服务器 ...

  9. 【POJ 1273】Drainage Ditches(网络流)

    一直不明白为什么我的耗时几百毫秒,明明差不多的程序啊,我改来改去还是几百毫秒....一个小时后:明白了,原来把最大值0x3f(77)取0x3f3f3f3f就把时间缩短为16ms了.可是为什么原来那样没 ...

  10. 【CodeForces 621A】Wet Shark and Odd and Even

    题 Today, Wet Shark is given n integers. Using any of these integers no more than once, Wet Shark wan ...