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

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

将颜色视为节点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. C++中map用法

    /************************************************************************** Map的特点: 1.存储Key-value对* ...

  2. 九度OJ 1513 二进制中1的个数

    题目地址:http://ac.jobdu.com/problem.php?pid=1513 题目描述: 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 输入: 输入可能包含多个测试样 ...

  3. Struts2文件下载浅析

    Struts2极大的简化了文件上传和下载,本文将主要介绍一下Struts2文件下载的实现1.功能主要是,在下载页面点击下载后,则下载相应的文件 2.代码部分jsp页面downloadPage:< ...

  4. Oracle 关于事物的描述

    事物在Oracle中的4种状态: commit--提交 rollback--全部回滚 savepoint name;--定义一个回滚到这里的点:例如:savepoint a; rollback to ...

  5. win7 64 安装Oracle 11G 、使用PLSQL进行连接 标准实践

    第一步: 安装oracle 服务,两个解压包,分别解压后 合并到一个文件夹,点击exe安装 (安装过程中如遇到PATH问题,直接忽略即可) 第二步:使用SQLPlus 测试是否成功 安装成功:CMD ...

  6. python with关键字学习

    1.with语句时用于对try except finally 的优化,让代码更加美观, 例如常用的开发文件的操作,用try except finally 实现: f=open('file_name', ...

  7. Nhibernate 一对多,多对一配置

    先来分析下问题,这里有两张表:Users(用户表) U和PersonalDynamic(用户动态表) PD,其中PD表的UserId对应U表的Id 如图: 现在映射这两张表: 如图: User.hbm ...

  8. python 读写文本文件

    本人最近新学python ,用到文本文件的读取,经过一番研究,从网上查找资料,经过测试,总结了一下读取文本文件的方法. 1.在读取文本文件的时无非有两种方法: a.f=open('filename', ...

  9. 纯CSS3代码实现表格奇偶行异色,鼠标悬浮变色

    1.首先会用到<tr></tr>元素两个伪类,nth-child()和hover. 然后需要注意的是伪类都是通过冒号引用的,不是点号,即tr:hover{} 其次,CSS代码中 ...

  10. Oracle数据库的下载和安装

    那天分享一下Oracle的下载和安装的过程,有需要的朋友可以借鉴参考一下.如有雷同不胜感激! 首先可以到Oracle的官网下载Oracle的最次年版本的Oracle数据库.一下是个人下载的数据库版本百 ...