【POJ】2513 Colored Sticks
字典树+并查集。
#include <cstdio>
#include <cstring>
#include <cstdlib> #define MAXN 500005
#define MAXL 11
#define TRIEN 26 typedef struct Trie {
int v;
Trie *next[TRIEN];
Trie() {
v = ;
for (int i=; i<TRIEN; ++i)
next[i] = NULL;
}
} Trie; Trie root;
int pre[MAXN], deg[MAXN], n = ; void create(char str[], int in) {
int i = , id;
Trie *p = &root, *q; while (str[i]) {
id = str[i] - 'a';
++i;
if (p->next[id] == NULL) {
q = new Trie();
p->next[id] = q;
}
p = p->next[id];
}
p->v = in;
} int Tfind(char str[]) {
int i = , id;
Trie *p = &root; while (str[i]) {
id = str[i] - 'a';
++i;
if (p->next[id] == NULL)
return ;
p = p->next[id];
} return p->v;
} int find(int x) {
return x==pre[x] ? x:pre[x]=find(pre[x]);
} void merge(int a, int b) {
a = find(a);
b = find(b);
if (a != b)
pre[b] = a;
} bool judge() {
int cnt = , i;
if (n == )
return true;
for (i=; i<n; ++i) {
if (pre[i] == i)
++cnt;
if (cnt > )
return false;
}
if (!cnt)
return false;
cnt = ;
for (i=; i<n; ++i)
if (deg[i] & )
++cnt;
if (cnt== || cnt==)
return true;
else
return false;
} int main() {
char a[MAXL], b[MAXL];
int i, k; memset(deg, , sizeof(deg));
for (i=; i<MAXN; ++i)
pre[i] = i; while (scanf("%s %s", a, b) != EOF) {
k = Tfind(a);
if (k)
deg[k]++;
else {
create(a, n);
k = n;
deg[n++] = ;
}
i = Tfind(b);
if (i)
deg[i]++;
else {
create(b, n);
i = n;
deg[n++] = ;
}
merge(k, i);
} if (judge())
printf("Possible\n");
else
printf("Impossible\n"); return ;
}
【POJ】2513 Colored Sticks的更多相关文章
- 【POJ】2653 Pick-up sticks(计算几何基础+暴力)
http://poj.org/problem?id=2653 我很好奇为什么这样$O(n^2)$的暴力能过.... 虽然说这是加了链表优化的,但是最坏不也是$O(n^2)$吗...(只能说数据太弱.. ...
- [欧拉] poj 2513 Colored Sticks
主题链接: http://poj.org/problem? id=2513 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Tota ...
- poj 2513 Colored Sticks(欧拉路径+并检查集合+特里)
题目链接:poj 2513 Colored Sticks 题目大意:有N个木棍,每根木棍两端被涂上颜色.如今给定每一个木棍两端的颜色.不同木棍之间拼接须要颜色同样的 端才干够.问最后是否能将N个木棍拼 ...
- 【POJ】1704 Georgia and Bob(Staircase Nim)
Description Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, ...
- 【POJ】1067 取石子游戏(博弈论)
Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...
- 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 (欧拉回路 + 字典树 +并查集)
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 ...
随机推荐
- xcode升级或者重新安装后不能编译的解决方法
昨天由于xcode有一些问题,因此进行了重新安装,结果安装好后进行编译,没有进行任何改动的代码出现了两个fatal error 查看错误信息为什么的header has allready build, ...
- eclipse-SDK-3.7-win32;eclipse-java-indigo-win32;eclipse-jee-indigo-win32 区别(ZZ)
eclipse-SDK-3.7-win32:eclipse-java-indigo-win32:eclipse-jee-indigo-win32 三个都是用于win32,即windows系统的32位机 ...
- iOS UIKit:viewController之Segues (4)
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- 解决mybatis使用枚举的转换
解决mybatis使用枚举的转换 >>>>>>>>>>>>>>>>>>>>> ...
- python s12 day3
python s12 day3 深浅拷贝 对于 数字 和 字符串 而言,赋值.浅拷贝和深拷贝无意义,因为其永远指向同一个内存地址. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...
- 微软企业库Microsoft Enterprise Library的相关文章链接
微软企业库4.1学习笔记 http://blog.csdn.net/anyqu/article/category/1228691/3 黄聪:Enterprise Library 5.0 系列教程 ww ...
- A除以B_2
本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数.你需要输出商数Q和余数R,使得A = B * Q + R成立. 输入格式: 输入在1行中依次给出A和B,中间以1空格分隔. 输出格 ...
- power desinger 学习笔记<八>
转-PowerDesigner 把Comment复制到name中和把name复制到Comment 在使用PowerDesigner对数据库进行概念模型和物理模型设计时,一般在NAME或Comment中 ...
- JavaScript HTML DOM - 改变 HTML
JavaScript HTML DOM - 改变 HTML HTML DOM 允许 JavaScript 改变 HTML 元素的内容. 改变 HTML 输出流 JavaScript 能够创建动态的 H ...
- 移动触摸事件(touchstart、touchmove和touchend)
touchstart事件:当手指触摸屏幕时候触发,即使已经有一个手指放在屏幕上也会触发. touchmove事件:当手指在屏幕上滑动的时候连续地触发.在这个事件发生期间,调用preventDefaul ...