poj2513- Colored Sticks 字典树+欧拉通路判断
题目链接:http://poj.org/problem?id=2513
思路很容易想到就是判断欧拉通路
预处理时用字典树将每个单词和数字对应即可
刚开始在并查集处理的时候出错了
代码:
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
using namespace std;
int color;
#define maxn 26
#define MAX 500010
int parent[MAX];
int degree[MAX];
class Trie
{
public:
bool flag;
int id;
Trie *next[maxn];
};
Trie *root=new Trie;
void init()
{
memset(degree,,sizeof(degree));//存放各个点的度数
memset(parent,-,sizeof(parent));
Trie *q=root;//字典树初始化
for(int i=;i<maxn;i++)
q->next[i]=NULL;
q->flag=false;
q->id=;
}
/* 并查集构建并判断是否为连通图*/
int find(int x)
{
if(parent[x]==-) return x;
return parent[x]= find(parent[x]); } void Union(int u,int v)
{
int r1=find(u);
int r2=find(v); if(r1!=r2)
parent[r1]=r2;
}
int insert(char *s)
{
Trie *p =root; for(int i=; s[i]!='\0' ;i++)
{
int d=s[i]-'a'; if(p->next[d]==NULL)
{
Trie *temp=new Trie;
for(int j=;j<maxn;j++)
temp->next[j]=NULL;
temp->flag=;
temp->id=;
p->next[d]=temp;
}
p=p->next[d]; }
if(p->flag)
{
return p->id;
}
else
{
p->flag=;
p->id=color++;
return p->id;
} }
void del_trie(Trie * p)
{
for(int i=;i<maxn;i++)
if(p->next[i])
del_trie(p->next[i]); free(p);
}
int main()
{
char s1[],s2[];
init();
color=;
while(scanf("%s%s",s1,s2)!=EOF)
{
int num1=insert(s1);
int num2=insert(s2); degree[num1]++;
degree[num2]++;
Union(num1,num2); }
int cnt1=,cnt2=;
for(int i=;i<color;i++)//判断是佛否含欧拉通路
{
if(parent[i]==-) cnt1++;
if(degree[i]%==) cnt2++;
if(cnt1>) break;
if(cnt2>) break;
} if((cnt1== || cnt1==) &&(cnt2== || cnt2==))
printf("Possible\n");
else printf("Impossible\n");
// del_trie(root);
return ; }
poj2513- Colored Sticks 字典树+欧拉通路判断的更多相关文章
- poj 2513 连接火柴 字典树+欧拉通路 好题
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27134 Accepted: 7186 ...
- POJ2513:Colored Sticks(字典树+欧拉路径+并查集)
http://poj.org/problem?id=2513 Description You are given a bunch of wooden sticks. Each endpoint of ...
- poj2513 Colored Sticks —— 字典树 + 并查集 + 欧拉回路
题目链接:http://poj.org/problem?id=2513 题解:通过这题了解了字典树.用字典树存储颜色,并给颜色编上序号.这题为典型的欧拉回路问题:将每种颜色当成一个点.首先通过并查集判 ...
- hdu1116有向图判断欧拉通路判断
Play on Words Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- 欧拉回路&欧拉通路判断
欧拉回路:图G,若存在一条路,经过G中每条边有且仅有一次,称这条路为欧拉路,如果存在一条回路经过G每条边有且仅有一次, 称这条回路为欧拉回路.具有欧拉回路的图成为欧拉图. 判断欧拉通路是否存在的方法 ...
- Colored Sticks (字典树哈希+并查集+欧拉路)
Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27704 Accepted: 7336 Description You ...
- POJ2513——Colored Sticks(Trie树+欧拉回路+并查集)
Colored Sticks DescriptionYou are given a bunch of wooden sticks. Each endpoint of each stick is col ...
- POJ 2513 Colored Sticks 字典树、并查集、欧拉通路
Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some ...
- poj 2513 Colored Sticks( 字典树哈希+ 欧拉回路 + 并查集)
题目:http://poj.org/problem?id=2513 参考博客:http://blog.csdn.net/lyy289065406/article/details/6647445 htt ...
随机推荐
- centos登录时一闪而过,显示module is unknown
原因是修改了在/etc/pam.d/login中加入了: session required /lib/security/pam_limits.so session required pam_limit ...
- 在NAS设备上用NFS服务为RAC数据库和集群件存储oracle文件时的mount选项
今天在家折腾自己的小实验室,把自己NAS上的一个目录用NFS挂载到一套11g RAC的实验环境中. 当我在备份数据库到NAS上时,发现一个奇怪的问题,同样的目录下,默认backup 备份集的情况,备份 ...
- XAMPP安装报错及解决
FROM:http://www.zeeronsolutions.com/installing-xampp-on-windows-7-user-account-control-uac-warning-m ...
- ubuntu-terminal快捷键
常用快捷键功能:Tab 自动补全 Ctrl+a 光标移动到开始位置 Ctrl+e 光标移动到最末尾 Ctrl+k 删除此处至末尾的所有内容 Ctrl+u 删除此处至开始的所有内容 Ctrl+d 删除当 ...
- 基于CDIF实现的——API在线自动化测试
传统的测试工具在测试一个API的时候,必须手动填写这个API所需要接收的所有信息,比如一个查询航班动态的API,他接收两个输入字段,一个叫flight, 一个叫date,那么测试这个API的用户,需要 ...
- PMBOK 和 PRINCE2的技术不同的地方是什么
首先,PMBOK是一个框架指导,PRINCE2是一种实现方法. PMBOK是一种建议及最佳实践的集锦.PMBOK包含项目管理的工具和技术并且是一个指导,告诉我们如何做事情,在一种环境中怎样处理问题;而 ...
- 【WCF】错误处理(三):错误协定
最近折腾换电脑的事,博客就更新慢了点.好,不废话,直入正题. 前面老周介绍过,SOAP消息中的错误信息是用一个 Fault 元素来包装的,前面老周也讲了其中的 FaultCode 元素,即可以对错误信 ...
- 2.熟悉Java基本类库系列——Java IO 类库
Java中常用的IO操作基本上可以分为四大部分,分别是:File类操作.RandomAccessFile类操作.字节流操作.字符流操作.只要熟练掌握了本文中所列举的所有例子,基本上对于Java的IO流 ...
- 自动生成数学题型三 (框架Struts2)题型如 a+b=c(a、b、c都为分数)
1. 约分分数 1.1 保留质数 /** * 将数值放入到fraction数组中 * @param fen 简要放的 int类型数值 */ public void fenshu(int fen) { ...
- loopj.com android-async-http
loopj.com android-async-http Android异步Http客户端 用于Android的基于回调的Http客户端库 下载版本1.4.9(最新) 或者在github上fork ...