POJ 2513 【字典树】【欧拉回路】
题意:
有很多棒子,两端有颜色,告诉你两端的颜色,让你把这些棒子拼接起来要求相邻的接点的两个颜色是一样的。
问能否拼接成功。
思路:
将颜色看作节点,将棒子看作边,寻找欧拉通路。
保证图的连通性的时候用到并查集。
这里颜色由于是字符串代替,所以需要用到字典树优化离散化过程。
字典树的学习感谢博客http://www.ahathinking.com/archives/14.html
重点是这个图很棒:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<string>
#include<iostream>
#include<stdlib.h>
using namespace std;
int me[*+];
int out[];
struct ti
{
void add(char *,int);
int findi(char *);
ti *mine[];
int num;
};
void ti::add(char *s,int tmp)
{
int len=strlen(s);
ti *next=this;
for(int i=;i<len;i++)
{
if(next->mine[s[i]-]==NULL)
{
next->mine[s[i]-]=(ti*)malloc(sizeof(ti));
memset(next->mine[s[i]-]->mine,NULL,sizeof(mine));
next->mine[s[i]-]->num=;
}
next=next->mine[s[i]-];
}
next->num=tmp;
}
int ti::findi(char *s)
{
int len=strlen(s);
ti *next=this;
for(int i=;i<len;i++)
{
if(next->mine[s[i]-]==NULL)
{
return ;
}
next=next->mine[s[i]-];
}
return next->num;
}
ti tree;
int findme(int n)
{
if(n!=me[n])
return me[n]=findme(me[n]);
return n;
}
int main()
{
memset(tree.mine,NULL,sizeof(tree.mine));
tree.num=;
char col1[],col2[];
for(int i=;i<=;i++)
{
me[i]=i;
}
int num=;
while(scanf("%s%s",col1,col2)!=EOF)
{
if(tree.findi(col1)==)
{
num++;
tree.add(col1,num);
}
if(tree.findi(col2)==)
{
num++;
tree.add(col2,num);
}
int tmpa=findme(tree.findi(col1));
int tmpb=findme(tree.findi(col2));
if(tmpa!=tmpb)
me[tmpb]=tmpa;
out[tree.findi(col1)]++;
out[tree.findi(col2)]++;
}
int c=;
int x,y,z;
x=y=z=;
for(int i=;i<=num;i++)
{
if(me[i]==i)
c++;
if(out[i]&)
x++;
}
if(x==||c>||x>)
printf("Impossible\n");
else
printf("Possible\n");
}
POJ 2513 【字典树】【欧拉回路】的更多相关文章
- POJ 2513 字典树+并查集+欧拉路径
Description: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 解题思路: 可以用图论中欧拉路的知识来解这道题,首先可以把木 ...
- nyoj 230/poj 2513 彩色棒 并查集+字典树+欧拉回路
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=230 题意:给你许许多多的木棍,没条木棍两端有两种颜色,问你在将木棍相连时,接触的端点颜色 ...
- POJ 2418 字典树
题目链接:http://poj.org/problem?id=2418 题意:给定一堆树的名字,现在问你每一棵树[无重复]的出现的百分比,并按树名的字典序输出 思路:最简单的就是用map来写,关于字典 ...
- POJ 2503 字典树
题目链接:http://poj.org/problem?id=2503 题意:给定一个词典,输入格式为[string1' 'string2] 意思是string2的值为string1. 然后给定一波 ...
- poj2513连接木棍(字典树+欧拉回路+并查集)
题目传送门 题目大意:给你一堆木棍,每根木管都有两种颜色,相同颜色的部分可以连接起来,问你这堆木棍可不可以连接成1根. 思路:大致的思路很好想,就是判断欧拉回路的方法(1.联通,2,要么顶点读书全为偶 ...
- 优先队列 + 并查集 + 字典树 + 欧拉回路 + 树状数组 + 线段树 + 线段树点更新 + KMP +AC自动机 + 扫描线
这里给出基本思想和实现代码 . 优先队列 : 曾经做过的一道例题 坦克大战 struct node { int x,y,step; friend bool operator <(no ...
- poj 3764 字典树
The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7332 Accepted: 1 ...
- POJ 2513 trie树+并查集判断无向图的欧拉路
生无可恋 查RE查了一个多小时.. 原因是我N define的是250500 应该是500500!!!!!!!!! 身败名裂,已无颜面对众人.. 吐槽完了 我们来说思路... 思路: 判有向图能否形成 ...
- POJ 2001 字典树(入门题)
#include<cstdio> #include<algorithm> #include<iostream> #include<cstring> #i ...
- POJ 2513 Colored Sticks (欧拉回路+并查集+字典树)
题目链接 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with ...
随机推荐
- vue同胞组件通讯解决方案(以下为一种另外可用vuex解决)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- dns2tcp使用教程
在2010年6月的更新(也是迄今为止最新的更新)后,其源代码支持编译为Windows平台的可执行程序.而且此工具使用C语言开发编写,不需要TUN/TAP,所以大大加强了它的可用性. 下载 当前最新的0 ...
- vue 数据没有驱动视图?
Part.1 问题 数据改变,视图却没有根据数据而改变. 原因在于,数据并不在 vue 监听范围之内,vue 只对事先在 data 中声明的变量丶对象等类型数据进行监听 Part.2 例子 < ...
- regular expression matching DP
这个题目,我从前天晚上(8月6号晚上)调试到现在(8月8号16:21),太心酸了,不好好总结一下,就太对不起自己了! 这是题目: Implement regular expression matchi ...
- 解决chrome 批量下载器 mgblihnaaedmhhgadafknogahbgejnno 插件乱码
找到 mgblihnaaedmhhgadafknogahbgejnno\当前版本号(0.0.1_0)\popup.html <html> <head> <meta cha ...
- git 添加外部项目地址
github 提交第三方模块流程 // git config --global user.name 'your name' 可以设置全局用户名,在commit记录里显示的是这个配置设置的名称. / ...
- linux 查看分区UUID的两种方法
1. sudo blkid /dev/loop0: TYPE="squashfs"/dev/loop1: TYPE="squashfs"/dev/loop2: ...
- Redis那些事(一) — Redis简介
本人最近在学习Redis的使用和底层原理,有一些收获,所以希望通过写博客的形式来记录自己的学习过程,加深自己的理解,同时也方便以后查阅复习.目前打算先记录一些基本的使用方法和部分底层实现,其他的如果有 ...
- JDK的4种引用类型
在java中,大致有以下几种引用类型,强引用(StrongReference).软引用(SoftReference).弱引用(WeakReference).虚引用(PhantomReference) ...
- 导出csv文件(php实现)
<?php namespace App\Library\lib; class CsvLib { /** * [构造函数] * */ public function __construct() { ...