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. python中的模块

    用python其实好久了,但是一直没有特别系统的学习过,当年迅速上手,也写了好多代码了,零零散散也学了很多知识点.到了把它们串起来的时候了.尝试记录一下系统整理的知识点,先从“模块”说起. 为什么需要 ...

  2. Ajax基础之封装3

    今天接着我们上篇博文的栗子,现在我来扩大一下需求,之前是点击按钮取出新闻,现在要实现每隔一段事件进行新闻的更新.这个时候,肯定是加一个定时器,然后每隔一段事件,再进行一次Ajax请求,既然要经常用到A ...

  3. java数据类型总结

    java8大基本数据类型: 整型: byte 字节型     1字节   数据范围:-27   ~  27- 1  即:-128 ~ 127 short 短整型    2字节 数据范围:-215 ~ ...

  4. unity3d 扩展NGUI —— 限制UI点击响应间隔

    当某个按钮按下后给服务器发送某条消息 如果玩家短时间内疯狂点击按钮很多次,这将会给服务器发送很多条无用数据 不但增加了服务器的压力,发送数据还浪费流量,甚至可能引发一些莫名其妙的bug 所以,限制UI ...

  5. jsPlumb插件做一个模仿viso的可拖拉流程图

    前言 这是我第一次写博客,心情还是有点小小的激动!这次主要分享的是用jsPlumb,做一个可以给用户自定义拖拉的流程图,并且可以序列化保存在服务器端. 我在这次的实现上面做得比较粗糙,还有分享我在做j ...

  6. js中的运动

    缓慢隐藏与出现 效果: 鼠标移至分享上黄色区域自动向左隐藏. <!DOCTYPE html> <html> <head> <title></tit ...

  7. Andriod开发环境的发展演变

    安卓早先只能使用JAVA开发应用程序,现在支持多种编程语言,方便了许多程序员,但主流的应该还是JAVA语言.早先安卓开发经常用到eclipse,但自从google 发布了Android studio后 ...

  8. org.hibernate.PropertyValueException: not-null property references a null or transient value:

    org.hibernate.PropertyValueException: not-null property references a null or transient value: com.bj ...

  9. Java基础-四要素之一《封装》

    封装从字面上来理解就是包装的意思,专业点就是信息隐藏,是指利用抽象数据类型将数据和基于数据的操作封装在一起,使其构成一个不可分割的独立实体,数据被保护在抽象数据类型的内部,尽可能地隐藏内部的细节,只保 ...

  10. Oracle写函数读写日志实例

    1.用DBA登录赋权限create or replace directory D_OUTPUT as 'D:\TEMP'; grant read,write on directory D_OUTPUT ...