题目链接: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. 浅谈APP的分享功能,有时候社交裂变形式比内容更“重要”

    回顾2018年的移动互联网,“社交裂变”“下沉”等成为年度关键词.一方面我们可以看到社交裂变助推用户增长,另一方面我们也看到了以拼多多.趣头条为代表的互联网企业对于社交裂变模式表现出的空前关注度.作为 ...

  2. 洛谷——P1988 火炬

    P1988 火炬 题目描述 2008北京奥运会,你想成为四川汶川的一名火炬手,经过层层选拔,终于到了最后一关,这一关是一道很难的题:任意给定一个正整数N(N<=100000),求一个最小的正整数 ...

  3. Light oj 1085 - All Possible Increasing Subsequences (简单dp + 离散化 + BIT)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1085 题意: 问你有多少个上升子序列. 思路: dp[i]表示以第i个数结尾的 ...

  4. C# 根据生日获取年龄

    C# 根据生日获取年龄 根据生日计算出准确的年龄,不等于0时,返回的是岁,等于0时,返回的是天(以‘-’来区分) public static string GetAgeByBirth(string B ...

  5. DBCC

    http://www.cnblogs.com/lyhabc/archive/2013/01/19/2867174.html http://www.cnblogs.com/lyhabc/articles ...

  6. ArcGIS 安装中,SQL的使用出现错误的解决

    1. SQL Server Configuration Manager 中 SQL Server Services出现 “远程调用失败..” 的问题 解决方法是卸载

  7. fastdfs-zyc监控系统的使用

    原文:http://blog.csdn.net/foreversunshine/article/details/51907659 写在前面 前面有介绍过怎么安装与使用FastDFS来进行分布式的文件存 ...

  8. 词典对象(NSDictionary和NSMutableDictionary)

    词典(dictionary)顾名思义就是同由键-值组成的数据集合.与在词典中查找单词定义一样,可以通过对象的键从词典中获取需要的对象,看到 这里,你是不是想起了java中的map?和NSArray一样 ...

  9. GOPATH设置

    go help gopath查看gopath的原文帮助信息 go env查看gopath的配置 GOPATH与工作空间 前面我们在安装Go的时候看到需要设置GOPATH变量,Go从1.1版本到1.7必 ...

  10. SilverLight:基础控件使用(1)

    ylbtech-SilverLight-Basic-Control:基础控件使用(1) 本文详解控件有: Label, TextBox, PasswordBox, Image, Button , Ra ...