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. C#基础总复习02

    继续更新第二篇: 1:一元运算符:++ -- ++:不管是前加加还是后加加,变量的值最终都会自身加一. 前加加和后加加的区别体现在参与运算的时候,如果是后加加,则首先拿原值参与运算, 运算完成后再自身 ...

  2. Eclipse查看历史代码

    选中要查看的文件(.class等) 右击->Team->Show Local History

  3. 嵌入式系统关机/Embeded System PowerOff HowTo?

    REFER: 嵌入式Linux实现关机命令 REFER: Embedded File System and power-off REFER: kernel/reboot.c REFER: PowerO ...

  4. Colored Linux Man pages

    Colored Linux Man pages 一.什么是Linux Man 参考: 二.如何高效率地使用Man 三.给Linux Man命令添加点颜色. 1.Unix / Linux: Displa ...

  5. 九度OJ 1501 最大连续子序列乘积 -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1501 题目描述: 给定一个浮点数序列(可能有正数.0和负数),求出一个最大的连续子序列乘积. 输入: 输入可能包含 ...

  6. Poj 2159 / OpenJudge 2159 Ancient Cipher

    1.链接地址: http://poj.org/problem?id=2159 http://bailian.openjudge.cn/practice/2159 2.题目: Ancient Ciphe ...

  7. 云盾正常扫描云服务器的IP是什么

    问题:云盾正常扫描云服务器的IP是什么?   解答:云盾扫描云服务器的的IP段固定为    42.120.145.0/24 110.75.105.0/24 110.75.185.0/24 110.75 ...

  8. OpenCV2学习笔记04:图像的读取与显示

    1. 图像读取:imread() Mat imread( ) 参数介绍: filename: 待加载的文件名称. flags: 此标志用来指定被加载图像的颜色类型(color type).这个标志的取 ...

  9. js实现克隆一个对象

    var app={}; app.cloneobj= function(obj){ var o; if(typeof obj == "object"){ if(obj===null) ...

  10. Javascript代码摘录

    判断浏览器窗口高度 if (document.documentElement.clientHeight <800) { var elm = document.getElementById('Di ...