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. 【html】【15】特效篇--分页

    下载参考: http://aspx.sc.chinaz.com/query.aspx?keyword=%E5%88%86%E9%A1%B5&classID=&page=1 实例:  h ...

  2. 用 CSS 隐藏页面元素的 5 种方法

    原文链接:用 CSS 隐藏页面元素的 5 种方法,转载请注明来源! 用 CSS 隐藏页面元素有许多种方法.你可以将 opacity 设为 0.将 visibility 设为 hidden.将 disp ...

  3. JavaScript中的apply与call与arguments对象

    (一) call方法 语法:presentObj.call(thisObj,arg1,arg2,arg3...) 参数thisObj :将被用作当前对象presentObj的对象. 当thisObj无 ...

  4. java集合 collection-list-LinkedList 模拟一个堆栈或者队列数据结构。

    /* 使用LinkedList模拟一个堆栈或者队列数据结构. 堆栈:先进后出 如同一个杯子. 队列:先进先出 First in First out FIFO 如同一个水管. */ import jav ...

  5. sgu 105 Div 3

    一个数能整除3当且仅当各位数之和能整除3. 有了这个规律就好办了, 但是呢,仔细一看, n太大了, 都到 2^31 了.所以简单的模拟肯定不行. 这种貌似像数论的题,一时找不到好办法,就打表! 打表出 ...

  6. LAMP虚拟主机配置以及控制目录访问

    3.基于域名的虚拟主机配置 NameVirtualHost192.168.3.32:80#apache2.2.xx版本需要开启此选项,而且要和下面的保持一致:2.4.x版本就不需要此项设置了 < ...

  7. hdu 5512 Pagodas 扩展欧几里得推导+GCD

    题目链接 题意:开始有a,b两点,之后可以按照a-b,a+b的方法生成[1,n]中没有的点,Yuwgna 为先手, Iaka后手.最后不能再生成点的一方输: (1 <= n <= 2000 ...

  8. 服务器环境搭建系列(三)-JDK篇

    1.如果系统已经有jdk,卸载之. rpm -qa|grep java rpm -e --nodeps 上面命令返回的软件包名字 或者 yum -y remove 上面命令返回的包名 下载bin文件, ...

  9. Viz World and Viz Curious Maps 教程 -- 基础篇

    0. 开篇之前的一些废话 本文的内容是之前因为一些原因而写的,现在打算分享出来,内容就不做更改纯迁移了…毕竟我也太久没摸过加密狗了( ╯□╰ ).内容定位是教程,对应的 Curious World M ...

  10. Jquer学习

    1:什么是Jquery 2:jquery的例子 3:后记