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. 第一章 搭建Qt开发环境

    第一章 搭建Qt开发环境 1.到http://download.qt-project.org/archive/上下载Qt的源码包.我下载的是qt-everywhere-opensource-src-4 ...

  2. qml自定义标题栏

    要实现自定义的标题栏只需在原来的窗口的基础上创建一个Rectangle并将其定位在窗口顶部即可,实现代码如下: ApplicationWindow { id: mainWindow visible: ...

  3. DTCMS自定义标签,tags分割

    DTcms.Web.UI\Label\article.cs /// <summary> /// 自定义:分割tags /// </summary> /// <param ...

  4. 【Android】Android SDK在线更新镜像服务器

    Android SDK在线更新镜像服务器 中国科学院开源协会镜像站地址: IPV4/IPV6: http://mirrors.opencas.cn 端口:80 IPV4/IPV6: http://mi ...

  5. mongodb下如何开启不同端口,本地远程ip的服务器呢

    mongod --bind_ip 10.0.10.27 --port 28000 像这样可以绑定ip,绑定地址

  6. Python-Day1 Python基础学习

    一.Python3.5.X安装 1.Windows Windows上找度娘搜索“Python for windows下载”就OK了,安装的时候可以勾选设置环境变量,也可以安装完手动设置,这样在cmd中 ...

  7. spring mvc 初步接触学习笔记

    1.使用maven导入spring mvc web 的jar 包 最新语句 <dependency> <groupId>org.springframework</grou ...

  8. Android UI学习前言:Android UI系统的知识结构

    Android UI系统的知识结构如下图所示: 对于 一个GUI系统地使用,首先是由应用程序来控制屏幕上元素的外观和行为,这在各个GUI系统中是不相同的,但是也具有相通性.Android系统在这方面, ...

  9. Java实现mysql数据库备份

    Runtime是一个与JVM运行时环境有关的类,这个类是Singleton的. Runtime.getRuntime()可以取得当前JVM的运行时环境,这也是在Java中唯一一个得到运行时环境的方法. ...

  10. easy ui 表单元素input控件后面加说明(红色)

    <%-- 上传图片到图库基本信息且将图片关联到图集 开始--%> <div id="win_AddPicLib" class="easyui-windo ...