poj 2513 Colored Sticks (trie树+并查集+欧拉路)
Time Limit: 5000MS | Memory Limit: 128000K | |
Total Submissions: 40043 | Accepted: 10406 |
Description
Input
Output
Sample Input
- blue red
- red violet
- cyan blue
- blue magenta
- magenta cyan
Sample Output
- Possible
Hint
- #include<cstdio>
- #include<algorithm>
- #include<cstring>
- #include<queue>
- #include<stack>
- #include<deque>
- typedef long long ll;
- const int maxn=;
- int col;
- struct ttrie
- {
- bool isword;
- int id;
- ttrie* next[];
- ttrie()
- {
- isword=false;
- for(int i=;i<;i++)
- next[i]=NULL;
- }
- };
- char str1[],str2[];
- int add(ttrie* root,char str[])
- {
- ttrie *p=root,*q;
- for(int i=;str[i]!='\0';i++)
- {
- int temp=str[i]-'a';
- if(p->next[temp]==NULL)
- {
- q=new ttrie;
- p->next[temp]=q;
- p=q;
- }
- else
- p=p->next[temp];
- }
- if(p->isword)
- return p->id;
- else
- {
- p->isword=true;
- p->id=++col;
- return p->id;
- }
- }
- int degree[maxn*+];
- int cnt;
- struct tedge
- {
- int u,v;
- };
- tedge edge[maxn+];
- int fa[maxn*+];
- int getfa(int x)
- {
- if(fa[x]==x)
- return x;
- else
- return fa[x]=getfa(fa[x]);
- }
- int main()
- {
- col=;
- ttrie* root=new ttrie;
- memset(degree,,sizeof(degree));
- cnt=;
- while(scanf("%s%s",str1,str2)!=EOF)
- {
- int u=add(root,str1);
- int v=add(root,str2);
- degree[u]++;
- degree[v]++;
- edge[++cnt]=(tedge){u,v};
- }
- bool flag=true;
- for(int i=;i<=col;i++)
- fa[i]=i;
- int tot=col;
- for(int i=;i<=cnt;i++)
- {
- int fx=getfa(edge[i].u);
- int fy=getfa(edge[i].v);
- if(fx!=fy)
- {
- fa[fx]=fy;
- tot--;
- }
- }
- if(tot>)
- flag=false;
- if(flag)
- {
- int odd=;
- for(int i=;i<=col;i++)
- {
- if(degree[i]%)
- odd++;
- }
- if(odd!=&&odd!=)
- flag=false;
- }
- if(flag)
- printf("Possible\n");
- else
- printf("Impossible\n");
- return ;
- }
poj 2513 Colored Sticks (trie树+并查集+欧拉路)的更多相关文章
- POJ 2513 Colored Sticks (离散化+并查集+欧拉通路)
下面两个写得很清楚了,就不在赘述. http://blog.sina.com.cn/s/blog_5cd4cccf0100apd1.htmlhttp://www.cnblogs.com/lyy2890 ...
- POJ 2513 Colored Sticks (欧拉回路+并查集+字典树)
题目链接 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with ...
- poj 2513 Colored Sticks trie树+欧拉图+并查集
点击打开链接 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27955 Accepted ...
- poj 2513 Colored Sticks (trie 树)
链接:poj 2513 题意:给定一些木棒.木棒两端都涂上颜色,不同木棒相接的一边必须是 同样的颜色.求能否将木棒首尾相接.连成一条直线. 分析:能够用欧拉路的思想来解,将木棒的每一端都看成一个结点 ...
- poj2513 Colored Sticks —— 字典树 + 并查集 + 欧拉回路
题目链接:http://poj.org/problem?id=2513 题解:通过这题了解了字典树.用字典树存储颜色,并给颜色编上序号.这题为典型的欧拉回路问题:将每种颜色当成一个点.首先通过并查集判 ...
- poj 2513 Colored Sticks( 字典树哈希+ 欧拉回路 + 并查集)
题目:http://poj.org/problem?id=2513 参考博客:http://blog.csdn.net/lyy289065406/article/details/6647445 htt ...
- POJ 2513 Colored Sticks 字典树、并查集、欧拉通路
Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some ...
- poj 2513 Colored Sticks(欧拉路径+并检查集合+特里)
题目链接:poj 2513 Colored Sticks 题目大意:有N个木棍,每根木棍两端被涂上颜色.如今给定每一个木棍两端的颜色.不同木棍之间拼接须要颜色同样的 端才干够.问最后是否能将N个木棍拼 ...
- [欧拉] poj 2513 Colored Sticks
主题链接: http://poj.org/problem? id=2513 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Tota ...
随机推荐
- HashMap的源码学习以及性能分析
HashMap的源码学习以及性能分析 一).Map接口的实现类 HashTable.HashMap.LinkedHashMap.TreeMap 二).HashMap和HashTable的区别 1).H ...
- 【控制系统数字仿真与CAD】实验三:离散相似法数字仿真
一.实验目的 1. 了解离散相似法的基本原理 2. 掌握离散相似法仿真的基本过程 3. 应用离散相似法仿真非线性系统 4. MATLAB实现离散相似法的非线性系统仿真 5. 掌握SIMULINK仿真方 ...
- Android官方提供的支持不同屏幕大小的全部方法(转)
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8830286 原文地址为:http://developer.android.com/ ...
- 使用Xdroid进行端口映射,出现adb server version (36) doesn't match this client (39); killing...的解决方案
第一反应就是adb冲突了,因为Xdroid这个产品看起来就不像是给开发人员用的模拟器,因为不能选择各种版本进行适配,所以肯定自带了一个adb. whereis命令发现果然有两个adb,一个直接是安装在 ...
- 词袋模型(BOW,bag of words)和词向量模型(Word Embedding)概念介绍
例句: Jane wants to go to Shenzhen. Bob wants to go to Shanghai. 一.词袋模型 将所有词语装进一个袋子里,不考虑其词法和语序的问题,即每个 ...
- Windows之Java开发环境快速搭建
说明:Node.js非必须,通常中小公司或创业公司,基本上都要求全栈. 补充说明: 除此之外,当公司固定JDK.Maven.Idea.Git.Node.js及其相关IDE等版本时,运维人员或者Team ...
- 数据库求闭包,求最小函数依赖集,求候选码,判断模式分解是否为无损连接,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} ...
- Slickflow.Graph 开源工作流引擎快速入门之四: 图形编码建模工具使用手册
前言: 业务人员绘制流程时,通常使用图形GUI界面交互操作来完成,然而对于需要频繁操作或者管理较多流程的系统管理用户,就需要一款辅助工具,来帮助他们快速完成流程的创建和编辑更新.Slickflow.G ...
- Android项目依赖库管理方式简介
在实际的android项目开发过程中,我们一般都会用一些现有的第三方库来实现我们的需求,避免重复造轮子.普遍使用到的,例如:网络请求库.图片处理库.界面UI库(自定义View.动画效果等).各种第三方 ...
- Alertmanager 部署配置
目录 前言 源码安装 配置 启动 配置prometheus监控Alertmanager 修改prometheus配置 重新加载配置文件 配置测试告警 修改prometheus配置 重新加载配置文件 测 ...