Colored Sticks (并查集+Trie + 欧拉路)
| Time Limit: 5000MS | Memory Limit: 128000K | |
| Total Submissions: 37340 | Accepted: 9796 |
Description
Input
Output
Sample Input
blue red
red violet
cyan blue
blue magenta
magenta cyan
Sample Output
Possible
Hint
Source
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 500008
#define INF 1000000009
#define eps 0.00000001
/*
欧拉路 一笔画问题!
Trie树就是一种节省空间的hash
利用并查集判断是否联通
*/
char a[], b[];
int cnt = , index = , pre[MAXN], degree[MAXN];
typedef struct node
{
bool flag;
int id;
struct node* next[];
}*Tree;
node memory[MAXN];
Tree Newnode()
{
Tree T = &memory[cnt++];
T->flag = false;
T->id = -;
for (int i = ; i < ; i++)
T->next[i] = NULL;
return T;
}
int Insert(char* s, Tree T)
{
int p = ;
while (s[p] != '\0')
{
int k = s[p] - 'a';
if (!T->next[k])
T->next[k] = Newnode();
T = T->next[k];
p++;
}
if (T->flag)
return T->id;
else
{
T->flag = true;
T->id = index++;
return T->id;
}
}
int find(int x)
{
if (pre[x] == -)
return x;
else
return pre[x] = find(pre[x]);
}
void mix(int a, int b)
{
int fa = find(a), fb = find(b);
if (fa != fb)
{
pre[fa] = fb;
}
}
int main()
{
memset(pre, -, sizeof(pre));
memset(degree, , sizeof(degree));
Tree T = Newnode();
while (scanf("%s %s", a, b) != EOF)
{
//if (a[0] == '#') break;
int ia = Insert(a, T), ib = Insert(b, T);
//cout << ia << ' ' << ib << endl;
degree[ia]++;
degree[ib]++;
mix(ia, ib);
}
int s = find(), tmp = ;
for (int i = ; i < index; i++)
{
if (find(i) != s) //存在多个祖先,图为森林,不连通
{
printf("Impossible\n");
return ;
}
if (degree[i] % )
tmp++;
}
if(tmp== || tmp==)
printf("Possible\n");
else
printf("Impossible\n");
}
Colored Sticks (并查集+Trie + 欧拉路)的更多相关文章
- PKU 2513 Colored Sticks(并查集+Trie树+欧拉路径(回路))
题目大意: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相连接的一端必须是同颜色的. 解题思路: 可以用图论中欧拉路的知识来解这道题,首先可以把木棒两端看成节点 ...
- Colored Sticks POJ - 2513(trie树欧拉路)
题意: 就是无向图欧拉路 解析: 不能用map..超时 在判断是否只有一个联通的时候,我比较喜欢用set,但也不能用set,会超时,反正不能用stl emm 用trie树来编号就好了 #include ...
- FZU 2112 并查集、欧拉通路
原题:http://acm.fzu.edu.cn/problem.php?pid=2112 首先是,票上没有提到的点是不需要去的. 然后我们先考虑这个图有几个连通分量,我们可以用一个并查集来维护,假设 ...
- POJ 2513 Colored Sticks 字典树、并查集、欧拉通路
Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some ...
- loj6198谢特 后缀数组+并查集+Trie
先把问题放在后缀数组上考虑 已知两个数组a b,求min(a[i],...,a[j])+(b[i]^b[j])的最大值 套路题 初始每个点都是一个小连通块 把a按从大到小的顺序加入,计算当前加入边作为 ...
- poj 2513 Colored Sticks (trie树+并查集+欧拉路)
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 40043 Accepted: 10406 ...
- Colored Sticks (字典树哈希+并查集+欧拉路)
Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27704 Accepted: 7336 Description You ...
- POJ2513——Colored Sticks(Trie树+欧拉回路+并查集)
Colored Sticks DescriptionYou are given a bunch of wooden sticks. Each endpoint of each stick is col ...
- poj 2513 Colored Sticks trie树+欧拉图+并查集
点击打开链接 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27955 Accepted ...
随机推荐
- Nginx的alias与root的用法区别和location匹配规则
1.alias与root的用法区别 最基本的区别:alias指定的目录是准确的,root是指定目录的上级目录,并且该上级目录要含有location指定名称的同名目录. location /abc/ { ...
- [Swift通天遁地]三、手势与图表-(4)3DTouch功能在项目中的应用
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- Django day26 HyperlinkedIdentityField,序列化组件的数据校验以及功能的(全局,局部)钩子函数,序列化组件的反序列化和保存
一:HyperlinkedIdentityField(用的很少):传三个参数:第一个路由名字,用来反向解析,第二个参数是要反向解析的参数值,第三个参数:有名分组的名字 -1 publish = ser ...
- jQuery里$.post请求,后台返回结果为“json”格式,前台解析错误问题记录
在JSP页面使用$.post请求后台返回json数据时,在最后 必须加上返回数据格式为json的才行.不然JSP页面解析会出错.
- Spark 概念学习系列之Spark基本概念和模型(十八)
打好基础,别小瞧它! spark的运行模式多种多样,在单机上既可以本地模式运行,也可以伪分布模式运行.而当以分布式的方式在集群中运行时.底层的资源调度可以使用Mesos或者Yarn,也可使用spark ...
- B树、B+树、红黑树、AVL树比较
B树是为了提高磁盘或外部存储设备查找效率而产生的一种多路平衡查找树. B+树为B树的变形结构,用于大多数数据库或文件系统的存储而设计. B树相对于红黑树的区别 在大规模数据存储的时候,红黑树往往出现由 ...
- 笔记《精通css》第3章 盒模型,定位,浮动,清理
第3章 盒模型,定位,浮动,清理 1.盒模型用到的属性width,height,padding,border,margin 普通文档流的上下垂直margin会叠加 2.块级框 与 行内框, 利用 ...
- c++枚举变量初始值
#include <iostream> // std::cout, std::boolalpha, std::noboolalpha enum foo { c = -1, a = 1, b ...
- dubbo之主机绑定
主机绑定 查找顺序 缺省主机 IP 查找顺序: 通过 LocalHost.getLocalHost() 获取本机地址. 如果是 127.* 等 loopback 地址,则扫描各网卡,获取网卡 IP. ...
- 使用码云gitee.com托管代码
1.新建项目 可以看到团队资源管理器是这样的,已经在本地有存储库,所有更改可以保存本地 2.在码云上新建项目 项目名称必填,其它项根据情况填写 3.复制项目地址关联到本地存储库 填写码云的项目地址,发 ...