Time Limit: 5000MS   Memory Limit: 128000K
Total Submissions: 27704   Accepted: 7336

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.
 
题意:给定若干个棒,棒的两端涂上不同的颜色,问是否能将棒首尾相连且不同棒相接的一端颜色相同;
 
思路:题意容易理解,但开始完全没思路,经大神指点后,可以用欧拉路的思想;可以把涂颜色的棒的两端看成结点,
         把木棒看成边,相同颜色的就是一个结点,要将木棒连成一个直线,也就是“一笔画”问题;
         无向图存在欧拉路的充要条件是:
         >图是连通的(可以用并查集判断,开始将每个点初始化一棵树,经过输入将有相同祖先的结点合并到一个集合中,
           最后任意枚举一个节点,若他们有共同的祖先,说明图是连通的);
         >度数为奇数的结点有0个或两个;
 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
int degree[],set[],id = ; struct node
{
int flag;
int id;
struct node* next[];
};
struct node* root; //开辟新结点
struct node* creat()
{
struct node *p = (struct node*)malloc(sizeof(struct node));
p->flag = ;
for(int i = ; i < ; i++)
p->next[i] = NULL;
return p;
} int find(int x)
{
if(set[x] != x)
set[x] = find(set[x]);//路径压缩;
return set[x];
} //字典树哈希
int Hash(char s[])
{
struct node *p = root;
for(int i = ; s[i]; i++)
{
if(p->next[s[i]-'a'] == NULL)
p->next[s[i]-'a'] = creat();
p = p->next[s[i]-'a'];
}
if(p->flag != )
{
p->flag = ;
p->id = id++;
}
return p->id;
} int check()
{
int sum = ;
int x = find();
for(int i = ; i < id; i++)
if(find(i) != x)//没有共同祖先,图是不连通的,直接返回;
return ;
for(int i = ; i < id; i++)
{
if(degree[i]%)
sum++;
}
if(sum == || sum == )
return ;//图是连通的并且奇度数是0或2,说明有欧拉路;
return ;//图是连通的但奇度数不是0或2也不存在欧拉路;
} int main()
{
memset(degree,,sizeof(degree));
for(int i = ; i <= ; i++)
set[i] = i;//所有节点初始化为一棵树
char s1[],s2[];
int u,v;
root = creat();
while(scanf("%s %s",s1,s2) != EOF)
{
u = Hash(s1);
v = Hash(s2);
degree[u]++;
degree[v]++;
int x = find(u);
int y = find(v);
if(x != y)
set[x] = y;
}
if(check()) printf("Possible\n");
else printf("Impossible\n");
return ;
}

Colored Sticks (字典树哈希+并查集+欧拉路)的更多相关文章

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

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

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

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

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

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

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

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

  5. poj2513--并查集+欧拉路+字典树

    经典好题,自己不知道哪里错了交上去是RE,可能是数组开的不好吧,字典树老碰到这种问题.. 先马上别人的代码,有空对拍看看 #include <cstdio> #include <cs ...

  6. poj2513字典树+欧拉图判断+并查集断连通

    题意:俩头带有颜色的木棒,要求按颜色同的首尾相连,可能否? 思路:棒子本身是一条边,以俩端为顶点(同颜色共点),即求是否有无向图欧拉路(每条棒子只有一根, 边只能用一次,用一次边即选一次棒子). 先判 ...

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

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

  8. Colored Sticks POJ - 2513 并查集+欧拉通路+字典树hash

    题意:给出很多很多很多很多个棒子 左右各有颜色(给出的是单词) 相同颜色的可以接在一起,问是否存在一种 方法可以使得所以棒子连在一起 思路:就是一个判欧拉通路的题目,欧拉通路存在:没奇度顶点   或者 ...

  9. [BZOJ3038]上帝造题的七分钟2 树状数组+并查集

    考试的时候用了两个树状数组去优化,暴力修改,树状数组维护修改后区间差值还有最终求和,最后骗了40分.. 这道题有好多种做法,求和好说,最主要的是开方.这道题过的关键就是掌握一点:在数据范围内,最多开方 ...

随机推荐

  1. Generate GUID using vbscript

    在 .msi 中 的 Component table,查看 ComponentId 列,是一个16进制数的字符串, 用 InstallShield IDE 添加一个 component ,Compon ...

  2. Android-ViewPagerIndicator-master 、Android-PullToRefresh 学习篇

    最近在学习android,然后看到了很多有用的开源库.其中一个就是Android-ViewPagerIndicator :. 这是与ViewPager兼容的一个分页指示器库.分页指示器(Friends ...

  3. C++中const修饰基本数据类型、指针、引用、对象

    const修饰基本数据类型 #include <iostream> using namespace std; void main(){ const int a = 1; const cha ...

  4. jQuery手风琴广告展示插件

    效果说明:当鼠标移动到已折叠广告的标题后,折叠当前已展开的广告,并同步展开相应的折叠广告.这种Accordion效果,看似简单,但因为存在动画同步的问题,不能简单地用两个animate()来实现.必须 ...

  5. 使用jQuery.FileUpload和Backload自定义控制器上传多个文件

    当需要在控制器中处理除了文件的其他表单字段,执行控制器独有的业务逻辑......等等,这时候我们可以自定义控制器. 通过继承BackloadController □ 思路 BackloadContro ...

  6. 关于aspx模板页面元素路径的问题,以及对模板页面的理解

    模板页面仅是模板,它不是单独存在的页面,它的路径就是引用它的内容页面的路径. 换句话说,模板页面,只是内容页面上固定的部分.     模板页面引用了的js和CSS,内容页面就不用重新引用了   css ...

  7. CentOS添加RPMforge软件源

    1.查看CentOS版本 cat /etc/redhat-release 2.查看系统位数 uname -i 3.下载rpm包 wget "rpm地址" CentOS 6: i68 ...

  8. OSG-3.4.0 简要说明(Readme)

    欢迎来到OpenSceneGraph(OSG)世界. Welcome to the OpenSceneGraph (OSG). 对于项目最新信息, 以及如何编译和运行库和示例的更多细节, 可以查看OS ...

  9. hdu 2222 Keywords Search ac自动机入门

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...

  10. HttpContext

    HttpContext 类:封装有关个别 HTTP 请求的所有 HTTP 特定的信息.也有人叫上下文信息. 1.生存周期:从客户端用户点击并产生了一个向服务器发送请求开始---服务器处理完请求并生成返 ...