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
#include <stdio.h>
#include <iostream>
using namespace std; int id = , par[], degree[] = {}; int find_set(int x)
{
return par[x] != x ? par[x] = find_set(par[x]) : x;
} struct Trie_node
{
int flag, id;
struct Trie_node *next[];
Trie_node()
{
for(int i = ; i < ; i++)
next[i] = NULL;
flag = ;
}
}; int trie_insert(struct Trie_node *p, char s[])
{
for(int i = ; s[i]; i++)
{
if(p->next[s[i]-'a'] == NULL)
p->next[s[i]-'a'] = new Trie_node;
p = p->next[s[i]-'a'];
}
if(p->flag != )
{
p->flag = ;
p->id = ++id;
}
return p->id;
} bool check()
{
int x = find_set();
for(int i = ; i <= id; i++)
{
if(find_set(i) != x)
return ;
}
int degree_odd = ;
for(int i = ; i <= id; i++)
{
if(degree[i] & )
degree_odd++;
}
return (degree_odd == || degree_odd > ) ? : ;
} int main()
{
for(int i = ; i <= ; i++)
par[i] = i;
struct Trie_node *root = new Trie_node;
char s1[], s2[];
while(scanf("%s %s", s1, s2) != EOF)
{
int x = trie_insert(root, s1);
int y = trie_insert(root, s2);
degree[x]++;
degree[y]++;
int fx = find_set(x);
int fy = find_set(y);
if(fx != fy)
par[fx] = fy;
}
printf("%s\n", check() ? "Possible" : "Impossible");
return ;
}

POJ 2513 Colored Sticks 字典树、并查集、欧拉通路的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. poj 2513 Colored Sticks (trie 树)

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

  8. Play on Words HDU - 1116 (并查集 + 欧拉通路)

    Play on Words HDU - 1116 Some of the secret doors contain a very interesting word puzzle. The team o ...

  9. [欧拉] poj 2513 Colored Sticks

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

随机推荐

  1. IntelliJ IDEA 配置Jetty

    jetty是google app engine 在大量使用的一款服务器软件,不过当然目前还撼动不了tomcat的地位,但是jetty相当轻量级,可以自己灵活定制 资源占用少 ,所以还是有吸引力的,接下 ...

  2. 三种排序算法python源码——冒泡排序、插入排序、选择排序

    最近在学习python,用python实现几个简单的排序算法,一方面巩固一下数据结构的知识,另一方面加深一下python的简单语法. 冒泡排序算法的思路是对任意两个相邻的数据进行比较,每次将最小和最大 ...

  3. http://poj.org/problem?id=2253

    floyd的应用求每条路径两点之间最大距离的最小值 #include <iostream> #include <cstdio> #include <algorithm&g ...

  4. [D3] 4. d3.max

    how to use d3.max to normalize your dataset visually within the specific bounds of a variable domain ...

  5. inux下网络发包工具 Tcpreplay3.x。

    第1章.     说明 本文档只适用于Tcpreplay3.x. 第2章.     Tcpreplay系列工具 2.1. 概述 首先推荐一个网站:http://tcpreplay.synfin.net ...

  6. Ⅵ.AngularJS的点点滴滴-- 指令

    指令 基本用法 <html> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.2/angul ...

  7. VC++/MFC操作ini配置文件详解

    在我们写的程序当中,总有一些配置信息需要保存下来,以便完成程序的功能,最简单的办法就是将这些信息写入INI文件中,程序初始化时再读入.具体应用如下: 一.将信息写入.INI文件中. 1.所用的WINA ...

  8. linux下的openoffice安装和服务自启动

    openoffice下载并安装 wget http://sourceforge.net/projects/openofficeorg.mirror/files/4.1.1/binaries/zh-CN ...

  9. Tcp抓包以及tcp状态解释

    tcp三次握手 发送端发送一个SYN=1,ACK=0标志的数据包给接收端,请求进行连接,这是第一次握手:接收端收到请求并且允许连接的话,就会发送一个SYN=1,ACK=1标志的数据包给发送端,告诉它, ...

  10. Oracle创建存储过程、执行存储过程基本语法

    >>>>>>>>>>>>>>>>>>>>>>>>> ...