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. c语言,结构体里面的函数

    以linux-3.2内核代码为例,结构体里面的函数的用法: 例,在某驱动文件中,定义了一个平台设备驱动: static struct platform_driver s3c24xx_led_drive ...

  2. int 转换成 CString(VC2008里有这个问题)

    int s = 123; CString str; str.Format("%d",s); 这样就可以了,但是有的会提示这个错误 如果出现这个错误,就改成下面这个就OK了:  st ...

  3. 基于visual Studio2013解决面试题之1503最大公约数最小公倍数

     题目

  4. 阿根廷探戈(Argentine Tango)舞步

    阿根廷探戈(Argentine Tango)舞步 阿根廷探戈(Argentine Tango)舞步 2011-11-22 13:05:11   不像其它大部分的社交舞,阿根廷探戈没有固定的舞步,它是一 ...

  5. if判断 和&&

    function aaa(){   console.log('我是aaa');  };   aaa && aaa();  //如果aaa函数存在 就调用 aaa()    //等价写法 ...

  6. 使用Eclipse EE开发web项目

    最近使用EclipseEE开发web项目,遇到了以下几个问题: 1. 通过tomcat启动web应用的时候,总是提示找不到包或者class. 经过排查,发现所有的jar包并没有放到WEB-INF/li ...

  7. Swift - 添加、修改、删除通讯录联系人

    使用AddressBook.framework框架,我们除了可以很方便的获取通信录里的联系人.同时,还能对通讯录进行新增.修改.删除联系人操作. (注意:这些操作同查询一样,首先需要发起授权请求) 1 ...

  8. mui如何增加自定义字体icon图标

    http://ask.dcloud.net.cn/article/128 字体地址:http://www.iconfont.cn/

  9. ios-王云鹤 调用ios系统功能---------------打电话、发短信、发邮件

    --------------------------------------菜鸟总结,欢迎读者雅正------------------------------------------------- 先 ...

  10. EasyUI - Resizable 调整大小

    效果: html代码: <div id="rr" style="width: 100px; height: 100px; border: 2px solid #cc ...