题目链接: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. Jmeter(四十九)_常用的性能测试监听器

    概述 jmeter中提供了很多性能数据的监听器,我们通过监听器可以来分析性能瓶颈 本文以500线程的阶梯加压测试结果来描述图表. 常用监听器 1:Transactions per Second 监听动 ...

  2. Codeforces Round #321 (Div. 2) Kefa and First Steps 模拟

    原题连接:http://codeforces.com/contest/580/problem/A 题意: 给你一个序列,问你最长不降子串是多长? 题解: 直接模拟就好了 代码: #include< ...

  3. POJ 3041 Asteroids 二分图

    原题连接:http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  4. 我的第一个Java程序HelloWorld

    public class HelloWorld{ public static void main(String [] args){ System.out.println("HelloWorl ...

  5. 邁向IT專家成功之路的三十則鐵律 鐵律十一:IT人應對之道-靈活

    身為一位優秀的IT專家,不能夠只是在技術面的應對能力強,而必須是在人事的應對能力上也要能夠靈活與彈性,否則就算一天給你48小時,你也會把自己的身心弄垮,再強的專業.技術.能力也會瞬間化為泡影. 坦白說 ...

  6. 【android】getDimension()、getDimensionPixelOffset()和getDimensionPixelSize()区别详解

    在自定义控件中使用自定义属性时,经常需要使用java代码获取在xml中定义的尺寸,相关有以下三个函数 getDimension() getDimensionPixelOffset() getDimen ...

  7. java单测时的等待模块awaitility

    单测时,可以用来等待异步任务完成 在编写自动化测试用例过程中,往往会遇见被测代码有异步或者队列处理的中间过程:如果需要校验这部分结果,必须等待异步操作结束或队列消费完,而这个中间等待的时间是不确定的, ...

  8. 第五讲_图像识别之图像检测Image Detection

    第五讲_图像识别之图像检测Image Detection 目录 物体检测 ILSVRC竞赛200类(每个图片多个标签):输出类别+Bounding Box(x,y,w,h) PASCAL VOC 20 ...

  9. HttpClient通过Post方式发送Json数据

    服务器用的是Springmvc,接口内容: @ResponseBody @RequestMapping(value="/order",method=RequestMethod.PO ...

  10. navicat小经验和快捷键

    1.有时按快捷键Ctrl+F搜某条数据的时候搜不到,但是能用sql查出来,这是怎么回事? Ctrl+F只能搜本页数据,不在本页的数据搜不到,navicat每页只显示1000条数据.在数据多的时候nav ...