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

解题思路:此题是一道典型的判断欧拉回路或欧拉通路的问题,以木棍的端点颜色为顶点。方法是:先用并查集判断图是否连通,然后统计奇度顶点的个数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. VideoView 视频播放 示例

    介绍 实现的功能: 可播放本地视频或网络视频,可控制播放或暂停 最小化时保存播放位置及播放状态,resume时恢复所有状态: 横竖屏切换时保持切换前的位置及状态 在屏幕上竖直滑动可调节屏幕亮度和音量 ...

  2. eclipse中svn版本不兼容问题

    eclipse中导入本地svn管理的Android项目 使用svn时弹出以下提示: org.apache.subversion.javahl.ClientException: Unsupported ...

  3. css01入门小例子

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  4. C# - 使用 OLEDB读取 excel(不用Excel对象).

    参考: How to read from an Excel file using OLEDB 为了使用方便,我做成了工具类(OledbCommon.cs),好以后使用. 注:连接字符串中,Provid ...

  5. Swift中简单的单例设计

    import Foundation class Test: NSObject { // 提供单例实例 static let shareInstance = Test() // 私有化构造方法 over ...

  6. MFC 全局配置 读取保存配置

    不知道关于全局配置别人都是怎么处理的,最近做的东西都用到全局配置,而且要保存软件的设置,下次启动时要使用上次关闭时的配置. 我的做法是建一个类用来保存和读取配置,并且在这个类中创建一些变量,供所有的界 ...

  7. 1.Tomcat配置

    1.启动 解压缩安装包后,点击startup.bat,保持控制台窗口开启 浏览器中输入http://localhost:8080 后看到启动界面则表示启动成功 点击shutdown.bat则关闭Tom ...

  8. uva 755 - 487--3279

    #include <iostream> #include <string> #include <map> #include <algorithm> #i ...

  9. Android中通过Java获取Webview加载内容

    有时候我们需要在加载webview时,获取加载完成的内容,当然,WebView也是有可能包含javascript.通过以下操作,我们是可以获取到WebView加载的内容. 1.自定义一个内部类,获取W ...

  10. 趣味PAT--循环-19. 币值转换(20)

    One visible minute on the stage is attributed to ten years of invisible practice off the stage. &quo ...