【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 ...
随机推荐
- 9.29noip模拟试题
环上的游戏(cycle) 有一个取数的游戏.初始时,给出一个环,环上的每条边上都有一个非负整数.这些整数中至少有一个0.然后,将一枚硬币放在环上的一个节点上.两个玩家就是以这个放硬币的节点为起点开始这 ...
- ASP.NET MVC 中 ActionResult 和 ViewResult 在使用上的区别
如果确认你返回的是一个视图(view),你可以直接返回类型为ViewResult. 如果你并不是很清楚,或者你根本不想去理解这些东西,你可以直接返回ActionResult
- Android 开发笔记——通过 Intent 传递类对象
Android中Intent传递类对象提供了两种方式一种是 通过实现Serializable接口传递对象,一种是通过实现Parcelable接口传递对象. 要求被传递的对象必须实现上述2种接口中的一种 ...
- Missing artifact com.sun:tools:jar:1.5.0
http://java-suddy.iteye.com/blog/1821871(解决了我的问题)
- 玩转CSLA.NET小技巧系列二:使用WCF无法上传附件,提示413 Entity Too Large
背景:由于系统需要展示图片,客户上传图片到本地客户端目录,然后在数据库中存储本地图片地址,和图片二进制数据 错误原因:我是使用CSLA的WCF服务,使用了数据门户,WCF协议使用的是wsHttpBin ...
- 【转】iOS开发6:UIActionSheet与UIAlertView
原文: http://my.oschina.net/plumsoft/blog/42763 iOS程序中的Action Sheet就像Windows中的 “确定-取消”对话框一样,用于强制用户进行选择 ...
- ASIHttpRequest网络请求第三方类库使用方法详解
一. 第一步首先你要从网络上下载ASIHttpRequestlib, 下载以后解压,增加到你的工程里面, 这个时间检查工程内部是否已经导入成功,文件结构如下: ASIHTTPRequestConfig ...
- php利用smtp类轻松的发送电子邮件
当你还在纠结php内置的mail()函数不能发送邮件时,那么你现在很幸运,此时的这篇文章可以帮助到你! php利用smtp类来发邮件真是屡试不爽,我用过很久了,基本上没出过问题.本博客后台,当博主回复 ...
- Builder 模式
Builder 模式和 AbstractFactory 模式在功能上很相似,因为都是用来创建大的复杂的对象,它们的区别是:Builder 模式强调的是一步步创建对象,并通过相同的创建过程可以获得不同的 ...
- 【POJ2266】【树状数组+离散化】Ultra-QuickSort
Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...