题目链接: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. iOS多用连接、反向协议、安全

    资源 WWDC-2013-Session-708 BlackHat-US-2014-"It Just (Net)works" Understanding Multipeer Con ...

  2. Machine Learning—Linear Regression

    Evernote的同步分享:Machine Learning-Linear Regression 版权声明:本文博客原创文章.博客,未经同意,不得转载.

  3. SQLServer RESOURCE_SEMAPHORE 等待状态

    原文:SQLServer RESOURCE_SEMAPHORE 等待状态 概述: 当一个SQLServer实例运行得很慢的时候,应该做一些检查,如检查等待状态.最好的方法是一开始就建立一个性能基线,以 ...

  4. B/S在北大青鸟-ASP.NET 总结

    一个.前言: 这几周跟着于海涛老师进入了.NET编程世界.领略到了ASP.NET的精髓. 要说起ASP.NET的发展史,那要追溯到HTML了,由于它功能简单,无法从用户接收信息并自己主动进行更新.而不 ...

  5. cocos2d-x3.0 windows 环境配置

    cocos2d-x3.0 windows 环境配置 参考Oo泡泡糖oO的CSDN博文 :http://blog.csdn.net/u010296979/article/details/24273393 ...

  6. NSIS:在注册表中记录安装路径以便重装或升级时读取

    原文 NSIS:在注册表中记录安装路径以便重装或升级时读取 在NSIS中,这个功能是非常有用的,可以避免用户把程序安装到多个位置的尴尬. 第1步:在“安装目录选择页面”前面加入以下代码: 1 !def ...

  7. Windows Phone获取WiFi BSSID

    原文:Windows Phone获取WiFi BSSID BSSID,一种特殊的Ad-hoc LAN的应用,也称为Basic Service Set (BSS),一群计算机设定相同的BSS名称,即可自 ...

  8. malloc实现原理

    记得早一段时间,看到一本书上写过delete的一个..今天突然找啦一下资料: malloc()是C语言中动态存储管理 的一组标准库函数之中的一个.其作用是在内存的动态存储区中分配一个长度为size的连 ...

  9. asp.net学习之再论sqlDataSource

    原文:asp.net学习之再论sqlDataSource 本节从上一节没有阐述的几个方面,再讨论一下SqlDataSource的用法及注意的事项.     上一节的链接地址如下:http://www. ...

  10. hdu 1542 Atlantis 段树区,并寻求,,,尼玛真坑人数据,不要打开一小阵!

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...