Colored Sticks
Time Limit: 5000MS   Memory Limit: 128000K
Total Submissions: 27134   Accepted: 7186

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

 
题意: 有很多个火彩 每头都有颜色   相同的颜色可以和另外的火柴相接  问这些火柴能不能连接成一条线 
 
思路:  很明显这是一个欧拉通路问题      一笔画画完整个通路 
用字典树标记字符串对应的id
用map超时
#include<stdio.h>
#include<string>
#include<string.h>
#include<map>
#include<malloc.h>
using namespace std;
#define N 250000+10
char s1[100],s2[100];
int rank[N],fa[N],num[N],co=0;
struct haha
{
int id;
struct haha *next[26];
}*root;
struct haha * creat()
{
int i;
struct haha *p;
p=(struct haha *)malloc(sizeof(struct haha));
p->id=-1;
for(i=0;i<26;i++) p->next[i]=NULL;
return p;
};
int update(char *s)
{
int d,pos,i;
struct haha *p;
p=root;d=strlen(s);
for(i=0;i<d;i++)
{
pos=s[i]-'a';
if(p->next[pos]==NULL)
{
p->next[pos]=creat();
p=p->next[pos];
}
else
{
p=p->next[pos];
}
}
if(p->id==-1)
p->id=co++;
return p->id;
}
int find(int n)
{
return n==fa[n]?n:fa[n]=find(fa[n]);
}
void join(int a,int b)
{
if(rank[a]>rank[b])
{
fa[b]=a;
rank[a]+=rank[b];
}
else
{
fa[a]=b;
rank[b]+=rank[a];
}
}
int main()
{
int i,j,k,cnt=0,a,b,rem;
for(i=0;i<N;i++)
{
rank[i]=1;fa[i]=i;
}
memset(num,0,sizeof(num));
root=creat();
while(scanf("%s %s",s1,s2)!=EOF)
{
a=update(s1);
b=update(s2); num[a]++;num[b]++;
a=find(a);b=find(b);
if(a==b) continue;
join(a,b);
}
cnt=0;
int temp=find(0);
for(i=0;i<N;i++)
{
if(num[i]%2==1) cnt++;
if(cnt>2) {printf("Impossible\n");return 0;}
if(num[i]!=0&&find(i)!=temp) {printf("Impossible\n");return 0;}
}
printf("Possible\n");
return 0;
}

poj 2513 连接火柴 字典树+欧拉通路 好题的更多相关文章

  1. poj2513- Colored Sticks 字典树+欧拉通路判断

    题目链接:http://poj.org/problem?id=2513 思路很容易想到就是判断欧拉通路 预处理时用字典树将每个单词和数字对应即可 刚开始在并查集处理的时候出错了 代码: #includ ...

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

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

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

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

  4. hdu1116有向图判断欧拉通路判断

    Play on Words Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

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

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

  6. POJ 2513 无向欧拉通路+字典树+并查集

    题目大意: 有一堆头尾均有颜色的木条,要让它们拼接在一起,拼接处颜色要保证相同,问是否能够实现 这道题我一开始利用map<string,int>来对颜色进行赋值,好进行后面的并查操作以及欧 ...

  7. POJ2513Colored Sticks(欧拉通路)(字典树)(并查集)

                                                             Colored Sticks Time Limit: 5000MS   Memory ...

  8. POJ 2001 Shortest Prefixes(字典树)

    题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...

  9. POJ 1300 欧拉通路&欧拉回路

    系统的学习一遍图论!从这篇博客开始! 先介绍一些概念. 无向图: G为连通的无向图,称经过G的每条边一次并且仅一次的路径为欧拉通路. 如果欧拉通路是回路(起点和终点相同),则称此回路为欧拉回路. 具有 ...

随机推荐

  1. java layout 表格项增加、删除、修改

    实现的内容为:点击表格某项,再点击删除钮,可实现删除点击表格某项,再点击编辑按钮,可实现内容改变点击添加按钮,可实现向表格中添加内容 总结:总的来说中间遇到了很多困难,但是都一步步的解决了. pack ...

  2. SqlParameter参数化查询

    上篇博客写了关于重构代码用到的SQLHelper类,这个类包括四种函数,根据是否含参和是否有返回值各分两种.在这里写写传参过程用到的SqlParameter. 如果我们使用如下拼接sql字符串的方式进 ...

  3. MyEclipse弹出提示窗体

    MyEclipse弹出提示窗体 1.弹窗例如以下

  4. C++汉字转拼音(转)

    #include<iostream> #include<string> using namespace std; string findLetter(int nCode); s ...

  5. Debian/Ubuntu Linux 下安装LLVM/Clang 编译器

    第一步,首先编辑 /etc/apt/sources.list,增加下面源: (加入源后务必执行apt-get update,假设有错误提示,先执行第二步,然后apt-get update) Debia ...

  6. .Net有许多Office,PDF,Email,HTML的控件

    比如: Aspose.Total for .NET includes the following components: Aspose.Words for .NET 16.3.0 (4/13/2016 ...

  7. 如何关闭IE浏览器在生成原型时候的安全警告

    在上一节中,我们学习了如何生成网页原型的三种方法,当时我们采用的默认浏览器,搜狗浏览器,没有弹出安全警告,一般情况下,如果你的浏览器是IE的话,在每次生成网页原型的时候都会弹出如下安全警告,如图: 暂 ...

  8. TinkPHP+WAMP

    1.下载wamp 我下载的是php5.5版本:根据你自身的需要嘛 http://www.wampserver.com/ 2.下载thinkphp 我下载的版本是3.2 http://www.think ...

  9. 前端javascript框架之BackboneJS学习笔记

    <!DOCTYPE html><html><head><meta charset="utf-8"><script src=&q ...

  10. HDU 3068 最长回文 Manacher算法

    Manacher算法是个解决Palindrome问题的O(n)算法,能够说是个超级算法了,秒杀其它一切Palindrome解决方式,包含复杂的后缀数组. 网上非常多解释,最好的解析文章当然是Leetc ...