连单词成欧拉路 欧拉回路+字典树+并查集 POJ 2513 Colored Sticks
来源:互联网 发布:莆田系 知乎 编辑:程序博客网 时间:2024/06/05 09:47
http://poj.org/problem?id=2513
题意:
给你很多对单词,单词相当于一个点,一对单词相当于一条边,问这么多对的单词能否组成一条欧拉路,要求每条边都要经过。
题目数据很大,每个单词最多10个字母,据说用map映射会TLE。
所有要改用字典树进行hash。
判断欧拉路径或者回路需要满足两个条件。
图上所有点联通
度数为奇数个的节点只有0个或者2个。
判断连通性就用并查集,判断是否只有1个根节点。
再判断最后奇度节点是否只有0个或者2个。
唯有满足以上两个条件,才能构成欧拉回路,或者欧拉路径。
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 500005;const int maxnode = MAXN * 15;const int sigma_size = 26;struct Trie { int ch[maxnode][sigma_size]; int val[maxnode]; int sz, clock; void clear() { clock = sz = 1; memset(ch[0], 0, sizeof(ch[0]));} Trie() { clear(); } int idx(char c) { return c - 'a'; } int hash(char *s) { int u = 0, n = strlen(s); for(int i = 0; i < n; i++) { int c = idx(s[i]); if(!ch[u][c]) { memset(ch[sz], 0, sizeof(ch[sz])); val[sz] = 0; ch[u][c] = sz++; } u = ch[u][c]; } if(!val[u]) val[u] = clock++; return val[u]; }} trie;char str[15], str2[15];int de[MAXN], fa[MAXN];void init() { trie.clear(); memset(de, 0, sizeof(de)); for(int i = 0; i < MAXN; i++) fa[i] = i;}int find(int u) { return u == fa[u] ? u : fa[u] = find(fa[u]);}void Union(int a, int b) { int u = find(a), v = find(b); if(u == v) return ; if(u < v) fa[v] = u; else fa[u] = v;}bool judge() { int odd = 0; for(int i = 1; i < trie.clock; i++) { if(de[i] & 1) odd++; if(find(i) != 1) return false; } if(odd == 2 || odd == 0) return true; return false;}int main() { init(); int u, v; while(scanf("%s%s", str, str2) != EOF) { u = trie.hash(str); v = trie.hash(str2); de[u]++, de[v]++; Union(u, v); } printf("%s\n", judge() ? "Possible" : "Impossible"); return 0;}
0 0
- 连单词成欧拉路 欧拉回路+字典树+并查集 POJ 2513 Colored Sticks
- POJ 2513 Colored Sticks 并查集 + 字典树 + 欧拉回路
- POJ 2513 Colored Sticks(欧拉回路判断+字典树Trie+并查集)
- POJ 2513 Colored Sticks(字典树,并查集,欧拉回路)
- POJ 2513 Colored Sticks(字典树+并查集连通性+欧拉回路)
- poj 2513 Colored Sticks 并查集 字典树 欧拉回路判断
- 并查集+欧拉回路+字典树 Colored Sticks POJ 2513
- poj 2513 Colored Sticks 欧拉回路(字典树 +并查集)
- POJ 2513 Colored sticks(字典树 + 欧拉回路 + 并查集)
- POJ 题目2513 Colored Sticks(字典树+并查集+欧拉回路)
- POJ 2513 - Colored Sticks(字典树+欧拉回路+并查集 = 好题!)
- POJ 2513 Colored Sticks(字典树+并查集+欧拉回路)
- poj 2513 Colored Sticks(欧拉回路 并查集 路径压缩 字典树)(困难)
- POJ 2513 Colored Sticks(欧拉回路+字典树+并查集)
- poj-2513-Colored Sticks-字典树trie / hash +并查集 欧拉回路
- POJ Colored Sticks 2513(字典树+并查集+欧拉回路)
- POJ 2513 Colored Sticks——字典树+并查集+欧拉回路
- poj 2513 Colored Sticks (字典树,并查集,欧拉回路)
- 【KMP字符串匹配】hdu 1711 Number Sequence
- Linux安装与配置samb服务器的过程以及遇到的问题
- [软考]之进程调度
- Linux查看和杀死进程
- 使用Java 的Mongo API操作doc
- 连单词成欧拉路 欧拉回路+字典树+并查集 POJ 2513 Colored Sticks
- PAT1008 数组元素循环右移问题
- iOS 多线程实例(自定义NSOperation并传值(block,notification))
- 使用GPU在caffe上进行CNN训练
- Product of Array Except Self
- JavaScript权威指南学习之第5章 语句
- swift的类与对象讲解
- DM6437平台开发-----程序烧写2
- hdu 5428