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 ...
随机推荐
- php 获取/设置用户訪问页面语言类
User Language Class 获取/设置用户訪问的页面语言,假设用户没有设置訪问语言.则读取Accept-Language. 依据用户选择的语言显示相应的页面(英文.中文简体,繁体中文) U ...
- 疯狂java学习路线图
- ssl证书之certbot
一.安装 1.下载压缩包:#wget https://github.com/certbot/certbot/archive/master.zip 2.解压包 3.官方文档https://github. ...
- 【转】iBatis简单入门教程
1. iBatis 简介: iBatis 是apache 的一个开源项目,一个O/R Mapping 解决方案,iBatis 最大的特点就是小巧,上手很快.如果不需要太多复杂的功能,iBatis 是能 ...
- Unity3d_ADBannerView
原地址:http://blog.csdn.net/cynixway/article/details/7686393 ADBnnerView提供对Apple iAd框架中ADBannerView的包中, ...
- python——关于Python Profilers性能分析器
1. 介绍性能分析器 profiler是一个程序,用来描述运行时的程序性能,并且从不同方面提供统计数据加以表述.Python中含有3个模块提供这样的功能,分别是cProfile, profile和ps ...
- js实现页面跳转的两种方式
CreateTime--2017年8月24日08:13:52Author:Marydon js实现页面跳转的两种方式 方式一: window.location.href = url 说明:我们常用 ...
- C++实现对数学基本运算表达式的解析
代码地址如下:http://www.demodashi.com/demo/11078.html 前段时间在LeetCode上刷题,遇到了很多涉及对字符串进行解析的题目.可能是出于这个原因,最近迷恋上了 ...
- Fiddler-抓取安卓手机APP请求地址
第一步:下载神器Fiddler,下载链接: http://fiddler2.com/get-fiddler 下载完成之后,傻瓜式的安装一下了! 第二步:设置Fiddler打开Fiddler, ...
- bbc mvn报错
http://www.cnblogs.com/zhouyalei/archive/2011/11/30/2268606.html