poj 2513 Colored Sticks(欧拉路径+并检查集合+特里)
题目大意:有N个木棍,每根木棍两端被涂上颜色。如今给定每一个木棍两端的颜色。不同木棍之间拼接须要颜色同样的
端才干够。问最后是否能将N个木棍拼接在一起。
解题思路:欧拉通路+并查集+字典树。
欧拉通路,每一个节点的统计度,度为奇数的点不能超过2个。并查集,推断节点
是否全然联通。
字典树,映射颜色。
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 5000005;
const int sigma_size = 26;
typedef pair<int,int> pii;
int N, E, far[maxn], cnt[maxn];
pii ed[maxn];
struct Tire {
int sz, g[maxn][sigma_size];
int val[maxn];
void init();
int idx(char ch);
int insert(char* s);
}T;
inline int getfar(int x) {
return x == far[x] ? x : far[x] = getfar(far[x]);
}
bool judge() {
int ret = 0;
for (int i = 1; i <= N; i++) {
if (cnt[i]&1)
ret++;
}
if (ret > 2)
return false;
for (int i = 0; i < E; i++) {
int p = getfar(ed[i].first);
int q = getfar(ed[i].second);
if (p != q) {
N--;
far[p] = q;
}
}
return N <= 1;
}
int main () {
T.init();
N = E = 0;
char a[11], b[11];
while (scanf("%s%s", a, b) == 2) {
int p = T.insert(a);
int q = T.insert(b);
cnt[p]++, cnt[q]++;
ed[E].first = p;
ed[E].second = q;
E++;
}
printf("%s\n", judge() ? "Possible" : "Impossible");
return 0;
}
void Tire::init() {
sz = 1;
val[0] = 0;
memset(g[0], 0, sizeof(g[0]));
}
int Tire::idx(char ch) {
return ch - 'a';
}
int Tire::insert(char* s) {
int u = 0, n = strlen(s);
for (int i = 0; i < n; i++) {
int v = idx(s[i]);
if (g[u][v] == 0) {
g[u][v] = sz;
memset(g[sz], 0, sizeof(g[sz]));
val[sz++] = 0;
}
u = g[u][v];
}
if (val[u] == 0) {
val[u] = ++N;
far[N] = N;
cnt[N] = 0;
}
return val[u];
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
poj 2513 Colored Sticks(欧拉路径+并检查集合+特里)的更多相关文章
- [欧拉] poj 2513 Colored Sticks
主题链接: http://poj.org/problem? id=2513 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Tota ...
- poj 2513 Colored Sticks trie树+欧拉图+并查集
点击打开链接 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27955 Accepted ...
- POJ 2513 Colored Sticks
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 28036 Accepted: 7428 ...
- poj 2513 Colored Sticks (trie 树)
链接:poj 2513 题意:给定一些木棒.木棒两端都涂上颜色,不同木棒相接的一边必须是 同样的颜色.求能否将木棒首尾相接.连成一条直线. 分析:能够用欧拉路的思想来解,将木棒的每一端都看成一个结点 ...
- POJ 2513 Colored Sticks (欧拉回路 + 字典树 +并查集)
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27097 Accepted: 7175 ...
- poj 2513 Colored Sticks (trie树+并查集+欧拉路)
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 40043 Accepted: 10406 ...
- POJ 2513 - Colored Sticks - [欧拉路][图的连通性][字典树]
题目链接: http://poj.org/problem?id=2513 http://bailian.openjudge.cn/practice/2513?lang=en_US Time Limit ...
- 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( 字典树哈希+ 欧拉回路 + 并查集)
题目:http://poj.org/problem?id=2513 参考博客:http://blog.csdn.net/lyy289065406/article/details/6647445 htt ...
随机推荐
- POJ3061 Subsequence(二进制前缀和法律+仿真足)
二分法+前缀和法律 满足子序列长度的条件(0,n)之间,sum[x+i]-sum[i]从i元素开始序列长度x和.前缀和可在O(n)的时间内统计 sum[i]的值.再用二分找出满足条件的最小的子序列长度 ...
- Windows Server 2012启用Windows功能NetFx3时出错解决方法
作者:冰点阳光 | 可以转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明网址:http://baohua.me/operating-system/windows-server-2012- ...
- JavaIO流程--创建文件和目录的实例
*创建函数: *public boolean createNewFile():创建文件 本文假设存在.不创造(转让file.createNewFile()返回false) *public bool ...
- 播放视频的框架Vitamio的使用问题
曾经用过这个牛逼的框架,后来又任意搞了下.发现播放不了视频了.搞了老半天才搞好,今天又随便整了下,发现又不行了.我勒个插! 如今最终又搞出来了,发现我总是把步骤搞错或少写了些东西 总的步骤: 一:导入 ...
- java通过抛异常来返回提示信息
结论:如果把通过抛异常的方式得到提示信息,可以使用java.lang.Throwable中的构造函数: public Throwable(String message) { fillInStackTr ...
- 初识 Cloudera Impala
Impala是Cloudera公司主导开发的新型查询系统,它提供SQL语义,能查询存储在Hadoop的HDFS和HBase中的PB级大数据.已有的Hive系统尽管也提供了SQL语义,但因为Hive底层 ...
- [Elasticsearch] 部分匹配 (一) - 前缀查询
部分匹配(Partial Matching) 敏锐的读者可能已经发现到眼下为止,介绍的查询都是在整个词条层面进行操作的. 匹配的最小单元必须是一个词条.你仅仅能找到存在于倒排索引(Inverted I ...
- openstack 网络架构 nova-network + neutron
openstack网络架构(nova-network/neutron) openstack网络体系中,网络技术没有创新,但用到的技术点很庞杂,包含bridge.vlan.gre.vxlan.ovs.o ...
- Dojo Mobile制定学习用品
Dojo Mobile开展 App技术开发QQ群:347072638 技术咨询.APP定制开发联系邮箱:messageloop@qq.com 时代在演变.技术在革新.无论你接受不接受. 初识Dojo ...
- vs2013 ADO联系SQL server2012数据库
平时,给定ADO例如使用以下过程数据源中的数据的数据库应用程序 (1) 创建一个Connection 物.定义的连接字符串信息.它包含了数据源名称.用户ID.密码.连接超时 . 默认数据库的位置和光标 ...