poj2513 Colored Sticks —— 字典树 + 并查集 + 欧拉回路
题目链接: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 —— 字典树 + 并查集 + 欧拉回路的更多相关文章
- 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: 40043 Accepted: 10406 ...
- POJ2513——Colored Sticks(Trie树+欧拉回路+并查集)
Colored Sticks DescriptionYou are given a bunch of wooden sticks. Each endpoint of each stick is col ...
- Colored Sticks (字典树哈希+并查集+欧拉路)
Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27704 Accepted: 7336 Description You ...
- POJ-2513 Colored Sticks---欧拉回路+并查集+字典树
题目链接: https://vjudge.net/problem/POJ-2513 题目大意: 给一些木棍,两端都有颜色,只有两根对应的端点颜色相同才能相接,问能不能把它们接成一根木棍 解题思路: 题 ...
- Colored Sticks POJ - 2513 并查集+欧拉通路+字典树hash
题意:给出很多很多很多很多个棒子 左右各有颜色(给出的是单词) 相同颜色的可以接在一起,问是否存在一种 方法可以使得所以棒子连在一起 思路:就是一个判欧拉通路的题目,欧拉通路存在:没奇度顶点 或者 ...
- POJ 2513 Colored Sticks (欧拉回路 + 字典树 +并查集)
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27097 Accepted: 7175 ...
- POJ 2513 Colored Sticks(欧拉道路+字典树+并查集)
http://poj.org/problem?id=2513 题意: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 思路: 题目很明 ...
- POJ2513Colored Sticks(欧拉通路)(字典树)(并查集)
Colored Sticks Time Limit: 5000MS Memory ...
随机推荐
- k8s入门简介
1.docker的三种编排工具 Docker的第一类编排工具: a.docker compose(docker原生):只能对一个主机上的容器进行编排,无法编排多个主机上的容器; b.docker sw ...
- [转] 32位 PL/SQL Develope r如何连接64位的Oracle 图解
原文地址:LINK 由于硬件技术的不断更新,Win7系统逐渐成为主流,而且计算机内存逐渐增大,为了充 分的利用内存资源(因为32为系统最多只能用到3G左右的内存),提高系统性能,很多人开始使用Win7 ...
- fastscript增加三方控件
fastscript增加三方控件 A.关于如何使用第三方控件,增加方法.属性.事件)举例如下: 如:有一控件为edtbutton:TedtButton,我们需要在动态脚本中使用该控件.我们采用如下方法 ...
- Redis及其Sentinel配置项详细说明
Redis及其Sentinel配置项详细说明 http://lixiaohui.iteye.com/blog/2315516
- PS 基础知识 渐变编辑器如何使用
ps渐变编辑器在哪 [ 标签:渐变,ps 渐变,编辑器 ] _______志 敏 回答:3 人气:9 解决时间:2009-04-16 15:28 满意答案 你先点渐变工具 然后左上出现渐变条设置 如图 ...
- C++获取站点的ip地址
#include "stdafx.h" #include <winsock2.h> #pragma comment (lib,"ws2_32.lib&q ...
- php empty、isset、is_null区别
有关 PHP 的 empty(),isset() 还有 is_null() 这三个函数的用法讨论得已经很多了,而且很多资料也未必能说得很清楚.这里再重复一次,但不是从概念去说,直接用程序例子来说话,应 ...
- 【CODEFORCES】 B. Dreamoon and Sets
B. Dreamoon and Sets time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 基于flask做权限控制
和Django实现的原理类似,有时间补充
- Tika解析word文件
Apache POI - HWPF and XWPF - Java API to Handle Microsoft Word Files http://poi.apache.org/document/ ...