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

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

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

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

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

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

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

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

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

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

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

代码如下:

C语言:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cstdlib>
  4. #define MAXN 500010
  5.  
  6. typedef struct node
  7. {
  8. struct node *next[];
  9. int pos;
  10. }Trie, *PT;
  11.  
  12. int n,fa[MAXN],deg[MAXN];
  13.  
  14. int init(PT *p)
  15. {
  16. (*p) = (PT)malloc(sizeof(Trie));
  17. (*p)->pos = ;
  18. for(int i = ; i<; i++)
  19. (*p)->next[i] = NULL;
  20. }
  21.  
  22. int trie(char *s, int k, PT *p)
  23. {
  24. if(!s[k])
  25. {
  26. if((*p)->pos) return (*p)->pos;
  27. (*p)->pos = ++n;
  28. fa[n] = n;
  29. return (*p)->pos;
  30. }
  31.  
  32. else
  33. {
  34. if(!(*p)->next[s[k]-'a'])
  35. init(&((*p)->next[s[k]-'a']));
  36. return trie(s,k+,&((*p)->next[s[k]-'a']));
  37. }
  38. }
  39.  
  40. int find(int a)
  41. {
  42. return (fa[a]==a)?a:find(fa[a]);
  43. }
  44.  
  45. void un(int x, int y)
  46. {
  47. x = find(x);
  48. y = find(y);
  49. if(x!=y)
  50. fa[x] = y;
  51. }
  52.  
  53. int main()
  54. {
  55. char s1[], s2[];
  56. int x,y,n1,n2;
  57. PT p;
  58. init(&p);
  59. n1 = n2 = n = ;
  60. memset(deg,,sizeof(deg));
  61. while(scanf("%s%s",s1,s2)==)
  62. {
  63. x = trie(s1,,&p);
  64. y = trie(s2,,&p);
  65. un(x,y);
  66. deg[x]++; deg[y]++;
  67. }
  68.  
  69. for(int i = ; i<=n; i++)
  70. {
  71. if(fa[i]==i) n1++;
  72. if(deg[i]&) n2++;
  73. }
  74.  
  75. if(n1<= && n2<=)
  76. puts("Possible");
  77. else
  78. puts("Impossible");
  79. return ;
  80. }

C++:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cstdlib>
  4. #define MAXN 500010
  5.  
  6. typedef struct node
  7. {
  8. struct node *next[];
  9. int pos;
  10. }Trie, *PT;
  11.  
  12. int n,fa[MAXN],deg[MAXN];
  13.  
  14. int init(PT &p)
  15. {
  16. p = (PT)malloc(sizeof(Trie));
  17. p->pos = ;
  18. for(int i = ; i<; i++)
  19. p->next[i] = NULL;
  20. }
  21.  
  22. int trie(char *s, int k, PT &p)
  23. {
  24. if(!s[k])
  25. {
  26. if(p->pos) return p->pos;
  27. p->pos = ++n;
  28. fa[n] = n;
  29. return p->pos;
  30. }
  31.  
  32. else
  33. {
  34. if(!p->next[s[k]-'a'])
  35. init(p->next[s[k]-'a']);
  36. return trie(s,k+,p->next[s[k]-'a']);
  37. }
  38. }
  39.  
  40. int find(int a)
  41. {
  42. return (fa[a]==a)?a:find(fa[a]);
  43. }
  44.  
  45. void un(int x, int y)
  46. {
  47. x = find(x);
  48. y = find(y);
  49. if(x!=y)
  50. fa[x] = y;
  51. }
  52.  
  53. int main()
  54. {
  55. char s1[], s2[];
  56. int x,y,n1,n2;
  57. PT p;
  58. init(p);
  59. n1 = n2 = n = ;
  60. memset(deg,,sizeof(deg));
  61. while(scanf("%s%s",s1,s2)==)
  62. {
  63. x = trie(s1,,p);
  64. y = trie(s2,,p);
  65. un(x,y);
  66. deg[x]++; deg[y]++;
  67. }
  68.  
  69. for(int i = ; i<=n; i++)
  70. {
  71. if(fa[i]==i) n1++;
  72. if(deg[i]&) n2++;
  73. }
  74.  
  75. if(n1<= && n2<=)
  76. puts("Possible");
  77. else
  78. puts("Impossible");
  79. return ;
  80. }

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. k8s入门简介

    1.docker的三种编排工具 Docker的第一类编排工具: a.docker compose(docker原生):只能对一个主机上的容器进行编排,无法编排多个主机上的容器; b.docker sw ...

  2. [转] 32位 PL/SQL Develope r如何连接64位的Oracle 图解

    原文地址:LINK 由于硬件技术的不断更新,Win7系统逐渐成为主流,而且计算机内存逐渐增大,为了充 分的利用内存资源(因为32为系统最多只能用到3G左右的内存),提高系统性能,很多人开始使用Win7 ...

  3. fastscript增加三方控件

    fastscript增加三方控件 A.关于如何使用第三方控件,增加方法.属性.事件)举例如下: 如:有一控件为edtbutton:TedtButton,我们需要在动态脚本中使用该控件.我们采用如下方法 ...

  4. Redis及其Sentinel配置项详细说明

    Redis及其Sentinel配置项详细说明 http://lixiaohui.iteye.com/blog/2315516

  5. PS 基础知识 渐变编辑器如何使用

    ps渐变编辑器在哪 [ 标签:渐变,ps 渐变,编辑器 ] _______志 敏 回答:3 人气:9 解决时间:2009-04-16 15:28 满意答案 你先点渐变工具 然后左上出现渐变条设置 如图 ...

  6. C++获取站点的ip地址

     #include "stdafx.h" #include <winsock2.h> #pragma comment (lib,"ws2_32.lib&q ...

  7. php empty、isset、is_null区别

    有关 PHP 的 empty(),isset() 还有 is_null() 这三个函数的用法讨论得已经很多了,而且很多资料也未必能说得很清楚.这里再重复一次,但不是从概念去说,直接用程序例子来说话,应 ...

  8. 【CODEFORCES】 B. Dreamoon and Sets

    B. Dreamoon and Sets time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. 基于flask做权限控制

    和Django实现的原理类似,有时间补充

  10. Tika解析word文件

    Apache POI - HWPF and XWPF - Java API to Handle Microsoft Word Files http://poi.apache.org/document/ ...