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. Blocked a frame with origin XXX from accessing a cross-origin 。iframe跨域问题

    在前端开发的过程中,我们常常会用到iframe去在我们的页面中引用一个子页面,而父子页面又常常会有交互.在同域情况下,子页面如果想要访问父页面中的window对象中的方法的话,直接在当前页面中使用wi ...

  2. PHP与Python进行数据交互

    最近,决定在一个项目用tp5进行APP接口开发,用Python做数据分析,然后这就面临一个问题:PHP和Python如何进行数据交互? 思路 我解决此问题的方法是利用了PHP的passthru函数来调 ...

  3. 使用Docker搭建maven私服 及常规使用方法

    安装-登录-配置 下载镜像 docker pull sonatype/nexus3 运行 docker run -d -p 9998:8081 --name nexus --restart=alway ...

  4. 2019-9-9:渗透测试,docker下载dvwa,使用报错型sql注入dvwa

    docker下载dvwa镜像,报错型注入dvwa,low级 一,安装并配置docker 1,更新源,apt-get update && apt-get upgrade &&am ...

  5. HDFS原理概念扫盲

    1.概述 hdfs文件系统主要设计为了存储大文件的文件系统:如果有个TB级别的文件,我们该怎么存储呢?分布式文件系统未出现的时候,一个文件只能存储在个服务器上,可想而知,单个服务器根本就存储不了这么大 ...

  6. linux免密登录和设置别名

    一.免密登录 (1) 配置公钥   ssh-keygen (2)让远程服务器记住公钥   ssh-copy-id   用户名@ip地址或域名 二.设置别名 (3)在~/.ssh目录下创建并编辑conf ...

  7. 串的匹配算法--C语言实现

    串这种数据结构,使用是比较多的,但是它的一些方法在更高级的语言中,比如Java,Python中封装的比较完整了.在这里,我只写了串中使用最多的匹配算法,即串的定位操作.串的匹配算法常用的两种就是朴素匹 ...

  8. #华为云·寻找黑马程序员#微服务-你真的懂 Yaml 吗?

    在Java 的世界里,配置的事情都交给了 Properties,要追溯起来这个模块还是从古老的JDK1.0 就开始了的. "天哪,这可是20年前的东西了,我居然还在用 Properties. ...

  9. 【开发者portal在线开发插件系列四】数组 及 可变长度数组

    基础篇 基础场景见上面两个帖子,这里单独说明数组和可变长度数组的用法. 话不多说,开始今天的演(表)示(演) Profile和插件开发 添加一个string类型的属性: 在插件里添加一条数据上报消息: ...

  10. luogu P2863 [USACO06JAN]牛的舞会The Cow Prom |Tarjan

    题目描述 The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in their ...