字典树+并查集。

 #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的更多相关文章

  1. 【POJ】2653 Pick-up sticks(计算几何基础+暴力)

    http://poj.org/problem?id=2653 我很好奇为什么这样$O(n^2)$的暴力能过.... 虽然说这是加了链表优化的,但是最坏不也是$O(n^2)$吗...(只能说数据太弱.. ...

  2. [欧拉] poj 2513 Colored Sticks

    主题链接: http://poj.org/problem? id=2513 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Tota ...

  3. poj 2513 Colored Sticks(欧拉路径+并检查集合+特里)

    题目链接:poj 2513 Colored Sticks 题目大意:有N个木棍,每根木棍两端被涂上颜色.如今给定每一个木棍两端的颜色.不同木棍之间拼接须要颜色同样的 端才干够.问最后是否能将N个木棍拼 ...

  4. 【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, ...

  5. 【POJ】1067 取石子游戏(博弈论)

    Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...

  6. poj 2513 Colored Sticks trie树+欧拉图+并查集

    点击打开链接 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27955   Accepted ...

  7. POJ 2513 Colored Sticks

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 28036   Accepted: 7428 ...

  8. POJ 2513 Colored Sticks (欧拉回路 + 字典树 +并查集)

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27097   Accepted: 7175 ...

  9. poj 2513 Colored Sticks (trie树+并查集+欧拉路)

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 40043   Accepted: 10406 ...

随机推荐

  1. eclipse删除空行

    1.打开源码编辑器 2.使用快捷键Ctrl+f 3.在Find输入框中输入:^\s*\n 4.Replace With输入框的值为空 5.在[Options]选中的"Regular expr ...

  2. Mac OS命令行运行Sublime Text

    Opening Sublime Text on command line as subl on Mac OS? Mac OS subl http://www.phodal.com/blog/mac-o ...

  3. order by 自定义排序

    使用order by排序,有时候不是根据字符或数字顺序,而是根据实际要求排序. 例如有客户A,B,C,我希望排序结果是B,C,A,那么就要通过自定义的规则排序. 第一种方法,可以构造一张映射表,将客户 ...

  4. CakePHP之Model

    模型 模型在应用程序中是作为业务层而存在的(怎么感觉是数据层......).这就意味着,模型应当负责管理几乎所有涉及数据的事情,其合法性,以及你的业务领域中数据在工作流程中的演化和互动 . 通常模型类 ...

  5. 把nc v6的源码看懂

    看懂nc v6的源码! 碧桂园全部的正式环境的补丁都在我手里. 2015-11-18 2:33 谢谢两位一起努力的兄弟 谢谢超哥,谢谢祈冰哥,谢谢连老师,陈明大哥,谢谢龙哥,珍玉,谢谢廖生哥,谢谢林春 ...

  6. Android端上传图片到后台,存储到数据库中 详细代码

    首先点击头像弹出popwindow,点击相册,相机,调用手机自带的裁剪功能,然后异步任务类访问服务器,上传头像,保存到数据库中, 下面写出popwindow的代码 //设置popwindow publ ...

  7. asp.net 实现对xml文件的 读取,添加,删除,修改

    用于修改站内xml文件 已知有一个XML文件(bookstore.xml)如下:<?xml version="1.0" encoding="gb2312" ...

  8. CentOS 7重装mysql编译过程报错解决方法

    错误记录: [ 82%] Building C object libmysql/CMakeFiles/clientlib.dir/__/sql-common/client.c.o/usr/local/ ...

  9. Objective-C消息机制的原理

    http://desheng.me/2012/03/31/objective-c%E6%B6%88%E6%81%AF%E6%9C%BA%E5%88%B6%E7%9A%84%E5%8E%9F%E7%90 ...

  10. hdoj 2049 错排

    代码: #include <stdio.h> int main(){ int n,a,b,i,j; __int64 s[22],h[22]; s[1]=0; s[2]=1; s[3]=2; ...