题目链接: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(欧拉路径+并检查集合+特里)的更多相关文章

  1. [欧拉] poj 2513 Colored Sticks

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

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

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

  3. POJ 2513 Colored Sticks

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 28036   Accepted: 7428 ...

  4. poj 2513 Colored Sticks (trie 树)

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

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

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27097   Accepted: 7175 ...

  6. poj 2513 Colored Sticks (trie树+并查集+欧拉路)

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

  7. POJ 2513 - Colored Sticks - [欧拉路][图的连通性][字典树]

    题目链接: http://poj.org/problem?id=2513 http://bailian.openjudge.cn/practice/2513?lang=en_US Time Limit ...

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

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

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

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

随机推荐

  1. 【微信公众平台开发】百度周边搜索接口php封装

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWl2YW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...

  2. iOS开发无第三方控件的援助达到的效果侧边栏

    最近的研究iOS程序侧边栏.渐渐的发现iOS该方案还开始采取风侧边栏格该,QQ,今日头条,Path(Path运营商最早的侧边栏app该,效果说成是Path效果),所以就研究了下. 然后发现Git Hu ...

  3. python2编码总结(转)

    以下依次列出python2常遇到的几个问题及讲解. # -*- coding:utf-8 -*- python2默认以ASCII编码,但是在实际编码过程中,我们会用到很多中文,为了不使包含中文的程序报 ...

  4. 基于docker构建jenkins和svn服务(转)

    码农们很定都知道svn的重要性,机器坏掉丢代码的惨痛教训想必很多人都有. jenkins可能很多人都不了解.这是一个持续集成的工具,在敏捷开发领域很流行:跟svn结合可以实现定期build.check ...

  5. 【白注意】Windows XP 大胆拥抱Linux在系统中所遇到的问题

    Windows XP至4月8日本将不再是微软官方技术支持.虽然仍可以继续使用,但他们大部分的风险.可一不留神被黑客攻击.似向下,Linux这也是一个不错的选择. 或许很多文章开始教你如何迁移,您,迁移 ...

  6. Could not roll back JDBC transaction途径

    [异常]接口数量:DM02;错误代码:ERR_EAI_02_014; 错误叙述性说明:当将中间库异常Could not roll back JDBC transaction; nested excep ...

  7. HDoj-1879-畅通project-并查集

    继续畅通project Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  8. Ohloh研究报告

    1.底       由于近期接合ospaf同样是一个开源项目的成熟度分析工具,由于该项目现在Ohloh我们有共同的东西,这么ohloh我们进行了调查.       简单的说,初始ohloh是一个代码搜 ...

  9. Linux访问Windows共享文件夹 (转)

    在开发Linux程序中,经常会使用Windows的编辑器进行编辑,这样还要拷贝到Linux服务器,如果有这种开发习惯的同学,可以使用这种方式进行开发. 当然除了这种方法之外,可以再Windows安装n ...

  10. JS基础——数组总结

    JS中数组被觉得是一种对象,慢慢的,怎么忽然感觉,JS中仅仅要能够独立出来的概念怎么都能够当成对象来解释呢?有点儿怀疑.继续学吧.先来总结一下JS中数组是怎样详细使用的. 一.创建 数组的创建在JS中 ...