Colored Sticks
Time Limit: 5000MS   Memory Limit: 128000K
Total Submissions: 40043   Accepted: 10406

Description

You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a straight line such that the colors of the endpoints that touch are of the same color?

Input

Input is a sequence of lines, each line contains two words, separated by spaces, giving the colors of the endpoints of one stick. A word is a sequence of lowercase letters no longer than 10 characters. There is no more than 250000 sticks.

Output

If the sticks can be aligned in the desired way, output a single line saying Possible, otherwise output Impossible.

Sample Input

  1. blue red
  2. red violet
  3. cyan blue
  4. blue magenta
  5. magenta cyan

Sample Output

  1. Possible

Hint

Huge input,scanf is recommended.
 
题目大意:
给定n根木棒,首尾各有一种颜色。求问有没有可能,将这些木棒连成一排,使得接头处颜色相同。
 
判断无向图是否存在欧拉路经典题。
一根木棒其实就是给两种颜色连上一条无向边(可能会有重边的)。那么一条欧拉路就相当于一种连接方式。
无向图存在欧拉图的条件:
1)联通。这个交给并查集就好了。
2)度数为奇数的点只能是0个或2个。这个建图的时候或建完图后都挺好处理的。
主要是如何建图呢。字符串太多了,要hash成数。用字典树比较合适。
每次遇见一种颜色,看字典树中有没有该颜色。如果有,返回该颜色的id,否则加入该颜色,id取全局变量++col。
 
  1. #include<cstdio>
  2. #include<algorithm>
  3. #include<cstring>
  4. #include<queue>
  5. #include<stack>
  6. #include<deque>
  7. typedef long long ll;
  8. const int maxn=;
  9.  
  10. int col;
  11. struct ttrie
  12. {
  13. bool isword;
  14. int id;
  15. ttrie* next[];
  16. ttrie()
  17. {
  18. isword=false;
  19. for(int i=;i<;i++)
  20. next[i]=NULL;
  21. }
  22. };
  23.  
  24. char str1[],str2[];
  25.  
  26. int add(ttrie* root,char str[])
  27. {
  28. ttrie *p=root,*q;
  29. for(int i=;str[i]!='\0';i++)
  30. {
  31. int temp=str[i]-'a';
  32. if(p->next[temp]==NULL)
  33. {
  34. q=new ttrie;
  35. p->next[temp]=q;
  36. p=q;
  37. }
  38. else
  39. p=p->next[temp];
  40. }
  41. if(p->isword)
  42. return p->id;
  43. else
  44. {
  45. p->isword=true;
  46. p->id=++col;
  47. return p->id;
  48. }
  49. }
  50.  
  51. int degree[maxn*+];
  52. int cnt;
  53. struct tedge
  54. {
  55. int u,v;
  56. };
  57. tedge edge[maxn+];
  58.  
  59. int fa[maxn*+];
  60.  
  61. int getfa(int x)
  62. {
  63. if(fa[x]==x)
  64. return x;
  65. else
  66. return fa[x]=getfa(fa[x]);
  67. }
  68.  
  69. int main()
  70. {
  71. col=;
  72. ttrie* root=new ttrie;
  73.  
  74. memset(degree,,sizeof(degree));
  75. cnt=;
  76. while(scanf("%s%s",str1,str2)!=EOF)
  77. {
  78. int u=add(root,str1);
  79. int v=add(root,str2);
  80. degree[u]++;
  81. degree[v]++;
  82. edge[++cnt]=(tedge){u,v};
  83. }
  84.  
  85. bool flag=true;
  86. for(int i=;i<=col;i++)
  87. fa[i]=i;
  88. int tot=col;
  89. for(int i=;i<=cnt;i++)
  90. {
  91. int fx=getfa(edge[i].u);
  92. int fy=getfa(edge[i].v);
  93. if(fx!=fy)
  94. {
  95. fa[fx]=fy;
  96. tot--;
  97. }
  98. }
  99. if(tot>)
  100. flag=false;
  101. if(flag)
  102. {
  103. int odd=;
  104. for(int i=;i<=col;i++)
  105. {
  106. if(degree[i]%)
  107. odd++;
  108. }
  109. if(odd!=&&odd!=)
  110. flag=false;
  111. }
  112. if(flag)
  113. printf("Possible\n");
  114. else
  115. printf("Impossible\n");
  116.  
  117. return ;
  118. }

