POJ2513 Colored Sticks(Trie+欧拉回路)
Description
Input
Output
Sample Input
blue red
red violet
cyan blue
blue magenta
magenta cyan
Sample Output
Possible
题意:给你n(n<=250000)根小木棒,木棒两端染有一些颜色,两根木棒相连需要保证相连端点颜色一致.求这些木棒是否可以全部相连?
题解:可以将木棒理解为边,连接两种颜色,那么全部相连就变成了一笔画的问题,即判断欧拉回路.
欧拉回路需要满足两个条件:
1.度数为奇数的点为0个或2个
2.图联通
现在的问题是如何把字符串变成数字的点,用map是肯定会TLE的
hash应该可以,但tire好写,所以我选择了trie树,存入trie的值为该字符串的映射
图联通用并查集去判断.
代码如下:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; int cnt=,du[],fa[]; struct Trie
{
int sz,tr[][];
int time[]; void clear()
{
sz=;
memset(tr,,sizeof(tr));
} void insert(char* c,int x)
{
int rt=,len=strlen(c);
for(int i=;i<len;i++)
{
if(!tr[rt][c[i]-'a'])
{
tr[rt][c[i]-'a']=++sz;
}
rt=tr[rt][c[i]-'a'];
}
time[rt]=x;
} int search_t(char *c)
{
int rt=,len=strlen(c);
for(int i=;i<len;i++)
{
if(!tr[rt][c[i]-'a'])
{
return ;
}
rt=tr[rt][c[i]-'a'];
}
return time[rt];
} }trie; void mem(int n)
{
for(int i=;i<=n;i++)
{
fa[i]=i;
}
} int find(int x)
{
if(x!=fa[x])
{
fa[x]=find(fa[x]);
}
return fa[x];
} void union_(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
{
fa[fx]=fy;
}
} int euler()
{
int sum=,t=find();
for(int i=;i<=cnt;i++)
{
if(du[i]%==)
{
sum++;
}
}
if(sum!=&&sum!=)
{
return ;
}
for(int i=;i<=cnt;i++)
{
if(find(i)!=find(t))
{
return ;
}
}
return ;
} int main()
{
char s1[],s2[];
mem();
// freopen("1.in","r",stdin);
// freopen("1.out","w",stdout);
while(scanf("%s %s\n",s1,s2)!=EOF)
{
int from,to;
if(!trie.search_t(s1))
{
trie.insert(s1,++cnt);
}
from=trie.search_t(s1);
du[from]++;
if(!trie.search_t(s2))
{
trie.insert(s2,++cnt);
}
to=trie.search_t(s2);
du[to]++;
union_(from,to);
}
if(euler())
{
puts("Possible");
}
else
{
puts("Impossible");
}
}
POJ2513 Colored Sticks(Trie+欧拉回路)的更多相关文章
- 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 ...
- 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 ...
- poj2513 Colored Sticks —— 字典树 + 并查集 + 欧拉回路
题目链接:http://poj.org/problem?id=2513 题解:通过这题了解了字典树.用字典树存储颜色,并给颜色编上序号.这题为典型的欧拉回路问题:将每种颜色当成一个点.首先通过并查集判 ...
- POJ 2513 Colored Sticks (欧拉回路+并查集+字典树)
题目链接 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with ...
- POJ2513:Colored Sticks(字典树+欧拉路径+并查集)
http://poj.org/problem?id=2513 Description You are given a bunch of wooden sticks. Each endpoint of ...
- POJ 2513 Colored Sticks(欧拉回路,字典树,并查集)
题意:给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 无向图存在欧拉路的充要条件为: ① 图是连通的: ② 所有节 ...
- poj 2513 Colored Sticks (trie 树)
链接:poj 2513 题意:给定一些木棒.木棒两端都涂上颜色,不同木棒相接的一边必须是 同样的颜色.求能否将木棒首尾相接.连成一条直线. 分析:能够用欧拉路的思想来解,将木棒的每一端都看成一个结点 ...
随机推荐
- laravel 中间件学习
http://blog.csdn.net/kwinh/article/details/56285204 http://blog.csdn.net/fationyyk/article/details/5 ...
- sqlbulkcopy 使用DataTable作为数据源的数据类型问题--来自数据源的String类型的给定值不能转换为指定目标列的类型 uniqueidentifier
今天做批量插入的时候,SQLSERVER总是报错,错误提示“来自数据源的String类型的给定值不能转换为指定目标列的类型 uniqueidentifier”. 首先核对了一下定义的dataTable ...
- git的操作
学习源头: https://www.cnblogs.com/yaoxc/p/3946280.html https://www.cnblogs.com/jeremylee/p/5715289.html ...
- Java中判断非空对象.
Java中经常会遇到判断非空的时候. 有的时候判断了非空但是还是报空指针,为什么.? 判断的时候一般都会判断两次.类似于: Org o = new Org(); if ( o.getId()!=nul ...
- unittest框架+ HTMLTestRunner 出报告时,展示控制台信息 不同展示的参数写法 加verbosity
加verbosity参数 没有加的时候展示: 参考: 来源: https://www.cnblogs.com/tomweng/p/6609937.html 介绍: HTMLTestRunner 是 ...
- MongoDB 4.X搭建
一.MongoDB4.X搭建 1.下载mongdb安装包,在官网上找到对应的版本,我的是centos7 找到上面的连接,通过命令行: 2.将下载的mongodb-linux-x86_64-4.0.0. ...
- python3+ros api
官方文档:https://wiki.mikrotik.com/wiki/Manual:API_Python3 # !/usr/bin/env python# -*- coding:utf-8 -*-# ...
- [置顶]
STM32的ADC1采集多条通道,可以不使用DMA功能吗?
类似的问题 为什么我采集5条通道的电压,而采集到的值却都是第一条的呢? 我什么时候需要使用DMA功能? Ⅰ关于ADC的一些知识 STM32的ADC是一种12位逐次逼近型的模拟数字转换器.它有多达18条 ...
- 2014.8.27 CAD数据结构
Rwy表中存放所有物理跑道,主键rwy_id,但没有跑道中心点坐标 rwy_direction表中存放所有所有逻辑跑道号,也没有跑道入口坐标.同一rwy_id对应有2条记录 rwy_cline_poi ...
- C# IP地址去掉端口号
string Ip1 = "192.168.0.199:7777"; string Ip2 = Ip1.Remove(Ip1.IndexOf(':'));