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. GitHub简历

    My GitHub Résumé可以帮你生成一份github简历,你只需要输入你的github用户名.

  2. [C#] 常用工具类——系统日志类

    using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; namespa ...

  3. [Oracle] - 性能优化工具(5) - AWRSQL

    在AWR中定位到问题SQL语句后想要了解该SQL statement的详细运行计划,于是就用AWR报告中得到的SQL ID去V$SQL等几个动态性能视图中查询,但发现V$SQL或V$SQL_PLAN视 ...

  4. RichTextBox选中文本时往自己的其他的位置实现拖拽

          private void Form1_Load(object sender, EventArgs e) { richTextBox1.AllowDrop = true; richTextB ...

  5. Reactor构架模式--转载

    原文:http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece76310468a3b404380143c86964868d4e419ce3b464 ...

  6. vs2010 Express 下载连接

    记录: vs2010  Express 下载连接 正式下载链接: http://download.microsoft.com/download/5/C/1/5C156922-CA10-49D8-B7E ...

  7. IHttpModule接口

    IHttpModule向实现类提供模块初始化和处置事件. IHttpModule包含兩個方法: public void Init(HttpApplication context);public voi ...

  8. c# 格式化百分比

    代码示例: <%#Eval("total").ToString() == "0" ? "00.00%" : (Convert.ToDe ...

  9. Python教程:操作数据库,MySql的安装详解

    各位志同道合的同仁请点击上方关注 本教程是基于Python语言的深入学习.本次主要介绍MySql数据库软件的安装.不限制语言语法,对MySql数据库安装有疑惑的各位同仁都可以查看一下. 如想查看学习P ...

  10. 核心运营报表无线端数据,pv,uv相关数据,从9月1号开始就没了,为什么?

    问题现象截图 核心运营报表 从获取数据的api的地址可以看出: http://data.51buy.com/json.php?biz=statistic&mod=OrderKeyData&am ...