Colored Sticks

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

题目大意:

    给定一些木条,木条的两头都有颜色,颜色相同的两根木条可以相连,问是否可以连成一条直线。

解题思路:

    其实就是问是否可以构成欧拉路径。这里给出欧拉路径的定义。

    若图G中存在这样一条路径,使得它恰通过G中每条边一次,则称该路径为欧拉路径。若该路径是一个圈,则称为欧拉(Euler)回路。(即一笔画问题)

    存在欧拉路径的成分必要条件为:一个无向图存在欧拉回路,当且仅当该图只存在0或2个奇数入度数的顶点,且该图是连通图。

    1)判断 奇数度数的个数

      很好搞定,建立一个数组存放每个节点的入度,建图结束后遍历即可。

    2)判断是否为连通图

      使用并查集和路径压缩来做。若路径压缩之后每个点都有着相同的父节点(即每个点都有相同的祖先节点)。说明图连通。

    Trie树的应用

    由于该题的Node为字符串,建图的时候会比较困难。使用map映射会超时,所以使用Trie树来进行映射。具体实现请看代码备注。

Code:

 /*************************************************************************
> File Name: poj2513.cpp
> Author: Enumz
> Mail: 369372123@qq.com
> Created Time: 2014年10月26日 星期日 20时16分17秒
************************************************************************/ #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<list>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#define MAXN 500001
using namespace std;
class TrieTree_Node
{
public:
bool flag; //判断是否为一个单词的叶子节点
int id; //为每一个单词分配一个id,相当于映射关系
TrieTree_Node *next[];
TrieTree_Node()
{
flag=;
id=;
memset(next,,sizeof(next));
}
}root;
int cnt=;
int degree[MAXN]; //入度数组
int father[MAXN];
int tree(char *s)
{
TrieTree_Node *p=&root;
int len=strlen(s);
for (int i=;i<=len-;i++)
{
int tmp=s[i]-'a';
if (p->next[tmp]==NULL)
p->next[tmp]=new TrieTree_Node;
p=p->next[tmp];
}
if (p->flag) //如果找到了该字符串,返回其ID
return p->id;
else //没有找到,就创建这个字符串,并标记叶子节点,分配ID
{
p->flag=;
p->id=++cnt;
return p->id;
}
}
int find(int a)
{
if (father[a]!=a)
father[a]=find(father[a]);
return father[a];
}
void join(int a,int b)
{
int fx=find(a),fy=find(b);
if (fx!=fy)
father[fx]=fy;
}
void init()
{
for (int i=;i<MAXN;i++) //并查集初始化,每一个节点相当于一棵树
father[i]=i;
}
int main()
{
char a[],b[];
init();
while (scanf("%s %s",a,b)!=EOF) //无向图,两个Node的入度都要加
{
int i=tree(a);
int j=tree(b);
degree[i]++;
degree[j]++;
join(i,j);
}
bool ok=;
int cnt_degree=;
for (int i=;i<=cnt;i++)
if (degree[i]%!=) cnt_degree++;
if (!(cnt_degree==||cnt_degree==))
ok=;
if (ok)
{
int tmp=find();
for (int i=;i<=cnt;i++)
{
if (tmp!=find(i))
ok=;
}
}
if (ok)
cout<<"Possible"<<endl;
else
cout<<"Impossible"<<endl;
return ;
}

    

POJ2513——Colored Sticks(Trie树+欧拉回路+并查集)的更多相关文章

  1. POJ2513:Colored Sticks(字典树+欧拉路径+并查集)

    http://poj.org/problem?id=2513 Description You are given a bunch of wooden sticks. Each endpoint of ...

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

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

  3. POJ 2513 Colored Sticks 字典树、并查集、欧拉通路

    Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some ...

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

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

  5. POJ2513 Colored Sticks(Trie+欧拉回路)

    Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some ...

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

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

  7. poj2513连接木棍(字典树+欧拉回路+并查集)

    题目传送门 题目大意:给你一堆木棍,每根木管都有两种颜色,相同颜色的部分可以连接起来,问你这堆木棍可不可以连接成1根. 思路:大致的思路很好想,就是判断欧拉回路的方法(1.联通,2,要么顶点读书全为偶 ...

  8. poj 2513 Colored Sticks (trie 树)

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

  9. Educational Codeforces Round 51 (Rated for Div. 2) G. Distinctification(线段树合并 + 并查集)

    题意 给出一个长度为 \(n\) 序列 , 每个位置有 \(a_i , b_i\) 两个参数 , \(b_i\) 互不相同 ,你可以进行任意次如下的两种操作 : 若存在 \(j \not = i\) ...

随机推荐

  1. Word中批量替换软回车

    在平时工作中,有时候需要拷贝一些截取自网页上的文字,当选中后拷贝到Word中时,有时候在每行的结尾出现如下的符号,,这给后期文字的整理带来了很多不便,在此记录从网上获取的解决方法,以免遗忘和便于查找. ...

  2. java 枚举 类 enum

    public abstract class Enum<E extends Enum<E>> implements Comparable<E>, Serializab ...

  3. Spring MVC Controller中GET方式传过来的中文参数会乱码的问题

    Spring MVC controller 这样写法通常意味着访问该请求,GET和POST请求都行,可是经常会遇到,如果碰到参数是中文的,post请求可以,get请求过来就是乱码.如果强行对参数进行了 ...

  4. 利用WeX5集成百度地图

    最近做一个地图类的app经过几天的摸索,终于完成百度地图集成的界面先看效果:1.加载完成之后,页面加载制定位置的地图2.顶部能够输入地图的关键字,地图显示符合条件的下拉列表3.用户选择了相应的选项后, ...

  5. javascript获取ckeditor编辑器的值(实现代码)

    CKeditor编辑器是FCKeditor的升级版本想对于FCK来说,确实比较好用,加载速度也比较快以下是如果通过JS获取CKeditor编辑器的值,用于表单验证 if(CKEDITOR.instan ...

  6. JQuery淡入淡出 banner切换特效

    附件中提供另一种实现方式 基本类似 主要的实现方法如下: var ShowAD=function(i){   showImg.eq(i).animate({opacity:1},settings.sp ...

  7. jquery 获取 CheckBox 的状态

    <td style="width:220px;vertical-align:central;"><input type="checkbox" ...

  8. 字符设备驱动中cdev与inode、file_operations的关系

    一.cdev与inode 二.cdev与file_operations

  9. 《C和指针》 读书笔记 -- 第8章 数组

    1.在C中,数组名的值是一个指针常量而不是指针变量,也就是数组第一个元素的地址. 2.数组和指针的区别: 声明一个数组时,编译器将根据声明所指定的元素数量为数组保留内存空间,然后再创建数组名,它的值是 ...

  10. Python操作RabbitMQ初体验(一)

    由于想用Python实现一套分布式系统,来管理和监控CDN的内容与运行状态,误打误撞认识了RabbitMQ,推荐的人很多,如余锋<我为什么要选择RabbitMQ>等等. 在MQ这个词汇映入 ...