问题便转化为:给定一个图,是否存在“一笔画”经过涂中每一点,以及经过每一边一次。这样就是求图中是否存在欧拉路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. Android面试题(1)

    1. 下列哪些语句关于内存回收的说明是正确的? (b ) A. 程序员必须创建一个线程来释放内存 B. 内存回收程序负责释放无用内存 C. 内存回收程序允许程序员直接释放内存 D. 内存回收程序可以在 ...

  2. (转)Vue2.0 推荐环境

    Vue2.0 新手完全填坑攻略——从环境搭建到发布 http://www.jianshu.com/p/5ba253651c3b Jinkey原创感谢 showonne.yubang 技术指导Demo ...

  3. ssh认证

    密钥认证 密码验证会造成账户口令的外泄,不安全,基于账号的保密性考虑,可以采用密钥验证实现远程连接. Linux--Linux 1.Linux客户端主机上生成密钥文件 ssh-keygen -t rs ...

  4. SDL 学习及相关API

    SDL_PeepEvents() 在事件队列中搜索特定类型的事件. int SDL_PeepEvents(SDL_Event *events, int numevents, SDL_eventacti ...

  5. github 管理图示

  6. 强大的vim配置文件,让编程更随意(转)

    欢迎来到小码哥的博客 博客搬家啦 blog.ma6174.com 强大的vim配置文件,让编程更随意 花了很长时间整理的,感觉用起来很方便,共享一下. 我的vim配置主要有以下优点: 1.按F5可以直 ...

  7. java数据库操作:JDBC的操作

    1,JDBC注意操作类及接口: 数据库操作过程: 1)打开数据库服务 2)连接数据库:一般都要输入用户名,密码, 3)操作数据库:创建表:查询表,更新,记录. 4)关闭数据库. 1,DriverMan ...

  8. 阿里云 oss python3 样例

    阿里云的oss SDK又是不支持python3,头疼头疼. 本想改一改它的SDK,让它支持python2+python3,无奈里面大量的代码使用不带括号的print.工作量恐怖. 幸好oss的使用很e ...

  9. 全排列算法 --javascript 实现

    var Ann = function a(arr){ if(arr.length == 1){return arr;} var rr = new Array(); for(var i = 0; i&l ...

  10. SuperMap iClient如何使用WMS地图服务

    什么是WMS服务 WMS(Web Map Service,Web 地图服务)服务,该服务符合 OGC(Open Geospatial Consortium,开放地理信息联盟)制定的 WMS 实现规范. ...