问题便转化为:给定一个图,是否存在“一笔画”经过涂中每一点,以及经过每一边一次。这样就是求图中是否存在欧拉路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 + 并查集)的更多相关文章

  1. poj2513 Colored Sticks —— 字典树 + 并查集 + 欧拉回路

    题目链接:http://poj.org/problem?id=2513 题解:通过这题了解了字典树.用字典树存储颜色,并给颜色编上序号.这题为典型的欧拉回路问题:将每种颜色当成一个点.首先通过并查集判 ...

  2. POJ 2513 Colored Sticks (欧拉回路+并查集+字典树)

    题目链接 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with ...

  3. POJ 2513 Colored Sticks (离散化+并查集+欧拉通路)

    下面两个写得很清楚了,就不在赘述. http://blog.sina.com.cn/s/blog_5cd4cccf0100apd1.htmlhttp://www.cnblogs.com/lyy2890 ...

  4. Colored Sticks POJ - 2513 并查集+欧拉通路+字典树hash

    题意:给出很多很多很多很多个棒子 左右各有颜色(给出的是单词) 相同颜色的可以接在一起,问是否存在一种 方法可以使得所以棒子连在一起 思路:就是一个判欧拉通路的题目,欧拉通路存在:没奇度顶点   或者 ...

  5. POJ2513 【并查集+欧拉路径+trie树】

    题目链接:http://poj.org/problem?id=2513 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total ...

  6. POJ-2513 Colored Sticks---欧拉回路+并查集+字典树

    题目链接: https://vjudge.net/problem/POJ-2513 题目大意: 给一些木棍,两端都有颜色,只有两根对应的端点颜色相同才能相接,问能不能把它们接成一根木棍 解题思路: 题 ...

  7. Colored Sticks(trie)

    http://poj.org/problem?id=2513 题意:给一些木棒,木棒两端图上颜色,将端点颜色相同的木棒连在一起,问是否能连成一条直线. 思路:将两端的颜色看成点,将木棒看成边,判断是否 ...

  8. [LOJ#6198]谢特[后缀数组+trie+并查集]

    题意 给你一个长度为 \(n\) 的字符串,问 \(LCP(i,j)+(w_i\ xor\ w_j)\) 的最大值,其中 \(LCP\) 表示两个后缀的最长公共前缀. \(n\le 10^5\) 分析 ...

  9. POJ2513——Colored Sticks(Trie树+欧拉回路+并查集)

    Colored Sticks DescriptionYou are given a bunch of wooden sticks. Each endpoint of each stick is col ...

随机推荐

  1. Linux 服务器环境启动

    1.PHP 关闭php killall php-fpm php重启 /usr/local/php/sbin/php-fpm & 或者 /usr/local/php/sbin/php-fpm { ...

  2. KafkaConsumer对于事务消息的处理

    Kafka添加了事务机制以后,consumer端有个需要解决的问题就是怎么样从收到的消息中滤掉aborted的消息.Kafka通过broker和consumer端的协作,利用一系列优化手段极大地降低了 ...

  3. CEdit自动换行和状态栏添加

    CEdit自动换行在对话框的属性中是可以直接设置的. Auto HScroll设置为False Auto VScroll设置为True Mulitline设置为True Want Return设置为T ...

  4. gcc编译选项汇集

    gcc -g 调试选项(DEBUGGING OPTION)GNU CC拥有许多特别选项,既可以调试用户的程序,也可以对GCC排错: -g 以操作系统的本地格式(stabs, COFF, XCOFF,或 ...

  5. ES6 数组扩展

    1....扩展运算符 该运算符将一个数组,变为参数序列. 作用:(1)代替aplly 'use strict'; Math.max(...[2,5,8]) (2)将字符串转为数组 2.Array.fr ...

  6. XAudio2学习之混音

    XAudio2不仅能够进行採样率转换.还能够进行混音.所谓混音就是将多路音频混合成一路进行输出.混音主要是IXAudio2SubmixVoice进行此项功能. 数据由IXAudio2SourceVoi ...

  7. android上FragmentTabHost实现自己定义Tab Indicator

    近期一直在做安卓开发,发现Tab布局能够用FragmentTabHost来实现,唯一不好的就是不能实现带图标的tabindicator, V4版本号中的尽管API有支持,可是不管怎么设置Drawabl ...

  8. [C/C++]关于C++11中的std::move和std::forward

    http://www.cnblogs.com/cbscan/archive/2012/01/10/2318482.html http://blog.csdn.net/fcryuuhou/article ...

  9. 【Spring实战】—— 6 内部Bean

    本篇文章讲解了Spring的通过内部Bean设置Bean的属性. 类似内部类,内部Bean与普通的Bean关联不同的是: 1 普通的Bean,在其他的Bean实例引用时,都引用同一个实例. 2 内部B ...

  10. TCP 的那些事儿(上) SACK

    http://blog.csdn.net/woxiaozhi/article/details/27328557 文章太好了,转载过啦 这篇文章分为上下两篇  确实不错  所以存在这里收藏 TCP是一个 ...