题目链接:http://poj.org/problem?id=2513

题解:通过这题了解了字典树。用字典树存储颜色,并给颜色编上序号。这题为典型的欧拉回路问题:将每种颜色当成一个点。首先通过并查集判断是否为连通图,再检验是否符合欧拉回路的特点。不过题目有一点很奇怪,在并查集中,若图为连通,不是有且仅有一个点的fa[]等于它自己吗,即类似于根节点?为什么会出现有0个的情况,这一点搞不懂。

判断欧拉路是否存在的方法:

有向图:图连通,有一个顶点出度大入度1,有一个顶点入度大出度1,其余都是出度=入度。

无向图:图连通,只有两个顶点是奇数度,其余都是偶数度的。

判断欧拉回路是否存在的方法:

有向图:图连通,所有的顶点出度=入度。

无向图:图连通,所有顶点都是偶数度。

一开始用C语言写,结果在传指针的时候,传的是指针的值,而不是指针的地址,所以并不能修改指针的值,故出错。

或者直接用C++的引用,简洁方便。但我还是更喜欢用C语言的传地址,因为这样更能理解其中的原理。

C语言和C++的代码都附上,区别不大。

代码如下:

C语言:

 #include<cstdio>
#include<cstring>
#include<cstdlib>
#define MAXN 500010 typedef struct node
{
struct node *next[];
int pos;
}Trie, *PT; int n,fa[MAXN],deg[MAXN]; int init(PT *p)
{
(*p) = (PT)malloc(sizeof(Trie));
(*p)->pos = ;
for(int i = ; i<; i++)
(*p)->next[i] = NULL;
} int trie(char *s, int k, PT *p)
{
if(!s[k])
{
if((*p)->pos) return (*p)->pos;
(*p)->pos = ++n;
fa[n] = n;
return (*p)->pos;
} else
{
if(!(*p)->next[s[k]-'a'])
init(&((*p)->next[s[k]-'a']));
return trie(s,k+,&((*p)->next[s[k]-'a']));
}
} int find(int a)
{
return (fa[a]==a)?a:find(fa[a]);
} void un(int x, int y)
{
x = find(x);
y = find(y);
if(x!=y)
fa[x] = y;
} int main()
{
char s1[], s2[];
int x,y,n1,n2;
PT p;
init(&p);
n1 = n2 = n = ;
memset(deg,,sizeof(deg));
while(scanf("%s%s",s1,s2)==)
{
x = trie(s1,,&p);
y = trie(s2,,&p);
un(x,y);
deg[x]++; deg[y]++;
} for(int i = ; i<=n; i++)
{
if(fa[i]==i) n1++;
if(deg[i]&) n2++;
} if(n1<= && n2<=)
puts("Possible");
else
puts("Impossible");
return ;
}

C++:

 #include<cstdio>
#include<cstring>
#include<cstdlib>
#define MAXN 500010 typedef struct node
{
struct node *next[];
int pos;
}Trie, *PT; int n,fa[MAXN],deg[MAXN]; int init(PT &p)
{
p = (PT)malloc(sizeof(Trie));
p->pos = ;
for(int i = ; i<; i++)
p->next[i] = NULL;
} int trie(char *s, int k, PT &p)
{
if(!s[k])
{
if(p->pos) return p->pos;
p->pos = ++n;
fa[n] = n;
return p->pos;
} else
{
if(!p->next[s[k]-'a'])
init(p->next[s[k]-'a']);
return trie(s,k+,p->next[s[k]-'a']);
}
} int find(int a)
{
return (fa[a]==a)?a:find(fa[a]);
} void un(int x, int y)
{
x = find(x);
y = find(y);
if(x!=y)
fa[x] = y;
} int main()
{
char s1[], s2[];
int x,y,n1,n2;
PT p;
init(p);
n1 = n2 = n = ;
memset(deg,,sizeof(deg));
while(scanf("%s%s",s1,s2)==)
{
x = trie(s1,,p);
y = trie(s2,,p);
un(x,y);
deg[x]++; deg[y]++;
} for(int i = ; i<=n; i++)
{
if(fa[i]==i) n1++;
if(deg[i]&) n2++;
} if(n1<= && n2<=)
puts("Possible");
else
puts("Impossible");
return ;
}

