poj 2513 Colored Sticks(欧拉路径+并检查集合+特里)
题目大意:有N个木棍,每根木棍两端被涂上颜色。如今给定每一个木棍两端的颜色。不同木棍之间拼接须要颜色同样的
端才干够。问最后是否能将N个木棍拼接在一起。
解题思路:欧拉通路+并查集+字典树。
欧拉通路,每一个节点的统计度,度为奇数的点不能超过2个。并查集,推断节点
是否全然联通。
字典树,映射颜色。
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 5000005;
const int sigma_size = 26;
typedef pair<int,int> pii;
int N, E, far[maxn], cnt[maxn];
pii ed[maxn];
struct Tire {
int sz, g[maxn][sigma_size];
int val[maxn];
void init();
int idx(char ch);
int insert(char* s);
}T;
inline int getfar(int x) {
return x == far[x] ? x : far[x] = getfar(far[x]);
}
bool judge() {
int ret = 0;
for (int i = 1; i <= N; i++) {
if (cnt[i]&1)
ret++;
}
if (ret > 2)
return false;
for (int i = 0; i < E; i++) {
int p = getfar(ed[i].first);
int q = getfar(ed[i].second);
if (p != q) {
N--;
far[p] = q;
}
}
return N <= 1;
}
int main () {
T.init();
N = E = 0;
char a[11], b[11];
while (scanf("%s%s", a, b) == 2) {
int p = T.insert(a);
int q = T.insert(b);
cnt[p]++, cnt[q]++;
ed[E].first = p;
ed[E].second = q;
E++;
}
printf("%s\n", judge() ? "Possible" : "Impossible");
return 0;
}
void Tire::init() {
sz = 1;
val[0] = 0;
memset(g[0], 0, sizeof(g[0]));
}
int Tire::idx(char ch) {
return ch - 'a';
}
int Tire::insert(char* s) {
int u = 0, n = strlen(s);
for (int i = 0; i < n; i++) {
int v = idx(s[i]);
if (g[u][v] == 0) {
g[u][v] = sz;
memset(g[sz], 0, sizeof(g[sz]));
val[sz++] = 0;
}
u = g[u][v];
}
if (val[u] == 0) {
val[u] = ++N;
far[N] = N;
cnt[N] = 0;
}
return val[u];
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
poj 2513 Colored Sticks(欧拉路径+并检查集合+特里)的更多相关文章
- [欧拉] poj 2513 Colored Sticks
主题链接: http://poj.org/problem? id=2513 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Tota ...
- 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 (trie 树)
链接:poj 2513 题意:给定一些木棒.木棒两端都涂上颜色,不同木棒相接的一边必须是 同样的颜色.求能否将木棒首尾相接.连成一条直线. 分析:能够用欧拉路的思想来解,将木棒的每一端都看成一个结点 ...
- 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 ...
- POJ 2513 - Colored Sticks - [欧拉路][图的连通性][字典树]
题目链接: http://poj.org/problem?id=2513 http://bailian.openjudge.cn/practice/2513?lang=en_US Time Limit ...
- POJ 2513 Colored Sticks (欧拉回路+并查集+字典树)
题目链接 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with ...
- poj 2513 Colored Sticks( 字典树哈希+ 欧拉回路 + 并查集)
题目:http://poj.org/problem?id=2513 参考博客:http://blog.csdn.net/lyy289065406/article/details/6647445 htt ...
随机推荐
- ZOJ 2412 Farm Irrigation(DFS 条件通讯块)
意甲冠军 两个农田管内可直接连接到壳体 他们将能够共享一个水源 有11种农田 管道的位置高于一定 一个农田矩阵 问至少须要多少水源 DFS的连通块问题 两个相邻农田的管道能够直接连接的 ...
- Centos 6.5下一个SNMP简单配置(snmp protocol v3,监控宝)
Centos 6.5下一个SNMP简单配置(snmp protocol v3.监控宝) jom_ch@2014/7/25 1,安装 >yum -y install net-snmp net-sn ...
- atcoder它A Mountaineer
Time limit : 2sec / Stack limit : 256MB / Memory limit : 256MB Problem Dave is a mountaineer. He is ...
- JSP简单的练习-功能标签
<!-- userfn.jsp --> <%@ page contentType="text/html;charset=gb2312" %> <%@ ...
- Maven构建Hadoop
Maven构建Hadoop工程 阅读目录 序 Maven 安装 构建 示例下载 系列索引 序 上一篇,我们编写了第一个MapReduce,并且成功的运行了Job,Hadoop1.x是通过ant来管理工 ...
- hdu 4965 Fast Matrix Calculation(矩阵高速幂)
题目链接.hdu 4965 Fast Matrix Calculation 题目大意:给定两个矩阵A,B,分别为N*K和K*N. 矩阵C = A*B 矩阵M=CN∗N 将矩阵M中的全部元素取模6,得到 ...
- 【SSH2(理论+实践)】--Hibernate步步(一个)
前几个博客讨论SSH2该框架Struts,它代表层,集成封装.和使用WebWork作为核心处理,依赖映射是它的处理核心.在使用时需要Struts.xml配置相应Action和Interceptor够完 ...
- PC结束 Spark 二次开发 收到自己主动,并允许好友请求
本次Spark二次开发是为了客服模块的开发, 能让用户一旦点击该客服则直接自己主动加入好友.而客服放则需自己主动加入好友,不同弹出对话框进行允许,这方便的广大客服. 如今废话不多说,直接上代码. pa ...
- mariadb 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@localhost /]# systemctl stop mariadb.service[root@localhost /]# mysqld_safe --user=mysql --ski ...
- 正则表达式 \b (转)
引用网上一段话: \b 是正则表达式规定的一个特殊代码(好吧,某些人叫它元字符,metacharacter),代表着单词的开头或结尾,也就是单词的分界处.虽然通常英文的单词是由空格,标点符号或者换行来 ...