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]如何使用Mobile_Detect来判断访问网站的设备:安卓,平板,电脑
Mobile_Detect 是一个轻量级的开源移动设备(手机)检测的 PHP Class, 它使用 User-Agent 中的字符串,并结合 HTTP Header,来检测移动设备环境. 这个设备检测 ...
- Android 解决qq分享后返回程序出现的Bug
问题:当我们使用qq分享时,分享成功后选择留在qq,这个时候按home键,回到手机主界面,在点击回到我的app,这个时候会出现界面显示出来了,但是任何事件都不响应,即按钮没反应. 分析:这个时候回到我 ...
- setImmediate 函数详解
1.兼容性 只有IE10以上的IE浏览器才支持. 2.用途 https://developer.mozilla.org/zh-CN/docs/Web/API/Window/setImmediate 该 ...
- [Spring MVC - 2A] - java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
严重: Servlet.service() for servlet [springMVC] in context with path [/ExceptionManageSystem] threw ex ...
- JavaScript | 创建对象的9种方法详解
————————————————————————————————————————————————————————— 创建对象 标准对象模式 "use strict"; // *** ...
- RabbitMQ的应用场景以及基本原理介绍(转载)
1.背景 RabbitMQ是一个由erlang开发的AMQP(Advanved Message Queue)的开源实现. 2.应用场景 2.1异步处理 场景说明:用户注册后,需要发注册邮件和注册短信, ...
- 安装Windows Service总是发生异常!
打开VS2010 创建个windows服务应用程序!没有添加删除任何一行代码!然后按照下面的步骤 1. 将这个服务程序切换到设计视图2. 右击设计视图选择“添加安装程序”3. 切换到刚被添加的Proj ...
- Mac 常用的手势
可以在触屏版-更多手势查看 技巧:前期慢慢滑动练习.一定要慢慢滑动,这样可以清楚的看出有没有产生效果,尤其注意大拇指滑动的感觉. 回到桌面:四指向外 打开Lanuchpad:四指向内 查看所有任务:三 ...
- Hibernate一级缓存和二级缓存具体解释
一.一级缓存二级缓存的概念解释 (1)一级缓存就是Session级别的缓存,一个Session做了一个查询操作,它会把这个操作的结果放在一级缓存中.假设短时间内这个 session(一定要同一个ses ...
- 点滴积累【JS】---JS小功能(offsetLeft实现图片滚动效果)
效果: 代码: <head runat="server"> <title></title> <style type="text/ ...