【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 ...
随机推荐
- git 工具
https://www.kernel.org/pub/software/scm/git/ wget https://www.kernel.org/pub/software/scm/git/git-2. ...
- GDB踪函数的完整调用过程 及原理
http://www.lenky.info/archives/2013/02/2202 Breakpoint , .so. (gdb) bt # .so. # .so. # .so. # .so. # ...
- Java基础知识强化之集合框架笔记19:List集合迭代器使用之 并发修改异常的产生原因 以及 解决方案
1. 我有一个集合,如下,请问,我想判断里面有没有"world"这个元素,如果有,我就添加一个"javaee"元素,请写代码实现. ConcurrentModi ...
- codevs 3119 高精度练习之大整数开根 (各种高精+压位)
/* codevs 3119 高精度练习之大整数开根 (各种高精+压位) 二分答案 然后高精判重 打了一个多小时..... 最后还超时了...压位就好了 测试点#1.in 结果:AC 内存使用量: 2 ...
- 每次打开VS2010都会报“ devenv.exe -Assert Failure”这个错误
把.net framework4.5中文包卸载掉,, 如果还不行就把.net framework4.5也卸载掉,,然后到微软官网下载net framework4.5安装包安装,安装完后把中文包卸载掉就 ...
- Java-Hirbernate小结大纲
Hibernate Hibernate是一个开放源代码的对象关系映射框架 Hibernate的核心接口一共有6个,分别为:Session.SessionFactory.Transaction.Quer ...
- List指定字段赋特定值(非循环) asp.net
List<Cart> cartd=cartd.Where(p => (p.Id= "123").Length > -1).ToList(); 把Id的值都赋 ...
- 导出你的GAC Assembly中的DLLS
方法1: CMD命令中,进入C:\windows\assembly,然后XCOPY GAC_MSIL c:\temp /E 这样就得到了dlls了,以命名空间来分类. 如果想将dlls从集合中分出来, ...
- Linux下安装Nginx1.9.3-0303(本人亲手实践)
Linux下安装Nginx1.9.3 Linux操作系统 Oel 5.8 64bit 最新版Nginx: 1.9.3 最近同事让我帮忙搞 ngix,两天时间 安装.配置搞定了.继续 Nginx 1.9 ...
- 通常我们使用[NSDate date]方法得到的时间与当前时间不一致,如何解决?
NSDate *date = [NSDate date]; NSTimeZone *zone = [NSTimeZone systemTimeZone]; NSInteger interv ...