Colored Sticks - poj2513(trie + 并查集)
问题便转化为:给定一个图,是否存在“一笔画”经过涂中每一点,以及经过每一边一次。这样就是求图中是否存在欧拉路Euler-Path。
由图论知识可以知道,无向图存在欧拉路的充要条件为:
① 图是连通的;
② 所有节点的度为偶数,或者有且只有两个度为奇数的节点。
trie 和 并查集
#include <iostream>
#include <vector>
#include <string.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
typedef struct trie{
int trie_index;
bool flag;
trie* next[]; } Trie;
void trie_init(Trie* tr){
tr->trie_index = -;
tr->flag = false;
for(int i = ; i< ; i ++)
tr->next[i] = NULL;
}
int trie_index;
Trie *root;
vector<int> trie_set;
vector<int> trie_rank; int trie_hash(char *input){
int i = ;
Trie* path = root;
while(input[i] != ){
if(path->next[input[i] - 'a'] == NULL){
path->next[input[i] - 'a'] = (Trie*)malloc(sizeof(Trie));
trie_init(path->next[input[i] - 'a']);
}
path = path->next[input[i] - 'a'];
i ++;
}
if(path->flag == true){
return path->trie_index;
}else{
path->flag = true;
path->trie_index = trie_index ++;
return path->trie_index;
} }
int find_set(int i){
if(trie_set[i] == i)
return i;
return find_set(trie_set[i]); }
void union_set(int i, int j){
int i_p = find_set(i);
int j_p = find_set(j);
if(i_p == j_p) return;
if(trie_rank[i_p] > trie_rank[j_p])
trie_set[j_p] = i_p;
else{
if(trie_rank[i_p] == trie_rank[j_p])
trie_rank[j_p]++;
trie_set[i_p] = j_p;
} }
int main(int argc, char* argv[]){
trie_index = ;
vector<int> trie_degree(,);
char a[], b[];
root = (Trie*)malloc(sizeof(Trie));
trie_init(root);
for(int i = ; i < ; i ++){
trie_set.push_back(i);
trie_rank.push_back();
}
while (scanf("%s%s",a,b)!=EOF){
int i = trie_hash(a);
int j = trie_hash(b);
trie_degree[i] += ;
trie_degree[j] += ; union_set(i, j); }
int pre = find_set();
bool result = true;
for(int i = ; i < trie_index; i ++)
if(find_set(i) != pre)
result = false;
int odd_num = ;
for(int i = ; i < trie_index; i ++){
if((trie_degree[i] & ) == )
odd_num += ;
}
if(odd_num== || odd_num > )
result = false;
if(result == true)
cout << "Possible" << endl;
else
cout << "Impossible" << endl;
return ;
}
样本输入
Sample Input blue red
red violet
cyan blue
blue magenta
magenta cyan
Sample Output Possible
Colored Sticks - poj2513(trie + 并查集)的更多相关文章
- poj2513 Colored Sticks —— 字典树 + 并查集 + 欧拉回路
题目链接:http://poj.org/problem?id=2513 题解:通过这题了解了字典树.用字典树存储颜色,并给颜色编上序号.这题为典型的欧拉回路问题:将每种颜色当成一个点.首先通过并查集判 ...
- POJ 2513 Colored Sticks (欧拉回路+并查集+字典树)
题目链接 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with ...
- POJ 2513 Colored Sticks (离散化+并查集+欧拉通路)
下面两个写得很清楚了,就不在赘述. http://blog.sina.com.cn/s/blog_5cd4cccf0100apd1.htmlhttp://www.cnblogs.com/lyy2890 ...
- Colored Sticks POJ - 2513 并查集+欧拉通路+字典树hash
题意:给出很多很多很多很多个棒子 左右各有颜色(给出的是单词) 相同颜色的可以接在一起,问是否存在一种 方法可以使得所以棒子连在一起 思路:就是一个判欧拉通路的题目,欧拉通路存在:没奇度顶点 或者 ...
- POJ2513 【并查集+欧拉路径+trie树】
题目链接:http://poj.org/problem?id=2513 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total ...
- POJ-2513 Colored Sticks---欧拉回路+并查集+字典树
题目链接: https://vjudge.net/problem/POJ-2513 题目大意: 给一些木棍,两端都有颜色,只有两根对应的端点颜色相同才能相接,问能不能把它们接成一根木棍 解题思路: 题 ...
- Colored Sticks(trie)
http://poj.org/problem?id=2513 题意:给一些木棒,木棒两端图上颜色,将端点颜色相同的木棒连在一起,问是否能连成一条直线. 思路:将两端的颜色看成点,将木棒看成边,判断是否 ...
- [LOJ#6198]谢特[后缀数组+trie+并查集]
题意 给你一个长度为 \(n\) 的字符串,问 \(LCP(i,j)+(w_i\ xor\ w_j)\) 的最大值,其中 \(LCP\) 表示两个后缀的最长公共前缀. \(n\le 10^5\) 分析 ...
- POJ2513——Colored Sticks(Trie树+欧拉回路+并查集)
Colored Sticks DescriptionYou are given a bunch of wooden sticks. Each endpoint of each stick is col ...
随机推荐
- 转:Android 签名验证机制(相当不错,强烈推荐)
转: http://riusksk.blogbus.com/logs/272154406.html Android应用签名验证过程中,满足以下条件才能安装应用: 1.SHA-1(除META-INF目 ...
- QSignalMapper类的使用
Qt中当定义了非常多的button,而他们的信号都同样时(比方都是点击信号),没有必要给他们每一个都设置信号和槽的链接.QSignalMapper给我们攻克了这个难题. 今天完毕这个相似的功能时,不知 ...
- Python工作日类库Busines Holiday介绍
引言: 在日常工作中.常常会碰到相似的场景.须要计算在某个时间段内的工作日以及确定某天是否为工作日,这里的介绍的工具包将很好的解决问题. 1. 工具包Business Holiday介绍 其提供了很e ...
- 【MVC5】日期选择控件DatePicker
项目中使用了Bootstrap,日期控件就选择了依赖于bootstrap的DatePicker. 在App_Start\BundleConfig.cs中引用css和js文件: bundles.Add( ...
- 算法笔记_085:蓝桥杯练习 9-3摩尔斯电码(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 摩尔斯电码破译.类似于乔林教材第213页的例6.5,要求输入摩尔斯码,返回英文.请不要使用"zylib.h",只能使用 ...
- .gitignore 里面常写的值
一般用这个文件来控制一些不想提交的内容 这个可以做一个参考 # Windows image file caches Thumbs.db ehthumbs.db # Folder config fi ...
- 11-hibernate,单表GRUD操作实例
1,save 2,update 3,delete 4,get/load(查询单个纪录) 实例代码: import java.io.File; import java.io.FileInputStrea ...
- 使用原生js将轮播图组件化
代码地址如下:http://www.demodashi.com/demo/11316.html 这是一个轮播图组件,这里是代码地址,需要传入容器的id和图片地址,支持Internet Explor ...
- Linux系统编程之----》信号
"===信号========================================================================================= ...
- Java程序猿的JavaScript学习笔记(10—— jQuery-在“类”层面扩展)
计划按例如以下顺序完毕这篇笔记: Java程序猿的JavaScript学习笔记(1--理念) Java程序猿的JavaScript学习笔记(2--属性复制和继承) Java程序猿的JavaScript ...