博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ2513——Colored Sticks(Trie树+欧拉回路+并查集)
阅读量:4705 次
发布时间:2019-06-10

本文共 2711 字,大约阅读时间需要 9 分钟。

Colored Sticks

Description
You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a straight line such that the colors of the endpoints that touch are of the same color?
Input
Input is a sequence of lines, each line contains two words, separated by spaces, giving the colors of the endpoints of one stick. A word is a sequence of lowercase letters no longer than 10 characters. There is no more than 250000 sticks.
Output
If the sticks can be aligned in the desired way, output a single line saying Possible, otherwise output Impossible.
Sample Input
blue red
red violet
cyan blue
blue magenta
magenta cyan
Sample Output
Possible

题目大意:

    给定一些木条,木条的两头都有颜色,颜色相同的两根木条可以相连,问是否可以连成一条直线。

解题思路:

    其实就是问是否可以构成欧拉路径。这里给出欧拉路径的定义。

    若图G中存在这样一条路径,使得它恰通过G中每条边一次,则称该路径为欧拉路径。若该路径是一个圈,则称为欧拉(Euler)回路。(即一笔画问题)

    存在欧拉路径的成分必要条件为:一个无向图存在欧拉回路,当且仅当该图只存在0或2个奇数入度数的顶点,且该图是连通图

    1)判断 奇数度数的个数

      很好搞定,建立一个数组存放每个节点的入度,建图结束后遍历即可。

    2)判断是否为连通图

      使用并查集和路径压缩来做。若路径压缩之后每个点都有着相同的父节点(即每个点都有相同的祖先节点)。说明图连通。

    Trie树的应用

    由于该题的Node为字符串,建图的时候会比较困难。使用map映射会超时,所以使用Trie树来进行映射。具体实现请看代码备注。

Code:

1 /*************************************************************************  2     > File Name: poj2513.cpp  3     > Author: Enumz  4     > Mail: 369372123@qq.com  5     > Created Time: 2014年10月26日 星期日 20时16分17秒  6  ************************************************************************/  7   8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #include
16 #include
17 #include
18 #include
19 #include
20 #define MAXN 500001 21 using namespace std; 22 class TrieTree_Node 23 { 24 public: 25 bool flag; //判断是否为一个单词的叶子节点 26 int id; //为每一个单词分配一个id,相当于映射关系 27 TrieTree_Node *next[27]; 28 TrieTree_Node() 29 { 30 flag=0; 31 id=0; 32 memset(next,0,sizeof(next)); 33 } 34 }root; 35 int cnt=0; 36 int degree[MAXN]; //入度数组 37 int father[MAXN]; 38 int tree(char *s) 39 { 40 TrieTree_Node *p=&root; 41 int len=strlen(s); 42 for (int i=0;i<=len-1;i++) 43 { 44 int tmp=s[i]-'a'; 45 if (p->next[tmp]==NULL) 46 p->next[tmp]=new TrieTree_Node; 47 p=p->next[tmp]; 48 } 49 if (p->flag) //如果找到了该字符串,返回其ID 50 return p->id; 51 else //没有找到,就创建这个字符串,并标记叶子节点,分配ID 52 { 53 p->flag=1; 54 p->id=++cnt; 55 return p->id; 56 } 57 } 58 int find(int a) 59 { 60 if (father[a]!=a) 61 father[a]=find(father[a]); 62 return father[a]; 63 } 64 void join(int a,int b) 65 { 66 int fx=find(a),fy=find(b); 67 if (fx!=fy) 68 father[fx]=fy; 69 } 70 void init() 71 { 72 for (int i=0;i

 

    

转载于:https://www.cnblogs.com/Enumz/p/4062872.html

你可能感兴趣的文章
【模式识别与机器学习】——SVM举例
查看>>
【转】IT名企面试:微软笔试题(1)
查看>>
IO流入门-第十章-DataInputStream_DataOutputStream
查看>>
DRF的分页
查看>>
Mysql 模糊匹配(字符串str中是否包含子字符串substr)
查看>>
python:open/文件操作
查看>>
流程控制 Day06
查看>>
Linux下安装Tomcat
查看>>
windows live writer 2012 0x80070643
查看>>
tomcat 和MySQL的安装
查看>>
git常用操作
查看>>
京东SSO单点登陆实现分析
查看>>
u-boot启动第一阶段
查看>>
MySQL批量SQL插入性能优化
查看>>
定义列属性:null,default,PK,auto_increment
查看>>
用户画像展示
查看>>
C#中StreamReader读取中文出现乱码
查看>>
使用BufferedReader的时候出现的问题
查看>>
批处理文件中的路径问题
查看>>
hibernate出现No row with the given identifier exists问题
查看>>