POJ 2513 Colored Sticks(欧拉道路+字典树+并查集)
http://poj.org/problem?id=2513
题意:
给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的。
思路:
题目很明显的是欧拉道路的问题。
欧拉道路的关键是:
①图是连通的。
②最多只能有两个奇点。(不能只存在一个奇点)
本来是想用map映射的,但是太多了,比较费时,这里用字典树的话会比较省时,判断图是否连通可以用并查集来完成。
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std; const int maxn = + ;
int p[maxn], deg[maxn];
int cnt; struct Trie
{
int ch[maxn][];
int vis[maxn];
int sz; void init()
{
sz = ;
memset(ch[], , sizeof(ch[]));
} int insert(char *s, int& v)
{
int u = , n = strlen(s);
for (int i = ; i < n; i++)
{
int c = s[i]-'a';
if (!ch[u][c])
{
memset(ch[sz], , sizeof(ch[sz]));
vis[sz] = ;
ch[u][c] = sz++;
}
u = ch[u][c];
}
if (!vis[u]) vis[u] = ++v;
return vis[u];
}
}t; int find(int x)
{
return x == p[x] ? x : find(p[x]);
} void merge(int x, int y)
{
int fx = find(x);
int fy = find(y);
if (fx != fy)
p[fx] = fy;
} int main()
{
//freopen("D:\\txt.txt", "r", stdin);
char a[], b[];
t.init();
memset(deg, , sizeof(deg));
for (int i = ; i <= maxn; i++) p[i] = i;
cnt = ;
while (~scanf("%s %s", a, b))
{
int id1 = t.insert(a,cnt);
int id2 = t.insert(b,cnt);
deg[id1]++;
deg[id2]++;
merge(id1, id2);
}
int ans = ;
for (int i = ; i <= cnt; i++)
{
if (deg[i] % == ) ans++;
if (ans > || find() != find(i))
{
printf("Impossible\n");
return ;
}
}
if (ans == )
printf("Impossible\n");
else
printf("Possible\n");
return ;
}
POJ 2513 Colored Sticks(欧拉道路+字典树+并查集)的更多相关文章
- POJ 2513 无向欧拉通路+字典树+并查集
题目大意: 有一堆头尾均有颜色的木条,要让它们拼接在一起,拼接处颜色要保证相同,问是否能够实现 这道题我一开始利用map<string,int>来对颜色进行赋值,好进行后面的并查操作以及欧 ...
- POJ 2513 Colored Sticks(欧拉回路,字典树,并查集)
题意:给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 无向图存在欧拉路的充要条件为: ① 图是连通的: ② 所有节 ...
- POJ 2513 - Colored Sticks - [欧拉路][图的连通性][字典树]
题目链接: http://poj.org/problem?id=2513 http://bailian.openjudge.cn/practice/2513?lang=en_US Time Limit ...
- [欧拉] poj 2513 Colored Sticks
主题链接: http://poj.org/problem? id=2513 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Tota ...
- poj 2513 Colored Sticks(欧拉路径+并检查集合+特里)
题目链接:poj 2513 Colored Sticks 题目大意:有N个木棍,每根木棍两端被涂上颜色.如今给定每一个木棍两端的颜色.不同木棍之间拼接须要颜色同样的 端才干够.问最后是否能将N个木棍拼 ...
- POJ2513 Colored Sticks(欧拉)
题目链接. 题目大意: 给很多木棍,两端被涂了颜色.任意两根木棍的相同颜色处可以拼接在一起,问有没有可能将所有的木棍都连起来,成一条直线? 分析: 考点,欧拉道路. 将一根木棍看成一条边,两端的颜色看 ...
- POJ 2513 Colored Sticks (离散化+并查集+欧拉通路)
下面两个写得很清楚了,就不在赘述. http://blog.sina.com.cn/s/blog_5cd4cccf0100apd1.htmlhttp://www.cnblogs.com/lyy2890 ...
- POJ - 2513 Colored Sticks(欧拉通路+并查集+字典树)
https://vjudge.net/problem/POJ-2513 题解转载自:優YoU http://user.qzone.qq.com/289065406/blog/1304742541 题 ...
- POJ 2513 Colored Sticks (欧拉回路 + 字典树 +并查集)
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27097 Accepted: 7175 ...
随机推荐
- maven安装和与IDE集成
第一部分:maven的基本信息和安装,配置 maven是一个项目构建和管理的工具,提供了帮助管理 构建.文档.报告.依赖.scms.发布.分发的方法.可以方便的编译代码.进行依赖管理.管理二进制库等 ...
- 【BZOJ4523】[Cqoi2016]路由表 Trie树模拟
[BZOJ4523][Cqoi2016]路由表 Description 路由表查找是路由器在转发IP报文时的重要环节.通常路由表中的表项由目的地址.掩码.下一跳(Next Hop)地址和其他辅助信息组 ...
- Windows Phone 几种页面间传递数据的方式
首先,我们要引用:using Microsoft.Phone.Shell; 第一种: // 导航到新页面 NavigationService.Navigate(new Uri("/Detai ...
- github(其他类似github)下载到本地配置
首先是先装完插件,低版本的myeclipse要自己装插件, 如何使window->show view->other找到git 注意是选第二个... 然后next->next-> ...
- 解读 Android TTS 语音合成播报
随着从事 Android 开发年限增加,负责的工作项目也从应用层开发逐步过渡到 Android Framework 层开发.虽然一开始就知道 Android 知识体系的庞大,但是当你逐渐从 Appli ...
- 阅读笔记:Solving the “false positives” problem in fraud prediction
刚读完一篇paper<Solving the “false positives” problem in fraud prediction>,趁热打铁,做个笔记. 文章下载链接:https: ...
- 【node】---multer模块实现图片上传---【巷子】
1.安装muterl第三方模块 cnpm install multer --save 2.使用 multer在解析完成后,会向request对象中添加一个body对象和一个file或者files对象( ...
- SQL---->mySQl安装for mac
我安装是参考如下两篇博客,但是有些不同,这里写好参考来源: http://blog.csdn.net/li_huifeng/article/details/9449685 http://www.jia ...
- Oracle等待事件之log file parallel write
log file parallel write表示等待 LGWR 向操作系统请求 I/O 开始直到完成 I/O.这种事件发生通常表示日志文件发生了I/O 竞争或者文件所在的驱动器较慢.这说明这种等待与 ...
- ArrayList遍历的三种方式 array arrayList转换
ArrayList遍历的三种方式 - 呵呵静 - 博客园 https://www.cnblogs.com/mjyung/p/6725182.html