链接:poj 2513

题意:给定一些木棒。木棒两端都涂上颜色,不同木棒相接的一边必须是

同样的颜色。求能否将木棒首尾相接。连成一条直线.

分析:能够用欧拉路的思想来解,将木棒的每一端都看成一个结点

由图论知识能够知道,无向图存在欧拉路的充要条件为:

①     图是连通的。

②     全部节点的度为偶数,或者有且仅仅有两个度为奇数的结点。

图的连通性能够用并查集,由于数据比較大,所以用并查集时要压缩路径,

全部节点的度(入度和出度的和)用数组记录就好

可是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 树)的更多相关文章

  1. poj 2513 Colored Sticks trie树+欧拉图+并查集

    点击打开链接 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27955   Accepted ...

  2. poj 2513 Colored Sticks (trie树+并查集+欧拉路)

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 40043   Accepted: 10406 ...

  3. poj 2513 Colored Sticks( 字典树哈希+ 欧拉回路 + 并查集)

    题目:http://poj.org/problem?id=2513 参考博客:http://blog.csdn.net/lyy289065406/article/details/6647445 htt ...

  4. POJ 2513 Colored Sticks 字典树、并查集、欧拉通路

    Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some ...

  5. poj 2513 Colored Sticks(欧拉路径+并检查集合+特里)

    题目链接:poj 2513 Colored Sticks 题目大意:有N个木棍,每根木棍两端被涂上颜色.如今给定每一个木棍两端的颜色.不同木棍之间拼接须要颜色同样的 端才干够.问最后是否能将N个木棍拼 ...

  6. [欧拉] poj 2513 Colored Sticks

    主题链接: http://poj.org/problem? id=2513 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Tota ...

  7. POJ 2513 Colored Sticks (欧拉回路 + 字典树 +并查集)

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27097   Accepted: 7175 ...

  8. POJ2513——Colored Sticks(Trie树+欧拉回路+并查集)

    Colored Sticks DescriptionYou are given a bunch of wooden sticks. Each endpoint of each stick is col ...

  9. POJ 2513 Colored Sticks

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 28036   Accepted: 7428 ...

随机推荐

  1. tomcat优化-有改protocol 和 缓存 集群方案

    tomcat优化 在线上环境中我们是采用了tomcat作为Web服务器,它的处理性能直接关系到用户体验,在平时的工作和学习中,归纳出以下七种调优经验. 1. 服务器资源 服务器所能提供CPU.内存.硬 ...

  2. 原生应用native、Web应用、混合应用hybrid:3者的优缺点解析

    最近原生应用.Web应用.混合应用的名字让我们听得比较熟悉了,现在我们就通过评析各种应用的优缺点来更进一步看看这三者的区别. 一. 原生应用: 你使用过微软PowerPoint 或者 Word吧?这些 ...

  3. jquery 中 (function( window, undefined ) {})(window)写法详解(转)

    最常见的闭包 (Closure) 范式大家都很熟悉了: 123 (function() {// ...})(); 很简单,大家都在用.但是,我们需要了解更多.首先,闭包是一个匿名函数 (Anonymo ...

  4. JavaEE Tutorials (25) - 使用Java EE拦截器

    25.1拦截器概述380 25.1.1拦截器类381 25.1.2拦截器生命周期381 25.1.3拦截器和CDI38125.2使用拦截器381 25.2.1拦截方法调用382 25.2.2拦截生命周 ...

  5. 【Java接口实现动态加载不同的类】

    public interface Person {       public double calcuMonthlySalary(double sal, int type);    }   publi ...

  6. 什么是dandy 风格_百度知道

    什么是dandy 风格_百度知道     什么是dandy 风格    2010-06-21 10:56 平ping123 | 分类:服装/首饰 | 浏览11257次     题谢谢     有没有比 ...

  7. Html代码seo优化最佳布局实例讲解

    搜索引擎对html代码是非常优化的,所以html的优化是做好推广的第一步.一个符合seo规则的代码大体如下界面所示. 1.<!–木庄网络博客–> 这个东西是些页面注释的,可以在这里加我的& ...

  8. uva 11524 - InCircle (二分法)

    题意:三角形ABC的内切圆把它的三边分别划分成 m1:n1,m2:n2 和 m3:n3 的比例.另外已知内切圆的半径 r ,求三角形ABC 的面积. #include<iostream> ...

  9. taobao面试要点

    第一: 其中有几个点必问,JVMGC深层机制.类加载,包括Tomcat和Jboss的.线程相关的如离线锁,互斥同步,java主线程和工作线程机制,concurrent包下的锁和sync关键字一些区别, ...

  10. Devexpress之barManager控件属性

    隐藏菜单栏左边的竖线和右边的箭头? 1.隐藏菜单栏上右边的箭头属性设置:OptionsBar=>>AllowQuickCustomization=False 2.隐藏菜单栏左边的竖线属性设 ...