poj 2513 Colored Sticks trie树+欧拉图+并查集
| Time Limit: 5000MS | Memory Limit: 128000K | |
| Total Submissions: 27955 | Accepted: 7403 |
Description
Input
Output
Sample Input
blue red
red violet
cyan blue
blue magenta
magenta cyan
Sample Output
Possible
Hint
一开始想用map建立字符串和数字的映射,然后用并查集判断联通,欧拉图判断是否可以构成一笔画问题,但是最后超时了,网上一查才知道必须用trie建立映射关系才能过
因为字符最多10位,那么树的深度最多10层,映射任何一个字符串都可以在近似常数时间内完成,效率非常高
先附上map实现的TLE的代码:
#include<stdio.h>
#include<string>
#include<map>
using namespace std;
int father[500010];
int degree[500010] = {0};
//int *root[26]; //
int getfather(int i)
{
if(father[i] == 0)
return father[i] = i;
else if(father[i] == i)
return i;
else
return father[i] = getfather(father[i]);
}
void mergeset(int a, int b)
{
a = getfather(a);
b = getfather(b);
father[a] = b;
}
int main()
{
// freopen("in.txt", "r", stdin);
map<string, int> color; char color1[20], color2[20];
int total = 0;
while(scanf("%s%s", color1, color2) != EOF)
{
int a, b;
if(color[color1] == 0)
a = (color[color1] = ++total);
if(color[color2] == 0)
b = (color[color2] = ++total);
degree[a] ++;
degree[b] ++;
mergeset(a, b);
}
int i;
int fa = getfather(1);
bool flag = 0;
int count = 0;
// for(i = 1; i <
for(i = 1; i <= total; i++)
{
if(getfather(i) != fa)
break;
if(degree[i] % 2 != 0)
{
count ++;
if(count > 2)
break;
}
}
if(i <= total || count == 1)
printf("Impossible\n");
else
printf("Possible\n"); return 0;
}
可以发现map省事很多,但是效率不高
这是trie图的AC代码:1250MS
#include<stdio.h>
#include<string>
#include<iostream>
#include<map>
using namespace std;
int father[500010];
int degree[500010] = {0}; struct Node
{
int num;
Node *next[26];
Node()
{
num = 0;
int i;
for(i = 0; i < 26; i++)
next[i] = 0;
}
};
Node root[26];
int total;
int getnum(char * str, Node * node)//getnum函数既可以插入也可以获取,如果没有这个单词就建立一个,如果有了就返回这个单词的编号
{
if(*str == 0)
{
if(node->num != 0)
return node->num;
else
return node->num = ++total;
}
if(node->next[*str - 'a'] == 0)
node->next[*str - 'a'] = new Node;
return getnum(str + 1, node->next[*str - 'a']);
}
int getfather(int i)
{
if(father[i] == 0)
return father[i] = i;
else if(father[i] == i)
return i;
else
return father[i] = getfather(father[i]);
}
void mergeset(int a, int b)
{
a = getfather(a);
b = getfather(b);
father[a] = b;
}
int main()
{
// freopen("in.txt", "r", stdin);
char color1[20], color2[20];
while(scanf("%s%s", color1, color2) != EOF)
{
int a = getnum(color1 + 1, &root[*color1 - 'a']), b = getnum(color2 + 1, &root[*color2 - 'a']);
degree[a] ++;
degree[b] ++;
mergeset(a, b);
}
int i;
int fa = getfather(1);
bool flag = 0;
int count = 0;
for(i = 1; i <= total; i++)
{
if(getfather(i) != fa)
break;
if(degree[i] % 2 != 0)
{
count ++;
if(count > 2)
break;
}
}
if(i <= total || count == 1)
printf("Impossible\n");
else
printf("Possible\n"); return 0;
}
poj 2513 Colored Sticks trie树+欧拉图+并查集的更多相关文章
- POJ 2513 Colored Sticks 字典树、并查集、欧拉通路
Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some ...
- poj 2513 Colored Sticks (trie 树)
链接:poj 2513 题意:给定一些木棒.木棒两端都涂上颜色,不同木棒相接的一边必须是 同样的颜色.求能否将木棒首尾相接.连成一条直线. 分析:能够用欧拉路的思想来解,将木棒的每一端都看成一个结点 ...
- poj 2513 Colored Sticks (trie树+并查集+欧拉路)
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 40043 Accepted: 10406 ...
- POJ2513——Colored Sticks(Trie树+欧拉回路+并查集)
Colored Sticks DescriptionYou are given a bunch of wooden sticks. Each endpoint of each stick is col ...
- POJ2513:Colored Sticks(字典树+欧拉路径+并查集)
http://poj.org/problem?id=2513 Description You are given a bunch of wooden sticks. Each endpoint of ...
- poj 2513 Colored Sticks( 字典树哈希+ 欧拉回路 + 并查集)
题目:http://poj.org/problem?id=2513 参考博客:http://blog.csdn.net/lyy289065406/article/details/6647445 htt ...
- poj 2513 Colored Sticks(欧拉路径+并检查集合+特里)
题目链接:poj 2513 Colored Sticks 题目大意:有N个木棍,每根木棍两端被涂上颜色.如今给定每一个木棍两端的颜色.不同木棍之间拼接须要颜色同样的 端才干够.问最后是否能将N个木棍拼 ...
- [欧拉] poj 2513 Colored Sticks
主题链接: http://poj.org/problem? id=2513 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Tota ...
- POJ 2513 - Colored Sticks - [欧拉路][图的连通性][字典树]
题目链接: http://poj.org/problem?id=2513 http://bailian.openjudge.cn/practice/2513?lang=en_US Time Limit ...
随机推荐
- 怎样将Sqlserver数据库转成mysql数据库
手上有一个网站之前是用asp.net做的,所使用的数据库是sqlserver,现在打算用PHP+MYSQL架构.因原来的站点有一定排名,直接改版的话,会导致产生很多错误页,网站排名和收录结果要恢复过来 ...
- PHP 缓存扩展opcache
opcache (全程 zend opcache): 从php5.5开始,默认提供的php脚本缓存扩展,编译php5.5时加上参数--enable-opcache就可以编译opcache了,只是要启用 ...
- iOS iPad开发之UIPopoverController的使用
1. 什么是UIPopoverController? 是iPad开发中常见的一种控制器(在iphone上不允许使用) 跟其他控制器不一样的是,它直接继承自NSObject,并非继承自UIViewCon ...
- HDU 4576 简单概率 + 滚动数组DP(大坑)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4576 坑大发了,居然加 % 也会超时: #include <cstdio> #includ ...
- Asp.Net异步导入Excel
故事:用户在页面上传一个excel文件,程序把excel里的内容入库. 技术方案:保存文件在服务器,jquey Ajax 异步读取文件中的记录到数据库,在页面实时刷新导入情况 页面前端 <%@ ...
- PHP操作Excel – PHPExcel 基本用法详解
导出excel属性设置//Include classrequire_once('Classes/PHPExcel.php');require_once('Classes/PHPExcel/Writer ...
- 05——C++自己合成的函数
C++编译器自己合成的构造函数: 默认构造函数 copy构造函数 copy assigment操作符 析构函数(编译器产生的析构时non-virtual) copy assignment(当含有con ...
- Mongodb在NUMA机器上的优化
10gen在mongodb的部署指南上,提到了在NUMA机器上,mongodb可能会出现问题,参见:http://docs.mongodb.org/manual/administration/prod ...
- LintCode Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- Nginx 502 Bad Gateway 错误的原因及解决方法
http://my.oschina.net/zhouyuan/blog/118708 刚才在调试程序的时候,居然服务器502错误,昨天晚上也发生了,好像我没有做非常规的操作. 然后网上寻找了下答案, ...