第一次接触欧拉回路。虽然在离散数学里学过,敲代码还是第一次。

本题是说端点颜色相同的两根木棒可连接,能否将所有的木棒连成一条直线。

将颜色视为节点v,将木棒视为边e,构成图G。如果能找到一条一笔画的路经过所有边,那么便符合条件。也就是判断是否是欧拉回路。

欧拉回路的条件是:

(1) 图是连通的。

(2) 度数为基数的点的个数是两个,或者不存在。

连通可以通过用并查集判断。度数可以建一个数组保存当前点的度数。

值得注意的是有全空的数据,此时应该输出Possible。这点坑了我一下,在Discuss里发现有人提出了,改一下就A了。

#include <cstdio>
#include <cstring> const int maxn=;
int root[maxn];
int degree[maxn]; struct Node
{
int next[];
int id;
} dic[];
int index=;
int id=; int find(int x)
{
return root[x]?root[x]=find(root[x]):x;
} bool union_set(int a,int b)
{
a=find(a);
b=find(b);
if(a==b)
return false;
root[b]=a;
return true;
} int insert(char* s)
{
int now=;
int len=strlen(s);
for(int i=;i<len;i++)
{
int next=s[i]-'a';
if(dic[now].next[next]==)
dic[now].next[next]=++index;
now=dic[now].next[next];
}
if(dic[now].id==)
dic[now].id=++id;
return dic[now].id;
} int main()
{
// freopen("in.txt","r",stdin);
int num=;
char s1[],s2[];
while(~scanf("%s%s",s1,s2))
{
int a=insert(s1);
int b=insert(s2);
degree[a]++;
degree[b]++;
if(union_set(a,b))
num++;
}
if(num==id || id==)
{
int count=;
for(int i=;i<=id;i++) if(degree[i]&)
{
count++;
if(count==)
break;
}
if(count== || count==)
{
printf("Possible\n");
return ;
}
}
printf("Impossible\n");
}

POJ 2513 Colored Sticks 解题报告的更多相关文章

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

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

  2. [欧拉] poj 2513 Colored Sticks

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

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

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

  4. POJ 2513 Colored Sticks

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

  5. poj 2513 Colored Sticks (trie 树)

    链接:poj 2513 题意:给定一些木棒.木棒两端都涂上颜色,不同木棒相接的一边必须是 同样的颜色.求能否将木棒首尾相接.连成一条直线. 分析:能够用欧拉路的思想来解,将木棒的每一端都看成一个结点 ...

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

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

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

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

  8. POJ 2513 - Colored Sticks - [欧拉路][图的连通性][字典树]

    题目链接: http://poj.org/problem?id=2513 http://bailian.openjudge.cn/practice/2513?lang=en_US Time Limit ...

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

    题目链接 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with ...

随机推荐

  1. 九度OJ 1209 最小邮票数 -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1209 题目描述: 有若干张邮票,要求从中选取最少的邮票张数凑成一个给定的总值.     如,有1分,3分,3分,3 ...

  2. 一个简单的Inno Setup例子

    ; 脚本由 Inno Setup 脚本向导 生成! ; 有关创建 Inno Setup 脚本文件的详细资料请查阅帮助文档! [Setup] ; 注: AppId的值为单独标识该应用程序. ; 不要为其 ...

  3. Java Web开发中的名词解释

    1.JVM Java虚拟机,class文件的运行时环境,就好比软件运行在操作系统一样,java要运行在JVM中才行,这也是Java之所以支持扩平台的基础. 2.Servlet/JSP 是满足一定接口需 ...

  4. 重力加速度陀螺仪传感器MPU-6050(一)

    MPU-60X0 对陀螺仪和加速度计分别用了三个16 位的ADC,将其测量的模拟量转化 为可输出的数字量.为了精确跟踪快速和慢速的运动,传感器的测量范围都是用户可控的,陀螺仪可测范围为±250,±50 ...

  5. JQuery 绑定事件时传递参数的实现方法

    如题,比如我想在$(":text").bind("keyup",funcionName);将当前的文本框作为参数传递给 functionName所代表的函数,应 ...

  6. 虚拟机添加磁盘LVM分区

    参考博客:http://kimjinlsgd.blog.51cto.com/1918030/932210 一.查看磁盘情况 新添加一块磁盘. [root@VMhost /]# fdisk -l Dis ...

  7. RepeatedDNASequences BestTime_to_Buy_and_SellStockIV

    /** * @Author: weblee * @Email: likaiweb@163.com * @Blog: http://www.cnblogs.com/lkzf/ * @Time: 2015 ...

  8. 在C#中IEnumerable与IEnumerator

    对于很多刚开始学习C#同学来说经常会遇到IEnumerable这个关键字,enumerate在字典里的解释是列举,枚举,因此可想而知这个关键字肯定是和列举数据有关的操作. public interfa ...

  9. uCGUI窗口重绘代码分析

    一.概述 µC/GUI的窗口重绘是学习者理解窗口工作原理和应用窗口操作的重点.µC/GUI的窗口重绘引入了回调机制,回调机制可以实现图形系统调用用户的代码,由于图形系统使用了剪切算法,使得屏幕重绘的效 ...

  10. mysql function 与 procedure

    Mysql 的 function 和 procedure 有啥区别呢 ? 网上搜索后说 function 有返回值, procedure 无返回值. 1.return  从function 的语法角度 ...