题目大意:给定一捆木棒,每根木棒的每个端点涂有某种颜色。问:是否能将这些棒子首位项链,排成一条直线,且相邻两根棍子的连接处的颜色一样。

解题思路:此题是一道典型的判断欧拉回路或欧拉通路的问题,以木棍的端点颜色为顶点。方法是:先用并查集判断图是否连通,然后统计奇度顶点的个数sumj , 如果 sumj == 0 , 则图中存在欧拉回路 ;如果 sumj == 2  , 则图中存在欧拉通路 ; 如果 sumj > 2 ,则图中不存在欧拉通路。但是此题的关键是如何给端点颜色编号,一开始,我用map映射,结果TLE,所以,我就用到了Trie 树。

Ps:此题有坑!!当没有输入时,应当输出 Possible 。。

具体请看代码:

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std ;
const int MAXN = 3e6;
int set[MAXN] ;
char s1[100] ;
char s2[100] ;
int cnt ; // 给端点编号
int d[MAXN] ; // 统计每个顶点的度
struct Tnode
{
int du ;
int xu ;
Tnode *next[26] ;
Tnode()
{
du = 0 ;
xu = 0 ;
memset(next , 0 ,sizeof(next)) ;
}
} ;
int find(int x) // 并查集
{
int r = x ;
while (r != set[r])
{
r = set[r] ;
}
return r ;
}
int inse(char * str , Tnode * root)
{
Tnode *p = root ;
while (*str)
{
int id = *str - 'a' ;
if(p -> next[id] == NULL)
{
p -> next[id] = new Tnode ;
}
++ str ;
p = p -> next[id] ;
}
if(p -> du == 0)
{
p -> xu = ++ cnt ; // 顶点颜色的编号从 1 开始
}
p -> du ++ ;
d[p -> xu] = p -> du ;
return p -> xu ;
}
int main()
{
memset(d , 0 , sizeof(d)) ;
int i ;
for(i = 1 ; i <= MAXN ; i ++)
{
set[i] = i ;
}
cnt = 0 ;
Tnode *root = new Tnode ;
while (scanf("%s%s" , s1 , s2) != EOF)
{
int x = inse(s1 , root) ;
int y = inse(s2 , root) ;
int tx = find(x) ;
int ty = find(y) ;
if(tx < ty)
{
set[ty] = tx ;
}
else
{
set[tx] = ty ;
}
}
int fz = 0 ; // 判断图的连通的分支个数
int sumj = 0 ; // 统计奇度顶点的个数
for(i = 1 ; i <= cnt ; i ++)
{
if(set[i] == i)
{
fz ++ ;
}
if(d[i] % 2 == 1)
{
sumj ++ ;
}
}
if(cnt == 0)
{
puts("Possible") ;
}
else if(fz == 1)
{
if(sumj > 2)
{
puts("Impossible") ;
}
else
{
puts("Possible") ;
}
}
else
{
puts("Impossible") ;
}
return 0 ;
}

POJ 2513 Colored Sticks - from lanshui_Yang的更多相关文章

  1. poj 2513 Colored Sticks(欧拉路径+并检查集合+特里)

    题目链接:poj 2513 Colored Sticks 题目大意:有N个木棍,每根木棍两端被涂上颜色.如今给定每一个木棍两端的颜色.不同木棍之间拼接须要颜色同样的 端才干够.问最后是否能将N个木棍拼 ...

  2. [欧拉] poj 2513 Colored Sticks

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

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

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

  4. POJ 2513 Colored Sticks

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 28036   Accepted: 7428 ...

  5. poj 2513 Colored Sticks (trie 树)

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

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

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27097   Accepted: 7175 ...

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

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

  8. POJ 2513 - Colored Sticks - [欧拉路][图的连通性][字典树]

    题目链接: http://poj.org/problem?id=2513 http://bailian.openjudge.cn/practice/2513?lang=en_US Time Limit ...

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

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

随机推荐

  1. ReSharper warning: Virtual member call in a constructor

    1.构造函数的执行顺序是:基类--->派生类 2.如果虚方法被重写后,由于基类中调用了虚方法,此时调用的是最外层的被重写后的虚方法,此时可能会发生异常 举例: class Parent { pu ...

  2. Javascript预解析、作用域、作用域链

    最近在看js的一些资料,总结一下昨晚看到的js作用域方面的知识,不准确的地方希望留言指正! 先看片段js代码如下: < script type="text/javascript&quo ...

  3. ajax+ashx 完美实现input file上传文件

    1.input file 样式不能满足需求 <input type="file" value="浏览" /> IE8效果图:    Firefox效 ...

  4. Cacti安装教程

    CentOS 6.0架设流量监控及集中日志系统 第一章.cacti的安装 1. 系统的基本设置2. 设置主机名3. [root@localhost ~]# vi /etc/sysconfig/netw ...

  5. Windows Server 2003 SP2 R2 企业版/标准版/32与64位 CD-KEY

    微软发布Windows Server 2003 R2版的目的是希望透过它填补Windows Server 2003 SP1和Longhorn Server之间的产品发布时间间隔.所以Windows S ...

  6. vs2013+sql server2012 +win8.1+entity framework + linq

    项目右键添加类选择“ADO.NET实体数据模型” 选择“空……” 项目会自动产生后缀.edmx的文件(ModelTest.edmx),会自动添加引用System.Runtime.Serializati ...

  7. PHP XML Expat 解析器

    PHP XML Expat 解析器 内建的 Expat 解析器使在 PHP 中处理 XML 文档成为可能. XML 是什么? XML 用于描述数据,其焦点是数据是什么.XML 文件描述了数据的结构. ...

  8. 如何通过Request.ServerVariables["HTTP_USER_AGENT"]获取客户端操作系统信息

    http://www.useragentstring.com/pages/api.php

  9. prototype constructor __proto__

    constructor, prototype, __proto__ 详解

  10. java 使用正则表达式对文件名非法字符处理

    1.文件名在操作系统中不允许出现  / \ " : | * ? < > 故将其以空替代       Pattern pattern = Pattern.compile(" ...