POJ 2513 Colored Sticks 字典树、并查集、欧拉通路
Description
Input
Output
Sample Input
blue red
red violet
cyan blue
blue magenta
magenta cyan
Sample Output
Possible
#include <stdio.h>
#include <iostream>
using namespace std; int id = , par[], degree[] = {}; int find_set(int x)
{
return par[x] != x ? par[x] = find_set(par[x]) : x;
} struct Trie_node
{
int flag, id;
struct Trie_node *next[];
Trie_node()
{
for(int i = ; i < ; i++)
next[i] = NULL;
flag = ;
}
}; int trie_insert(struct Trie_node *p, char s[])
{
for(int i = ; s[i]; i++)
{
if(p->next[s[i]-'a'] == NULL)
p->next[s[i]-'a'] = new Trie_node;
p = p->next[s[i]-'a'];
}
if(p->flag != )
{
p->flag = ;
p->id = ++id;
}
return p->id;
} bool check()
{
int x = find_set();
for(int i = ; i <= id; i++)
{
if(find_set(i) != x)
return ;
}
int degree_odd = ;
for(int i = ; i <= id; i++)
{
if(degree[i] & )
degree_odd++;
}
return (degree_odd == || degree_odd > ) ? : ;
} int main()
{
for(int i = ; i <= ; i++)
par[i] = i;
struct Trie_node *root = new Trie_node;
char s1[], s2[];
while(scanf("%s %s", s1, s2) != EOF)
{
int x = trie_insert(root, s1);
int y = trie_insert(root, s2);
degree[x]++;
degree[y]++;
int fx = find_set(x);
int fy = find_set(y);
if(fx != fy)
par[fx] = fy;
}
printf("%s\n", check() ? "Possible" : "Impossible");
return ;
}
POJ 2513 Colored Sticks 字典树、并查集、欧拉通路的更多相关文章
- POJ 2513 Colored Sticks (离散化+并查集+欧拉通路)
下面两个写得很清楚了,就不在赘述. http://blog.sina.com.cn/s/blog_5cd4cccf0100apd1.htmlhttp://www.cnblogs.com/lyy2890 ...
- poj 2513 Colored Sticks (trie树+并查集+欧拉路)
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 40043 Accepted: 10406 ...
- POJ 2513 Colored Sticks (欧拉回路+并查集+字典树)
题目链接 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with ...
- poj2513 Colored Sticks —— 字典树 + 并查集 + 欧拉回路
题目链接:http://poj.org/problem?id=2513 题解:通过这题了解了字典树.用字典树存储颜色,并给颜色编上序号.这题为典型的欧拉回路问题:将每种颜色当成一个点.首先通过并查集判 ...
- poj 2513 Colored Sticks( 字典树哈希+ 欧拉回路 + 并查集)
题目:http://poj.org/problem?id=2513 参考博客:http://blog.csdn.net/lyy289065406/article/details/6647445 htt ...
- poj 2513 Colored Sticks trie树+欧拉图+并查集
点击打开链接 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27955 Accepted ...
- poj 2513 Colored Sticks (trie 树)
链接:poj 2513 题意:给定一些木棒.木棒两端都涂上颜色,不同木棒相接的一边必须是 同样的颜色.求能否将木棒首尾相接.连成一条直线. 分析:能够用欧拉路的思想来解,将木棒的每一端都看成一个结点 ...
- Play on Words HDU - 1116 (并查集 + 欧拉通路)
Play on Words HDU - 1116 Some of the secret doors contain a very interesting word puzzle. The team o ...
- [欧拉] poj 2513 Colored Sticks
主题链接: http://poj.org/problem? id=2513 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Tota ...
随机推荐
- 如何解决因为找不到Notepad++的安装路径而导致的不能更新CS-Script的问题
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:如何解决因为找不到Notepad++的安装路径而导致的不能更新CS-Script的问题.
- Guava库
Guava是一个非常棒的库,它就是Java标准库"所缺失的那部分",是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, ...
- foreach -begin -process -end
gc d:\vm.txt|foreach -begin {write-host "It's beginning."} -process {$_ +"aa"} ...
- WPF组件开发
在做组件之前,为了适应框架,我们需要有一个基类,并将这个基类打包成一个模板,让大部分组件去使用这个模板. 组件的基类就不多讲了,上篇文章中已经说过了.这是地址: http://www.cnblogs. ...
- Linux学习笔记总结--ssh认证登录
原理简介 SSH证书认证登录的基础是一对唯一匹配密钥: 私钥(private key)和公钥(public key).公钥用于对数据进行加密,而且只能用于加密.而私钥只能对使用所匹配的公钥,所加密过的 ...
- OKHttp的简单使用
一方面,最近关于OKHttp的讨论甚嚣尘上,另一方面,我最近也更新了android6.0,发现在6.0中HttpClient不能使用了,于是决定抽时间也看一下OKHttp,总结了一点东西,与大家分享. ...
- PHP中的cookie创建取回删除;
<?php $expire=time()+3600;//设置过期cookie时间 setcookie('yaoyuan',"webyaoyuan",$expire);//se ...
- centos 安装mysqldb 记录
vim setup_pofix.py #修改mysql_config路径 <pre> ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /us ...
- sql if
SELECT a.id, a.EduSiteNo, a.EduSiteName, a.SchoolId, a.LinkMan, a.Tel, a.Mobile, a.Fax, a.Address, C ...
- 个人博客设计:创建Sql数据库操作类。
整体的博客框架如下 数据库操作java类如下 package com.yxq.dao; import java.sql.Connection; import java.sql.DriverManage ...