poj 2513 Colored Sticks (trie树+并查集+欧拉路)的更多相关文章

  1. POJ 2513 Colored Sticks (离散化+并查集+欧拉通路)

    下面两个写得很清楚了,就不在赘述. http://blog.sina.com.cn/s/blog_5cd4cccf0100apd1.htmlhttp://www.cnblogs.com/lyy2890 ...

  2. POJ 2513 Colored Sticks (欧拉回路+并查集+字典树)

    题目链接 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with ...

  3. poj 2513 Colored Sticks trie树+欧拉图+并查集

    点击打开链接 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27955   Accepted ...

  4. poj 2513 Colored Sticks (trie 树)

    链接:poj 2513 题意:给定一些木棒.木棒两端都涂上颜色,不同木棒相接的一边必须是 同样的颜色.求能否将木棒首尾相接.连成一条直线. 分析:能够用欧拉路的思想来解,将木棒的每一端都看成一个结点 ...

  5. poj2513 Colored Sticks —— 字典树 + 并查集 + 欧拉回路

    题目链接:http://poj.org/problem?id=2513 题解:通过这题了解了字典树.用字典树存储颜色,并给颜色编上序号.这题为典型的欧拉回路问题:将每种颜色当成一个点.首先通过并查集判 ...

  6. poj 2513 Colored Sticks( 字典树哈希+ 欧拉回路 + 并查集)

    题目:http://poj.org/problem?id=2513 参考博客:http://blog.csdn.net/lyy289065406/article/details/6647445 htt ...

  7. POJ 2513 Colored Sticks 字典树、并查集、欧拉通路

    Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some ...

  8. poj 2513 Colored Sticks(欧拉路径+并检查集合+特里)

    题目链接:poj 2513 Colored Sticks 题目大意:有N个木棍,每根木棍两端被涂上颜色.如今给定每一个木棍两端的颜色.不同木棍之间拼接须要颜色同样的 端才干够.问最后是否能将N个木棍拼 ...

  9. [欧拉] poj 2513 Colored Sticks

    主题链接: http://poj.org/problem? id=2513 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Tota ...

随机推荐

  1. HashMap的源码学习以及性能分析

    HashMap的源码学习以及性能分析 一).Map接口的实现类 HashTable.HashMap.LinkedHashMap.TreeMap 二).HashMap和HashTable的区别 1).H ...

  2. 【控制系统数字仿真与CAD】实验三:离散相似法数字仿真

    一.实验目的 1. 了解离散相似法的基本原理 2. 掌握离散相似法仿真的基本过程 3. 应用离散相似法仿真非线性系统 4. MATLAB实现离散相似法的非线性系统仿真 5. 掌握SIMULINK仿真方 ...

  3. Android官方提供的支持不同屏幕大小的全部方法(转)

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8830286 原文地址为:http://developer.android.com/ ...

  4. 使用Xdroid进行端口映射,出现adb server version (36) doesn't match this client (39); killing...的解决方案

    第一反应就是adb冲突了,因为Xdroid这个产品看起来就不像是给开发人员用的模拟器,因为不能选择各种版本进行适配,所以肯定自带了一个adb. whereis命令发现果然有两个adb,一个直接是安装在 ...

  5. 词袋模型(BOW,bag of words)和词向量模型(Word Embedding)概念介绍

    例句: Jane wants to go to Shenzhen. Bob  wants to go to Shanghai. 一.词袋模型 将所有词语装进一个袋子里,不考虑其词法和语序的问题,即每个 ...

  6. Windows之Java开发环境快速搭建

    说明:Node.js非必须,通常中小公司或创业公司,基本上都要求全栈. 补充说明: 除此之外,当公司固定JDK.Maven.Idea.Git.Node.js及其相关IDE等版本时,运维人员或者Team ...

  7. 数据库求闭包,求最小函数依赖集,求候选码,判断模式分解是否为无损连接,3NF,BCNF

    1.说白话一点:闭包就是由一个属性直接或间接推导出的所有属性的集合. 例(1):   设有关系模式R(U,F),其中U={A,B,C,D,E,I},F={A→D,AB→E,BI→E,CD→I,E→C} ...

  8. Slickflow.Graph 开源工作流引擎快速入门之四: 图形编码建模工具使用手册

    前言: 业务人员绘制流程时,通常使用图形GUI界面交互操作来完成,然而对于需要频繁操作或者管理较多流程的系统管理用户,就需要一款辅助工具,来帮助他们快速完成流程的创建和编辑更新.Slickflow.G ...

  9. Android项目依赖库管理方式简介

    在实际的android项目开发过程中,我们一般都会用一些现有的第三方库来实现我们的需求,避免重复造轮子.普遍使用到的,例如:网络请求库.图片处理库.界面UI库(自定义View.动画效果等).各种第三方 ...

  10. Alertmanager 部署配置

    目录 前言 源码安装 配置 启动 配置prometheus监控Alertmanager 修改prometheus配置 重新加载配置文件 配置测试告警 修改prometheus配置 重新加载配置文件 测 ...