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 ...
随机推荐
- Python基础总结之第九天开始【python之OS模块对目录的操作、以及操作文件】(新手可相互督促)
年薪20万的梦想... python对文件.目录能做什么?或者说我们需要python替我们做什么?最经常的操作就是对文件的:打开.关闭.读取.写入.修改.保存等等对目录的操作,无非就是 ...
- 从三个语言(C++,Java,.Net)的几个性能测试案例来看性能优化
随着时间的发展,现在的虚拟机技术越来越成熟了,在有些情况下,Java,.Net等虚拟机密集计算的性能已经和C++相仿,在个别情况下,甚至还要更加优秀.本文详细分析几个性能测试案例,探讨现象背后的原因. ...
- openGL基本概念
OpenGL自身是一个巨大的状态机(State Machine):一系列的变量描述OpenGL此刻应当如何运行.OpenGL的状态通常被称为OpenGL上下文(Context).我们通常使用如下途径去 ...
- interceptor拦截器
fifter.servlet.interceptor fifter用来处理请求头.请求参数.编码的一些设置,然后转交给servlet,处理业务,返回 servlet现在常用的spring,servle ...
- selenium定时签到程序
selenium定时签到程序 定时任务 # -*- coding: utf-8 -*- import time import os import sched import datetime from ...
- PythonDay05
第五章 今日内容 字典 字典 语法:{'key1':1,'key2':2} 注意:dict保存的数据不是按照我们添加进去的顺序保存的. 是按照hash表的顺序保存的. ⽽hash表 不是连续的. 所以 ...
- 编码规范 | Java函数优雅之道(上)
导读 随着软件项目代码的日积月累,系统维护成本变得越来越高,是所有软件团队面临的共同问题.持续地优化代码,提高代码的质量,是提升系统生命力的有效手段之一.软件系统思维有句话“Less coding, ...
- css常用代码块
顶部固定导航栏 | css position: fixed; top: 0; left: 0; z-index: 9999; width: 100%; height: 48px; border-top ...
- Hive安装与部署
进去root权限(su) 1.从https://mirrors.tuna.tsinghua.edu.cn/apache/hive/hive-1.2.2/apache-hive-1.2.2-bin.ta ...
- grep文本搜索工具详解
############grep命令############这个命令属于文本处理三大命令之一,强大的文本搜索工具(贪婪模式)全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达 ...