POJ-2153Colored Sticks解题报告+欧拉回路,字典树,并查集;
传送门:http://poj.org/problem?id=2513
题意:给你许多木棍,木棍两端都有颜色,问能不能首尾相接,要求颜色相同。
参考:https://www.cnblogs.com/kuangbin/archive/2012/08/07/2626223.html
思路:
由图论知识可以知道,无向图存在欧拉路的充要条件为:
① 图是连通的;
② 所有节点的度为偶数,或者有且只有两个度为奇数的节点。
图的连通可以利用并查集去判断。
度数的统计比较容易。
数据比较大,首先需要将颜色的字符串和数字一一对应起来。
虽然可以用map,但是map效率不高,肯定会超时的。
最好的办法的是建立字典树。
将字符串和数字一一对应起来。
注意本题的数据有没有木棒的情况,要输出Possible。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iterator> using namespace std; #define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back #define Pll pair<ll,ll>
#define Pii pair<int,int> #define fi first
#define se second #define OKC ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
typedef long long ll;
typedef unsigned long long ull; /*-----------------show time----------------*/ const int tm = ;
int col = ;
struct trie{
trie* nxt[tm];
int last;
trie()
{
memset(nxt,,sizeof(nxt));
last = ;
}
}*root;
int insert(char *s)
{
trie * p = root;
for(int i=;s[i];i++)
{
if(!p->nxt[s[i] - 'a'])
p->nxt[s[i]-'a'] = new trie;
p = p->nxt[s[i]-'a'];
}
if(p->last)
return p->last;
else
{
col++;
return p->last = col;
}
}
const int maxn = 5e5+;
int deg[maxn];
int fa[maxn];
void init(){
for(int i=; i<maxn; i++)
fa[i] = i;
root = new trie;
}
int f(int x)
{
if(fa[x]==x)return x;
else return fa[x] = f(fa[x]);
}
void uni(int x,int y)
{
int px = f(x);
int py = f(y);
if(px==py)return ;
fa[px] = py;
}
int main(){
char s1[],s2[];
init();
while(~scanf("%s%s",s1,s2))
{
// if(s1[0]==s2[0])break;
int x = insert(s1),y = insert(s2);
uni(x,y);
deg[x]++;
deg[y]++;
}
int cnt1 = ,cnt2 = ;
for(int i=; i<=col; i++)
{
if(fa[i]==i)cnt1++;
if(deg[i]%==)cnt2++;
}
if((cnt1==||cnt1==)&&(cnt2==||cnt2==))
{
puts("Possible");
}
else puts("Impossible");
return ;
}
POJ-2153Colored Sticks解题报告+欧拉回路,字典树,并查集;的更多相关文章
- POJ 2513 Colored Sticks (欧拉回路 + 字典树 +并查集)
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27097 Accepted: 7175 ...
- [poj 2912] Rochambeau 解题报告 (带权并查集)
题目链接:http://poj.org/problem?id=2912 题目: 题目大意: n个人进行m轮剪刀石头布游戏(0<n<=500,0<=m<=2000) 接下来m行形 ...
- poj2513 Colored Sticks —— 字典树 + 并查集 + 欧拉回路
题目链接:http://poj.org/problem?id=2513 题解:通过这题了解了字典树.用字典树存储颜色,并给颜色编上序号.这题为典型的欧拉回路问题:将每种颜色当成一个点.首先通过并查集判 ...
- POJ 2513 Colored Sticks(欧拉道路+字典树+并查集)
http://poj.org/problem?id=2513 题意: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 思路: 题目很明 ...
- POJ2513Colored Sticks(欧拉通路)(字典树)(并查集)
Colored Sticks Time Limit: 5000MS Memory ...
- POJ 2513 无向欧拉通路+字典树+并查集
题目大意: 有一堆头尾均有颜色的木条,要让它们拼接在一起,拼接处颜色要保证相同,问是否能够实现 这道题我一开始利用map<string,int>来对颜色进行赋值,好进行后面的并查操作以及欧 ...
- POJ 2513 字典树+并查集+欧拉路径
Description: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 解题思路: 可以用图论中欧拉路的知识来解这道题,首先可以把木 ...
- POJ 2503 Babelfish(map,字典树,快排+二分,hash)
题意:先构造一个词典,然后输入外文单词,输出相应的英语单词. 这道题有4种方法可以做: 1.map 2.字典树 3.快排+二分 4.hash表 参考博客:[解题报告]POJ_2503 字典树,MAP ...
- POJ 1984 Navigation Nightmare 【经典带权并查集】
任意门:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K To ...
随机推荐
- CentOS7 部署K8S集群
虚拟机: VMware® Workstation 12 Pro 12.5.9 build-7535481操作系统:CentOS Linux release 7.6.1810 (Core) 部署 ...
- web安全脑图
- 【iOS】UIAlertController 弹出框
UIAlertView 虽然还能用,但已经废弃了.因此以后尽量用 UIAlertController.示例代码如下: UIAlertController *alert = [UIAlertContro ...
- Selenium+java - 下拉框处理
常见下拉框也分两种:一种是标准控件和非标准控件(一般为前端开发人员自己封装的下拉框),本篇文章中将重点讲解标准下拉框操作. 1.Select提供了三种选择某一项的方法 select.selectByI ...
- bit、byte、kb、mb、g的区别
1Byte=8bit1KB=1024Byte(字节)=8*1024bit1MB=1024KB1GB=1024MB1TB=1024GB bit是计算机数据的最小单元.要么是0,要么是1. byte 关键 ...
- Asp.Net Core 发布到 Docker(Linux Centos 虚拟机,使用Dockerfile)
实践一下 Asp.Net Core (基于.net core 2.2)部署到Docker 一.准备工作: 1. 使用Virtualbox创建一个Centos系统的虚拟机,并安装docker和vim 2 ...
- MariaDB 修改存储路径后启动失败问题解决
修改 MariaDB 路径到 home 路径下, 执行 systemctl start mariadb 启动MariaDB 时,报错提示: Job for mariadb.service failed ...
- 使用富文本编辑器Kindeditor
今天在做需求的时候,遇到有一个字段,需要保存带有格式的内容,决定使用富文本框编辑器Kindeditor来实现,解决方法如下: 登录官网下载控件包: http://kindeditor.net/down ...
- bootstrape select使用小结
看看上面的效果是bootstrape使用的效果.虽然不是很好看,但是符合bootstrape的风格.来看看普通的select的样式 bootstrape下的select和普通select在bootst ...
- 百度Echarts,蚂蚁金服G2,D3三种主流可视化工具对比
1.百度的Echarts 官网:https://echarts.baidu.com/ 介绍:ECharts,缩写来自Enterprise Charts,是百度推出的一款开源的,商业级数据图表,它最初是 ...