Colored Sticks
Time Limit: 5000MS   Memory Limit: 128000K
Total Submissions: 40043   Accepted: 10406

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.
 
题目大意:
给定n根木棒,首尾各有一种颜色。求问有没有可能,将这些木棒连成一排,使得接头处颜色相同。
 
判断无向图是否存在欧拉路经典题。
一根木棒其实就是给两种颜色连上一条无向边(可能会有重边的)。那么一条欧拉路就相当于一种连接方式。
无向图存在欧拉图的条件:
1)联通。这个交给并查集就好了。
2)度数为奇数的点只能是0个或2个。这个建图的时候或建完图后都挺好处理的。
主要是如何建图呢。字符串太多了,要hash成数。用字典树比较合适。
每次遇见一种颜色,看字典树中有没有该颜色。如果有,返回该颜色的id,否则加入该颜色,id取全局变量++col。
 
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<stack>
#include<deque>
typedef long long ll;
const int maxn=; int col;
struct ttrie
{
bool isword;
int id;
ttrie* next[];
ttrie()
{
isword=false;
for(int i=;i<;i++)
next[i]=NULL;
}
}; char str1[],str2[]; int add(ttrie* root,char str[])
{
ttrie *p=root,*q;
for(int i=;str[i]!='\0';i++)
{
int temp=str[i]-'a';
if(p->next[temp]==NULL)
{
q=new ttrie;
p->next[temp]=q;
p=q;
}
else
p=p->next[temp];
}
if(p->isword)
return p->id;
else
{
p->isword=true;
p->id=++col;
return p->id;
}
} int degree[maxn*+];
int cnt;
struct tedge
{
int u,v;
};
tedge edge[maxn+]; int fa[maxn*+]; int getfa(int x)
{
if(fa[x]==x)
return x;
else
return fa[x]=getfa(fa[x]);
} int main()
{
col=;
ttrie* root=new ttrie; memset(degree,,sizeof(degree));
cnt=;
while(scanf("%s%s",str1,str2)!=EOF)
{
int u=add(root,str1);
int v=add(root,str2);
degree[u]++;
degree[v]++;
edge[++cnt]=(tedge){u,v};
} bool flag=true;
for(int i=;i<=col;i++)
fa[i]=i;
int tot=col;
for(int i=;i<=cnt;i++)
{
int fx=getfa(edge[i].u);
int fy=getfa(edge[i].v);
if(fx!=fy)
{
fa[fx]=fy;
tot--;
}
}
if(tot>)
flag=false;
if(flag)
{
int odd=;
for(int i=;i<=col;i++)
{
if(degree[i]%)
odd++;
}
if(odd!=&&odd!=)
flag=false;
}
if(flag)
printf("Possible\n");
else
printf("Impossible\n"); return ;
}

poj 2513 Colored Sticks (trie树+并查集+欧拉路)的更多相关文章

  1. POJ 2513 Colored Sticks (离散化+并查集+欧拉通路)

    下面两个写得很清楚了,就不在赘述. http://blog.sina.com.cn/s/blog_5cd4cccf0100apd1.htmlhttp://www.cnblogs.com/lyy2890 ...

  2. POJ 2513 Colored Sticks (欧拉回路+并查集+字典树)

    题目链接 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with ...

  3. poj 2513 Colored Sticks trie树+欧拉图+并查集

    点击打开链接 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27955   Accepted ...

  4. poj 2513 Colored Sticks (trie 树)

    链接:poj 2513 题意:给定一些木棒.木棒两端都涂上颜色,不同木棒相接的一边必须是 同样的颜色.求能否将木棒首尾相接.连成一条直线. 分析:能够用欧拉路的思想来解,将木棒的每一端都看成一个结点 ...

  5. poj2513 Colored Sticks —— 字典树 + 并查集 + 欧拉回路

    题目链接:http://poj.org/problem?id=2513 题解:通过这题了解了字典树.用字典树存储颜色,并给颜色编上序号.这题为典型的欧拉回路问题:将每种颜色当成一个点.首先通过并查集判 ...

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

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

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

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

  8. poj 2513 Colored Sticks(欧拉路径+并检查集合+特里)

    题目链接:poj 2513 Colored Sticks 题目大意:有N个木棍,每根木棍两端被涂上颜色.如今给定每一个木棍两端的颜色.不同木棍之间拼接须要颜色同样的 端才干够.问最后是否能将N个木棍拼 ...

  9. [欧拉] poj 2513 Colored Sticks

    主题链接: http://poj.org/problem? id=2513 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Tota ...

随机推荐

  1. 【故障公告】docker swarm 集群问题造成新版博客后台故障

    非常抱歉,今天下午 16:55~17:05 左右,由于 docker swarm 集群的突发不稳定问题造成新版博客后台(目前处于灰度发布阶段)无法正常使用,由此给您带来麻烦,请您谅解. 出故障期时,新 ...

  2. 前端的构建化工具Webpack

    经常看到如jquery-3.0.0.js和jquery-3.0.0-min.js等两相似的文件名. 其实以上两个文件名的内容是一样的,不过带min代表的是占用最小的空间,为项目提高性能.压缩的部分如换 ...

  3. 【Luogu P4779】dijkstra算法的堆优化

    Luogu P4779 利用堆/优先队列快速取得权值最小的点. 在稠密图中的表现比SPFA要优秀. #include<iostream> #include<cstdio> #i ...

  4. 【译】为什么永远都不要使用MongoDB Why You Should Never Use MongoDB

    背景 最近在学习DDIA(Designing Data-Intensive Applications)这本分布式领域非常急经典的入门书籍,里面第二章<数据模型与查询语言>,强调了对一对多. ...

  5. windows安装Pytorch报错:from torch._C import * ImportError: DLL load failed: 找不到指定的模块”解决方案

    问题描述 python环境下安装cpu版本pytorch,安装成功,但是导入出错. 报错如下 解决方法 参考博客,大家解决方法大概有:升级numpy.添加.dll文件到环境变量,均没有成功.本地pyt ...

  6. HashMap面试题,看这一篇就够了!

    目录 序言 一.JDK7中的HashMap底层实现 1.1 基础知识 1.2 put()方法 1.2.1 特殊key值处理 1.2.2 扩容 1.2.3 如何计算bucket下标? 1.2.4 在目标 ...

  7. HUB-交换机-路由器

    HUB集线器-物理层 工作原理: 机器1发送一个数据(广播发送),经过集线器hub,hub会转发到其他所有机器,其他机器接收到数据,如果数据是给自己的就收下,如果不是自己的就丢弃 集线器的作用?(su ...

  8. jQuery实现倒计时重新发送短信验证码功能示例

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  9. 【在 Nervos CKB 上做开发】Nervos CKB 脚本编程简介[3]:自定义代币

    原文作者:Xuejie 原文链接:https://xuejie.space/2019_09_06_introduction_to_ckb_script_programming_udt/ Nervos ...

  10. 堡垒机WebSSH进阶之实时监控和强制下线

    这个功能我可以不用,但你不能没有 前几篇文章实现了对物理机.虚拟机以及Kubernetes中Pod的WebSSH操作,可以方便的在web端对系统进行管理,同时也支持对所有操作进行全程录像,以方便后续的 ...