poj 2513 Colored Sticks (trie 树)
题意:给定一些木棒。木棒两端都涂上颜色,不同木棒相接的一边必须是
同样的颜色。求能否将木棒首尾相接。连成一条直线.
分析:能够用欧拉路的思想来解,将木棒的每一端都看成一个结点
由图论知识能够知道,无向图存在欧拉路的充要条件为:
① 图是连通的。
② 全部节点的度为偶数,或者有且仅仅有两个度为奇数的结点。
图的连通性能够用并查集,由于数据比較大,所以用并查集时要压缩路径,
全部节点的度(入度和出度的和)用数组记录就好
可是25w个木棒,有50w个结点,要怎么存呢,假设用数组,每次得查找,
效率特别低,这样就能够用trie树存储数据,用空间换取时间
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define M 500000
typedef struct stu
{
int id,flag; //id为结点的编号。flag标记改颜色是否存在
struct stu *next[26];
}node;
int num=1,f[M+5],d[M+5];
node* creat_node() //创建结点并初始化
{
node *p=(node*)malloc(sizeof(node));
p->id=p->flag=0;
memset(p->next,0,sizeof(p->next));
return p;
}
int trie_insert(node *p,char *s) //插入颜色结点。并返回其编号
{
int i;
while(*s!='\0'){
i=*s-'a';
if(p->next[i]==NULL)
p->next[i]=creat_node();
p=p->next[i];
s++;
}
if(!p->flag){
p->flag=1;
p->id=num++;
}
return p->id;
}
int find(int x) //并查集查找,并压缩路径
{
if(x!=f[x])
f[x]=find(f[x]);
return f[x];
}
void mix(int a,int b) //并查集的合并
{
a=find(a);
b=find(b);
if(a!=b)
f[a]=b;
}
int main()
{
int i,j,n,m,flag=1;
node *root=NULL;
char s1[15],s2[15];
root=creat_node(); //创建根结点
for(i=1;i<=M;i++){ //父节点以及度的初始化
f[i]=i;
d[i]=0;
}
while(scanf("%s%s",s1,s2)!=EOF){
i=trie_insert(root,s1);
j=trie_insert(root,s2);
mix(i,j);
d[i]++;
d[j]++;
}
m=n=0;
for(i=1;i<num&&flag;i++){
if(d[i]%2)
n++; //记录度为奇数的结点
if(n>2)
flag=0;
if(f[i]==i) //记录公共祖先结点的个数
m++;
if(m>1)
flag=0;
}
if(flag)
printf("Possible\n");
else
printf("Impossible\n");
return 0;
}
poj 2513 Colored Sticks (trie 树)的更多相关文章
- poj 2513 Colored Sticks trie树+欧拉图+并查集
点击打开链接 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27955 Accepted ...
- poj 2513 Colored Sticks (trie树+并查集+欧拉路)
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 40043 Accepted: 10406 ...
- poj 2513 Colored Sticks( 字典树哈希+ 欧拉回路 + 并查集)
题目:http://poj.org/problem?id=2513 参考博客:http://blog.csdn.net/lyy289065406/article/details/6647445 htt ...
- 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(欧拉路径+并检查集合+特里)
题目链接:poj 2513 Colored Sticks 题目大意:有N个木棍,每根木棍两端被涂上颜色.如今给定每一个木棍两端的颜色.不同木棍之间拼接须要颜色同样的 端才干够.问最后是否能将N个木棍拼 ...
- [欧拉] poj 2513 Colored Sticks
主题链接: http://poj.org/problem? id=2513 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Tota ...
- POJ 2513 Colored Sticks (欧拉回路 + 字典树 +并查集)
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27097 Accepted: 7175 ...
- 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
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 28036 Accepted: 7428 ...
随机推荐
- javascript闭包特性
http://www.cnblogs.com/rubylouvre/archive/2009/07/24/1530074.html var name = "The Window"; ...
- [置顶] 如何把你的笔记本电脑变成一个Wi-Fi路由器在Windows 7 & 8?
翻译自:http://www.hakanakdag.net/windows/how-to-create-wireless-ad-hoc-internet-connection-in-windows-8 ...
- c++ 17介绍
作者:hearts zh链接:https://www.zhihu.com/question/32222337/answer/55238928来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商 ...
- C语言的本质(15)——C语言的函数接口入门
C语言的本质(15)--C语言的函数接口 函数的调用者和其实现者之间存在一个协议,在调用函数之前,调用者要为实现者提供某些条件,在函数返回时,实现者完成调用者需要的功能. 函数接口通过函数名,参数和返 ...
- poj 1328 Radar Installation_贪心
翻出一年多前的代码看,发现以前的代码风格很糟糕 题意:给你n个点 m为圆的半径,问需要多少个圆能把全部点圈到 #include <iostream> #include <algori ...
- Multiply Strings 大数相乘 java
先贴上代码 public String multiply(String num1, String num2) { String str = ""; StringBuffer sb ...
- JSP指令 include 和forward
包括指令:include 静态包括 <%@include file="要包括的文件路径" %> 先将所包括 ...
- ios 第三方qq授权登陆,第一次登陆后,再次登陆,失效
这问题找了非常久.最后跟客服联系到.等授权成功后要对 _tencentOAuth 对象释放
- 【单点更新,区间查询,线段树】【HDU1166】【敌兵布阵】
线段树要捡回来学了 才知道以前抄的模板就是杭电传奇学长写的,写起来更有激情了: 一点注意: 单点更新完后记得pushup(),向上更新信息 以下是对线段树的理解 线段树的节点代表一段线段,节点编号没有 ...
- mvn profile 深层次目录打参数核心配置
<build> <resources> <resource> <directory>src/main/resources</directory&g ...