链接: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. linux脚本之简单实例

    利用脚本计算10的阶乘 简单说明一下: #!/bin/bash说明该shell使用的bash shell程序.这一句不可少for i in `seq 1 10`还可以写成for i in 1 2 3 ...

  2. POJ 1724 最短路费用限制

    迪杰斯塔拉裸题 最大花费 n个点 m条有向边 起点终点 路径长度 路径花费 问:在花费限制下,最短路径的长度 #include <iostream> #include <string ...

  3. awk(流程控制、内置变量、内置函数、数组)

    摘自:http://bbs.51cto.com/thread-883948-1-1.html awk(流程控制.内置变量.内置函数.数组) ... 参考其他的资料,给大家看看.一.awk流程控制语句 ...

  4. qq视频api代码

    <!--视频容器--> <div id="mod_player"></div>   <!--腾讯视频代码开始--> <scri ...

  5. Linux多线程编程小结

     Linux多线程编程小结 前一段时间由于开题的事情一直耽搁了我搞Linux的进度,搞的我之前学的东西都遗忘了,非常烦躁的说,如今抽个时间把之前所学的做个小节.文章内容主要总结于<Linux程序 ...

  6. mobilebone.js 移动web APP单页切换骨架

    轻便体积小 原生无依赖 插件可扩展 设计无限制 动效可定制 动静两相宜 能进亦能退 桌面也兼修 一句话功能简介跟传统网页浏览的差别仅仅在于无刷新! 例如,我们浏览首页,首页上有个如下HTML链接: & ...

  7. ParNew收集器

    ParNew收集器其实就是Serial收集器的多线程版本,除了使用多条线程进行垃圾收集之外,其余行为包括Serial收集器可用的所有控制参数,其中Par是Paralle简写l 并行(Parallel) ...

  8. JqueryUI-2

    本文在于巩固基础 jQuery UI Widgets jQuery UI Widgets控件-Accordion <!DOCTYPE html> <html> <head ...

  9. vba考勤处理

    sheet1 格式 Sub 统计人数() 'A为号码C姓名 D时间 Dim j As Integer j = 1 For i = 2 To 100000 Step 1 If Range("a ...

  10. UVA 806 Spatial Structures

    题意: 如果某一大区域所有色块颜色是相同的,那么这一个大区域就算作一块,如果不同,则将其划分成四个小区域,然后重复上述步骤递归进行直到所有区域的颜色相同为止.然后根据上面划分的区域建树,小区域作为大区 ...