http://poj.org/problem?id=2513

Colored Sticks
Time Limit: 5000MS   Memory Limit: 128000K
Total Submissions: 37812   Accepted: 9907

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

   对每一对颜色进行连接一条无向边然后跑欧拉图验证是否可行,用了Trie来哈希字符串,并查集判断是否是连通图,坑点是有空串,,,所以可能有0个集合,此时输出"Possible";

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
using namespace std;
int N;
struct node
{
node *next[];
int v;
node(){v=;memset(next,NULL,sizeof(next));}
}*root;
int gec(char *s)
{
int len=strlen(s);
node *p=root;
for(int i=;i<len;++i)
{
if(p->next[s[i]-'a']==NULL) p->next[s[i]-'a']=new node();
p=p->next[s[i]-'a'];
}
if(p->v==) p->v=++N;
return p->v;
}
int cnt[];
int f[];
int getf(int v){return f[v]==v?v:f[v]=getf(f[v]);}
int main()
{
char s1[],s2[];
int i;
for(i=;i<=;++i)f[i]=i;
root=new node();
while(scanf("%s%s",s1,s2)!=EOF){
int u=gec(s1);
int v=gec(s2);
int fu=getf(u),fv=getf(v);
if(fu!=fv) f[fv]=fu;
cnt[u]++;
cnt[v]++;
}
int s=,x=;
for(i=;i<=N;++i)
{
if(cnt[i]%==) s++;
if(i==getf(i))x++;
}
if(x<=&&(s==||s==)) puts("Possible");
else puts("Impossible");
return ;
}

poj 2513 欧拉图/trie的更多相关文章

  1. Colored Sticks POJ - 2513(trie树欧拉路)

    题意: 就是无向图欧拉路 解析: 不能用map..超时 在判断是否只有一个联通的时候,我比较喜欢用set,但也不能用set,会超时,反正不能用stl emm 用trie树来编号就好了 #include ...

  2. poj 2513 Colored Sticks (trie 树)

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

  3. poj 3321 Apple Trie

    /* poj 3321 Apple Trie 这道题的关键是如何将一个树建成一个一维数组利用树状数组来解题! 可以利用dfs()来搞定,我们在对一个节点深搜后,所经过的节点的数目就是该节点的子树的数目 ...

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

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

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

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

  6. poj 2513 欧拉回路+并查集推断是否联通+Trie树

    http://poj.org/problem? id=2513 最初看到 第一感觉---map  一看250000的数据量 果断放弃 然后记得曾经看过.trie取代map.尤其当数据量特别大的时候 学 ...

  7. POJ 2513 trie树+并查集判断无向图的欧拉路

    生无可恋 查RE查了一个多小时.. 原因是我N define的是250500 应该是500500!!!!!!!!! 身败名裂,已无颜面对众人.. 吐槽完了 我们来说思路... 思路: 判有向图能否形成 ...

  8. poj 2513 Colored Sticks (trie树+并查集+欧拉路)

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

  9. poj 2513

    http://poj.org/problem?id=2513 73348K        1438MS        C++        1614B解题思路:欧拉路的应用 要点 :1.判断连通性   ...

随机推荐

  1. Js用户引导插件bootstrap-tour

    1.demo直接贴上来了,有什么不懂的,直接去官网上看,地址:http://bootstraptour.com/. 2.这个bootstrap-tour插件的版本是v0.12.0,复制下来代码,引入库 ...

  2. mongodb文档支持的数据类型

    版权声明:转载请标明来源. https://blog.csdn.net/u014285882/article/details/25510377 1. 存储类型 mongodb文档相似于json,但不是 ...

  3. docker学习笔记1-- 用Toolbox安装Docker--介绍Docker Machine

    使用的是Docker Toolbox,非Docker for Windows 一.docker的认识与安装(windows安装) http://blog.csdn.net/tina_ttl/artic ...

  4. UI控件之UITabBarController

    UITabBarController:标签栏控制器,继承自UIViewController,用标签栏的形式管理视图控制器,各标签栏对应的视图控制器之间相互独立,互不影响,单击标签栏,显示标签栏对应的视 ...

  5. 修改subline text3左侧样式

    安装PackageResourceViewer 快捷键 ⌘(command)+⇧(shift)+P 打开 Command Palette 输入 Package Control:Install 回车,等 ...

  6. Docker容器技术-在开发中引用Docker

    明确一点: 容器不适合构建那种发布周期以周或月为单位的大型单一架构企业软件,容器适合采用微服务的方式,以及探索诸如持续部署这样的技术,使得我们能安全地在一天内多次更新生产环境. 一.在开发中引用Doc ...

  7. iOS_触摸事件与手势识别

    目  录: 一.触摸事件 1.1iOS的输入事件 1.2 触摸事件的处理 1.3 UITouch类中包含五个属性 1.4 UITouch类中包含两个成员函数 1.5响应者链 二.手势识别 2.1使用手 ...

  8. INSPIRED启示录 读书笔记 - 第6章 招聘产品经理

    产品经理应有的特质 个人素质和态度:技术可以学习,素质却难以培养,有些素质是成功的产品经理必不可少的 对产品的热情:对产品有一种本能的热爱,是夜以继日克服困难.完善产品的动力 用户立场:能换位思考,能 ...

  9. DBUtiles中的简单使用(QueryRunner和ResultSetHandler的手动实现)

    DBUtiles是一个很好的处理JDBC的工具类.(DbUtils is a small set of classes designed to make working with JDBC easie ...

  10. Netsh 命令详解

    1. help帮助指南 2. 常用命令介绍netsh interface ip show addressnetsh interface ip dumpnetsh interface ip dump & ...