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. vue项目中安装使用vux

    vux是个vue的移动端框架. 目前移动端UI框架这么多,为啥选择vux呢?vux虽然说是个个人维护项目,但是有15000+个star,应该不比其他的团队开源框架差. 最重要的是,目前要做微信公众号和 ...

  2. ubuntu 16.04上源码编译dlib教程 | compile dlib on ubuntu 16.04

    本文首发于个人博客https://kezunlin.me/post/c6ead512/,欢迎阅读! compile dlib on ubuntu 16.04 Series Part 1: compil ...

  3. [FPGA]浅谈LCD1602字符型液晶显示器(Verilog)

    目录 概述 LCD1602 LCD1602是什么? LCD1602的管脚 RS_数据/命令选择 E_使能 D0-D7 LCD1602有个DDRAM LCD1602还有个CGROM 指令集 清屏 进入模 ...

  4. 使用node.js开发一个生成逐帧动画小工具

    在实际工作中我们已经下下来不下于一万个npm包了,像我们熟悉的 vue-cli,react-native-cli 等,只需要输入简单的命令 vue init webpack project,即可快速帮 ...

  5. 2019-10-30:渗透测试,基础学习,mssql堆叠内联注入,mongodb基础语法

    使用xp_cmdshell需要堆叠注入,http://192.168.190.148/less-1.asp?id=1';EXEC sp_configure 'show advanced options ...

  6. 基于Pytorch的简单小案例

    神经网络的理论知识不是本文讨论的重点,假设读者们都是已经了解RNN的基本概念,并希望能用一些框架做一些简单的实现.这里推荐神经网络必读书目:邱锡鹏<神经网络与深度学习>.本文基于Pytor ...

  7. Spring Security(二)--WebSecurityConfigurer配置以及filter顺序

    “致"高级"工程师(BUG工程师) 一颗折腾的心

  8. 【Android - 自定义View】之自定义View实现“刮刮卡”效果

    首先来介绍一下这个自定义View: (1)这个自定义View的名字叫做 GuaguakaView ,继承自View类: (2)这个View实现了很多电商项目中的“刮刮卡”的效果,即用户可以刮开覆盖层, ...

  9. Vue项目解析

    各个文件夹 node_modules:用来放环境依赖 public:用来放公共资源,里面的index.html文件,就是初始的挂载点.被app.vue给取代了. src:放各种资源的. assets: ...

  10. exportfs命令、NFS客户端问题、FTP介绍、使用vsftpd搭建ftp

    6月22日任务 14.4 exportfs命令14.5 NFS客户端问题15.1 FTP介绍15.2/15.3 使用vsftpd搭建ftp 14.4 exportfs命令 当我们修改nfs的配置文件e ...