题目链接: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. for 、foreach 、iterator 三种遍历方式的比较

    习惯用法 for.foreach循环.iterator迭代器都是我们常用的一种遍历方式,你可以用它来遍历任何东西:包括数组.集合等 for 惯用法: List<String> list = ...

  2. linux下添加定时任务。

    这周要做一个定时增量更新Elasticsearch索引的功能,以前没有做过定时更新,所以请教了下同事,使用crontab添加linux下的定时任务. 一.linux下定时执行任务的方法 在linux中 ...

  3. Cookie安全与CSRF和XSS攻击知识点收集

    个人简单理解: 1.XSS最简单的理解就是可以在表单提交的内容上嵌入JS执行代码,然后页面渲染的时候没有过滤时会自动执行这个脚本. 2.CSRF可以理解为当你登录了京东,浏览器上保存了你登录的Cook ...

  4. ubuntu navicat for mysql破解

    ubuntu navicat for mysql破解 ubuntu navicat for mysql只能试用14天. 破解方法:rm -rf /home/cxg/.navicat64/

  5. [Android] 环境配置之Android Studio开发NDK

    分类:Android环境搭建 (14351)  (20) ========================================================作者:qiujuer博客:bl ...

  6. Swift oc 混编 - oc导入Swift协议

    (默认已经设置好桥接头文件)1.在Swift文件中写好协议2.oc类文件中导入:"项目名-swift.h"格式的文件 即:#include "项目名-swift.h&qu ...

  7. 拦截器及 Spring MVC 整合

    一.实验介绍 1.1 实验内容 本节课程主要利用 Spring MVC 框架实现拦截器以及 Spring MVC 框架的整合. 1.2 实验知识点 Spring MVC 框架 拦截器 1.3 实验环境 ...

  8. Examples osgparticleshader例子学习

    Examples osgparticleshader  粒子与shader的使用 参考文档 http://blog.csdn.net/csxiaoshui/article/details/234345 ...

  9. Linux 主机被入侵后的处理案例

    Linux主机被入侵后的处理案例 提交 我的留言 加载中 已留言 一次Linux被入侵后的分析 下面通过一个案例介绍下当一个服务器被rootkit入侵后的处理思路和处理过程,rootkit攻击是Lin ...

  10. vue2.0 flexible.js + rem 进行自适应开发

    1.在页面中引入flexible.js base.js /** * flexible.js 阿里前端自适应解决方案 */ ;(function(win, lib) { var doc = win.do ...