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. unity3d 扩展NGUI —— 限制UI点击响应间隔

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

  2. Java学习笔记(十五)——javadoc学习笔记和可能的注意细节

    [前面的话] 这次开发项目使用jenkins做持续集成,PMD检查代码,Junit做单元测试,还会自动发邮件通知编译情况,会将javadoc生成的文档自动发到一个专门的服务器上面,每个人都可以看,所以 ...

  3. servlet请求转发、包含以及重定向

    请求转发: 方式一: ServletContext对象.getRequestDispatcher(目标资源的URI).forward(request,response); 目标资源的URI " ...

  4. nhibernate入门使用经验

    最近项目中用到nhibernate,学到了一点.就在这里写一点. nhibernate是java世界的hibernate的.net版本,其工作原理和hibernate一样的.我们需要用hbm的文件来描 ...

  5. CNN 手写数字识别

    1. 知识点准备 在了解 CNN 网络神经之前有两个概念要理解,第一是二维图像上卷积的概念,第二是 pooling 的概念. a. 卷积 关于卷积的概念和细节可以参考这里,卷积运算有两个非常重要特性, ...

  6. ListView 和 Adapter用法

    一个ListView通常有两个职责. (1)将数据填充到布局. (2)处理用户的选择点击等操作. 第一点很好理解,ListView就是实现这个功能的.第二点也不难做到,在后面的学习中读者会发现,这非常 ...

  7. 关于js字符串替换的一道笔试题目

    题目描述 请写出一个字符串转换函数,接受两个参数: 1.字符串 形如{a}ab-{b}cde{c}fff{d}{}: 2.对象,形如{'a':'1','b':'2','d':'4'} 根据,对象的属性 ...

  8. git的牛逼

    http://rogerdudler.github.io/git-guide/index.zh.html

  9. DVR分布式路由

    1. 背景 没有使用DVR的场景: 从图中可以明显看到东西向和南北向的流量会集中到网络节点,这会使网络节点成为瓶颈. 如果启用DVR,如下图: 对于东西向的流量, 流量会直接在计算节点之间传递. 对于 ...

  10. 【CodeForces 605A】BUPT 2015 newbie practice #2 div2-E - Sorting Railway Cars

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/E Description An infinitely lon ...