POJ2513——Colored Sticks(Trie树+欧拉回路+并查集)
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树+欧拉回路+并查集)的更多相关文章
- POJ2513:Colored Sticks(字典树+欧拉路径+并查集)
http://poj.org/problem?id=2513 Description You are given a bunch of wooden sticks. Each endpoint of ...
- poj 2513 Colored Sticks trie树+欧拉图+并查集
点击打开链接 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27955 Accepted ...
- POJ 2513 Colored Sticks 字典树、并查集、欧拉通路
Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some ...
- poj2513 Colored Sticks —— 字典树 + 并查集 + 欧拉回路
题目链接:http://poj.org/problem?id=2513 题解:通过这题了解了字典树.用字典树存储颜色,并给颜色编上序号.这题为典型的欧拉回路问题:将每种颜色当成一个点.首先通过并查集判 ...
- POJ2513 Colored Sticks(Trie+欧拉回路)
Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some ...
- poj 2513 Colored Sticks (trie树+并查集+欧拉路)
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 40043 Accepted: 10406 ...
- poj2513连接木棍(字典树+欧拉回路+并查集)
题目传送门 题目大意:给你一堆木棍,每根木管都有两种颜色,相同颜色的部分可以连接起来,问你这堆木棍可不可以连接成1根. 思路:大致的思路很好想,就是判断欧拉回路的方法(1.联通,2,要么顶点读书全为偶 ...
- poj 2513 Colored Sticks (trie 树)
链接:poj 2513 题意:给定一些木棒.木棒两端都涂上颜色,不同木棒相接的一边必须是 同样的颜色.求能否将木棒首尾相接.连成一条直线. 分析:能够用欧拉路的思想来解,将木棒的每一端都看成一个结点 ...
- Educational Codeforces Round 51 (Rated for Div. 2) G. Distinctification(线段树合并 + 并查集)
题意 给出一个长度为 \(n\) 序列 , 每个位置有 \(a_i , b_i\) 两个参数 , \(b_i\) 互不相同 ,你可以进行任意次如下的两种操作 : 若存在 \(j \not = i\) ...
随机推荐
- win7中CIFS挂载和解挂
1.win7挂载CIFS共享至Z盘指令(用户名:test,密码:123456): net use Z: \\192.168.8.63\ygcd\duanxiuwei 123456 /USER:test ...
- Geographic Coordinate Systems
Coordinate Systems Geographic Coordinate Systems This is an archive of a previous version of the Arc ...
- opener 属性是一个可读可写的属性,可返回对创建该窗口的 Window 对象的引用
opener 属性是一个可读可写的属性,可返回对创建该窗口的 Window 对象的引用
- JS判断设备终端(PC,iPad,iPhone,android,winPhone)和浏览器
JS判断设备终端(PC,iPad,iPhone,android,winPhone)和浏览器 var ua = navigator.userAgent; var browser = {}, weixin ...
- HTML5 + SOCKET视频传输
<html> <head> <meta http-equiv="content-type" content="text/html; char ...
- 1043. Is It a Binary Search Tree
http://www.patest.cn/contests/pat-a-practise/1043 #include <stdio.h> #include <vector> u ...
- C语言字符串与字符数组
字符串儿与字符数组 字符数组的定义: Char buffer[]; 字符数组初始化: Char buffer1[]="hello world"; 利用scanf输入一个字符串儿 代 ...
- 三类,23种设计模式,速记理解法!PHP
一,创建型设计模式 1.FACTORY—追MM少不了请吃饭了,麦当劳的鸡翅和肯德基的鸡翅都是MM爱吃的东西,虽然口味有所不同,但不管你带MM去麦当劳或肯德基,只管向服务员说“来四个鸡翅”就行了.麦当劳 ...
- WebApp之 apple-touch-icon
在iPhone,iPad,iTouch的safari上可以使用添加到主屏按钮将网站添加到主屏幕上.apple-touch-icon是IOS设备的私有标签,如果设置了相应apple-touch-icon ...
- Netty 4.0 demo
netty是一个异步,事件驱动的网络编程框架&工具,使用netty,可以快速开发从可维护,高性能的协议服务和客户端应用.是一个继mina之后,一个非常受欢迎的nio网络框架 netty4.x和 ...