Colored Sticks POJ - 2513(trie树欧拉路)
题意:
就是无向图欧拉路
解析:
不能用map。。超时
在判断是否只有一个联通的时候,我比较喜欢用set,但也不能用set,会超时,反正不能用stl
emm
用trie树来编号就好了
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define pd(a) printf("%d\n", a);
#define plld(a) printf("%lld\n", a);
#define pc(a) printf("%c\n", a);
#define ps(a) printf("%s\n", a);
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff;
char str1[maxn], str2[maxn];
int rt, ans, tot;
int f[maxn], deg[maxn];
int trie[maxn << ][], vis[maxn << ];
//map<string, int> vis;
set<int> ss; int find(int x)
{
return f[x] == x ? x : (f[x] = find(f[x]));
} void init()
{
for(int i = ; i < maxn; i++) f[i] = i;
tot = ans = ;
mem(deg, );
} int build(char* s)
{
int len = strlen(s);
rt = ;
for(int i = ; i < len; i++)
{
int x = s[i] - 'a';
if(trie[rt][x] == ) trie[rt][x] = ++tot;
rt = trie[rt][x];
}
if(!vis[rt]) vis[rt] = ++ans;
return vis[rt];
} int main()
{
init();
int u, v;
int cnt = ;
while(scanf("%s%s", str1, str2) != EOF)
{
u = build(str1);
v = build(str2);
deg[u]++;
deg[v]++;
int l = find(u);
int r = find(v);
if(l != r) f[l] = r;
}
int ater = find();
int flag = , cnt1 = , cnt2 = ;
for(int i = ; i <= ans; i++)
{
int x = find(i);
if(x != ater)
{
printf("Impossible\n");
return ;
}
if(deg[i] & )
cnt1++;
}
if(cnt1 == || cnt1 == )
printf("Possible\n");
else
printf("Impossible\n"); return ;
}
Colored Sticks POJ - 2513(trie树欧拉路)的更多相关文章
- POJ 2513 trie树+并查集判断无向图的欧拉路
生无可恋 查RE查了一个多小时.. 原因是我N define的是250500 应该是500500!!!!!!!!! 身败名裂,已无颜面对众人.. 吐槽完了 我们来说思路... 思路: 判有向图能否形成 ...
- Colored Sticks POJ - 2513 并查集+欧拉通路+字典树hash
题意:给出很多很多很多很多个棒子 左右各有颜色(给出的是单词) 相同颜色的可以接在一起,问是否存在一种 方法可以使得所以棒子连在一起 思路:就是一个判欧拉通路的题目,欧拉通路存在:没奇度顶点 或者 ...
- POJ 2513 Colored Sticks (欧拉回路 + 字典树 +并查集)
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27097 Accepted: 7175 ...
- poj 1780 code(欧拉路)
/* 对于n为密码想要序列最短 那么 1234 2345 这两个一定挨着 就是说 前一个的后n-1位是后一个的前n-1位 假设n==3 我们用0-99作为点的编号建图 然后每个点连出去10条边 两个相 ...
- POJ 2513 字典树+并查集+欧拉路径
Description: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 解题思路: 可以用图论中欧拉路的知识来解这道题,首先可以把木 ...
- poj 2945 trie树统计字符串出现次数
用记录附加信息的val数组记录次数即可. trie的原理:每个可能出现的字目给一个编号c,那么整个树就是一个c叉树 ch[u][c]表示 节点u走c边过去之后的节点 PS:trie树还有种动态写法,使 ...
- POJ 3630 trie树
Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26559 Accepted: 8000 Descripti ...
- POJ 2945 trie树
Find the Clones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7704 Accepted: 2879 Descr ...
- POJ 2513 无向欧拉通路+字典树+并查集
题目大意: 有一堆头尾均有颜色的木条,要让它们拼接在一起,拼接处颜色要保证相同,问是否能够实现 这道题我一开始利用map<string,int>来对颜色进行赋值,好进行后面的并查操作以及欧 ...
随机推荐
- SpringSecurity初步理解
Authenticating a User with LDAP 首先创建一个简单的web控制器 package hello; import org.springframework.web.bind.a ...
- 新页面,简单的tree视图写法
.xml文件 <?xml version="1.0"?><openerp> <data> <!--Tree view--> < ...
- EXPERT FOR SQL SERVER诊断系列--索引
概述 索引设计是数据库设计中比较重要的一个环节,对数据库的性能起着至关重要的作用,但是索引的设计却又不是那么容易的事情,性能也不是那么轻易就获取到的,很多的技术人员因为不恰当的创建索引,最后使得其 ...
- C语言基础复习:字符,字符数组,字符串,字符指针
1. 概述2. 字符2.1 字符定义和大小2.2 字符的输入和输出2.3 字符的计算3. 字符数组3.1 字符数组的定义和大小3.2 字符数组的输入和输出3.3 字符数组的计算4. 字符串4.1 字符 ...
- Hexo+Github搭建博客问题
搭建过程如下: http://www.cnblogs.com/fengxiongZz/p/7707568.html 问题:第6步,发布上传代码一直不成功(没异常,也没成功). 解决:修改_ ...
- [T-ARA N4/二段横踢][Can We Love]
歌词来源:http://music.163.com/#/song?id=26310867 Can We Love Can We Love [Can We Love Can We Love] Can W ...
- Aop笔记
参考: https://blog.csdn.net/bombSKLK/article/details/79143145 示例 拦截的 注解的方法 @Around("@annotation(c ...
- A. Make a triangle!
题意 给你三条边a,b,c问使得构成三角形,需要增加的最少长度是多少 思路 数学了啦 代码 #include<bits/stdc++.h> using namespace std; #de ...
- Maven -Maven配置tomcat插件 两种
Maven Tomcat插件现在主要有两个版本,tomcat-maven-plugin和tomcat7-maven-plugin,使用方式基本相同. tomcat-maven-plugin 插件官网: ...
- [系统软件]Ubuntu 18.04中的Shutter禁用了“编辑”选项解决
本文引用自linux公社, 原文请点击 : https://www.linuxidc.com/Linux/2018-04/151911.htm 在Ubuntu 18.04中安装了我最喜欢的截图工具 ...