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. Android---60---Notification 通知栏的简单使用

    Notification是显示在手机状态栏的通知 通过Notification.Builder类创建Notification对象. Notification.Builder经常用法: setDefau ...

  2. 主题:Java WebService 简单实例

    链接地址:主题:Java WebService 简单实例    http://www.iteye.com/topic/1135747 前言:朋友们开始以下教程前,请先看第五大点的注意事项,以避免不必要 ...

  3. winform利用代码将控件置于顶端底端

    有时,我们可能动态的添加控件,并准备将其置于对顶层或最底层.实现的方法有两个: 一种方法是在WinForm窗体中使用Controls控件集的SetChildIndex方法,该方法将子控件设定为指定的索 ...

  4. 双击GridView查看详情

    效果如下: protected void gvEquData_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowTy ...

  5. 基于visual Studio2013解决面试题之1102合并字符串

     题目

  6. 使用boost中的property_tree实现配置文件

    property_tree是专为配置文件而写,支持xml,ini和json格式文件   ini比较简单,适合简单的配置,通常可能需要保存数组,这时xml是个不错的选择.   使用property_tr ...

  7. hdu4431 Mahjong

    Mahjong Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  8. Android原生APP内分享

    Android原生APP内分享,可实现数据分享以及assets文件夹分享及私有文件分享 项目地址:https://github.com/json-pu/AndroidAppShare.git

  9. android的单元测试

    1.新建android Test project 2. 选择针对测试的项目 3.新建类继承AndroidTestCase即可: package com.howlaa.sms.test; import ...

  10. Java面试题精选(三) JSP/Servlet Java面试逻辑题

    --   JSP/Servlet  Java面试逻辑题   --     很显然,Servlet/JSP的WEB前端动态制作的重要性比HTML/CSS/JS的价值高很多,但我们都知道他们都是建立在HT ...