【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 ...
随机推荐
- INERT DELEYED、INSERT IGNORE replace into和insert区别
insert into表示插入数据,数据库会检查主键,如果出现重复会报错:replace into表示插入替换数据,需求表中有PrimaryKey,或者unique索引,如果数据库已经存在数据 ...
- iOS-iPad开发之popoverController使用介绍
iOS-iPad开发之popoverController使用介绍 iOS开发UI篇-popoverController使用注意 iOS SDK:自定义Popover(弹出窗口) 实现的简单例子: // ...
- SA密钥长度、明文长度和密文长度
本文介绍RSA加解密中必须考虑到的密钥长度.明文长度和密文长度问题,对第一次接触RSA的开发人员来说,RSA算是比较复杂的算法,RSA的复杂度是因为数学家把效率和安全也考虑进去的缘故. 本文先只谈密钥 ...
- java的继承机制
这次我想深入探究以下java里类的继承机制. 我们知道,继承是java设计里的一个失败的地方.高司令说过:如果让他重新设计java的话,他会把继承去掉.而java里继承到底怎么了,会这么不 ...
- Map的迭代操作
Map的迭代操作 public static void main(String[] args) { Map<String, List<Integer>> map = new H ...
- 项目中常用SQL语句总结
1.项目中常常需要修改字段长度,但需要保留数据--增加业务受理 项目名称 字段长度alter table t_ywsl add aa varchar2(200);update t_ywsl set a ...
- iOS9 application:application openURL: sourceApplication: annotation: 方法不执行
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url NS_DEPRECATED_IOS(2_0, 9 ...
- cocos2d中如何使用图片纹理图集的加载来实现一个动画的功能
cocos2d中要实现一个动画,一般采用纹理图集的方式,也就是说把几个连续动作的图片挨个显示切换这样就是动画 一: 首先先看下今天要实现的具体的目的,打飞机的时间屏幕上会有一个喷火的小飞机,飞机的尾部 ...
- css 不确定元素宽度的水平居中
对于一个不确定宽度的元素居中,我们想到使用的方法是 text-align:center; 或者 margin:0 auto; text-align只对行内元素有效,对于块元素我们要用margin,块元 ...
- How far away ?
#include<iostream> #include <algorithm> using namespace std; const int M=40010; int dis[ ...