poj2513 Colored Sticks —— 字典树 + 并查集 + 欧拉回路的更多相关文章

  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: 40043   Accepted: 10406 ...

  3. POJ2513——Colored Sticks(Trie树+欧拉回路+并查集)

    Colored Sticks DescriptionYou are given a bunch of wooden sticks. Each endpoint of each stick is col ...

  4. Colored Sticks (字典树哈希+并查集+欧拉路)

    Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27704   Accepted: 7336 Description You ...

  5. POJ-2513 Colored Sticks---欧拉回路+并查集+字典树

    题目链接: https://vjudge.net/problem/POJ-2513 题目大意: 给一些木棍,两端都有颜色,只有两根对应的端点颜色相同才能相接,问能不能把它们接成一根木棍 解题思路: 题 ...

  6. Colored Sticks POJ - 2513 并查集+欧拉通路+字典树hash

    题意:给出很多很多很多很多个棒子 左右各有颜色(给出的是单词) 相同颜色的可以接在一起,问是否存在一种 方法可以使得所以棒子连在一起 思路:就是一个判欧拉通路的题目,欧拉通路存在:没奇度顶点   或者 ...

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

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

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

    http://poj.org/problem?id=2513 题意: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 思路: 题目很明 ...

  9. POJ2513Colored Sticks(欧拉通路)(字典树)(并查集)

                                                             Colored Sticks Time Limit: 5000MS   Memory ...

随机推荐

  1. netframework中等待多个子线程执行完毕并计算执行时间

    本文主要描述在.netframework中(实验环境.netframework版本为4.6.1)提供两种方式等待多个子线程执行完毕. ManualResetEvent 在多线程中,将ManualRes ...

  2. C++对象

  3. luogu P1103 书本整理

    题目描述 Frank是一个非常喜爱整洁的人.他有一大堆书和一个书架,想要把书放在书架上.书架可以放下所有的书,所以Frank首先将书按高度顺序排列在书架上.但是Frank发现,由于很多书的宽度不同,所 ...

  4. Linux环境下编译JDK

    环境准备 操作系统,ubuntu-14.04.6-desktop-amd64.iso,下载地址:http://59.80.44.100/releases.ubuntu.com/14.04/ubuntu ...

  5. HDU3625 Examining the Rooms

    @(HDU)[Stirling數] Problem Description A murder happened in the hotel. As the best detective in the t ...

  6. Android中Drawable知识总结

    本文是学习<Android开发艺术探索>中Drawable章节之后的一个总结. 一.常见的Drawable种类介绍 Drawable类 xml标签 描述 BitmapDrawable 表示 ...

  7. Android View源码解读:浅谈DecorView与ViewRootImpl

    前言 对于Android开发者来说,View无疑是开发中经常接触的,包括它的事件分发机制.测量.布局.绘制流程等,如果要自定义一个View,那么应该对以上流程有所了解.研究.本系列文章将会为大家带来V ...

  8. Spring 与 MyBatis 整合

    一.实验介绍 1.1 实验内容 本节课程将整合 Spring 和 MyBatis,并采用 Junit 进行单元测试. 1.2 实验知识点 Spring - MyBatis 整合 Junit 单元测试 ...

  9. Oracle Apex 有用笔记系列 6 - 可编辑交互报告 Editable Interactive Report

    据笔者所知.Apex 4.x 是没有提供可编辑交互报告组件的.这就须要我们手动实现. 事实上这也并非非常复杂,仅仅须要简单几步. 1. 依据向导建立一个interactive report.查询语句能 ...

  10. FastDFS的配置、部署与API使用解读(7)Nginx的FastDFS模块(转)

    1.Nginx的FastDFS模块什么作用? 我们在使用FastDFS部署一个分布式文件系统的时候,通过FastDFS的客户端API来进行文件的上传.下载.删除等操作.同时通过 FastDFS的HTT ...