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

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

将颜色视为节点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. [leetcode] 406. Queue Reconstruction by Height

    https://leetcode.com/contest/6/problems/queue-reconstruction-by-height/ 分析:每个表示成(a,b)的形式,其实找第一个,就是b为 ...

  2. ssh: connect to host xxx.xxx.xxx.xxx port 22: Connection refused

    工具/原料:Ubuntu 在采用scp在不同机器之间进行文件拷贝时出现标题所示的错误,原因可能是: 1.sshd 未启动 2.sshd 未安装 3.防火墙 4需重新启动ssh 服务 查看sshd进程方 ...

  3. MySql启动提示:The server quit without updating PID file(…)失败

    在网上找了很多 1.可能是/usr/local/mysql/data/rekfan.pid文件没有写的权限解决方法 :给予权限,执行 “chown -R mysql:mysql /var/data”  ...

  4. linux RedHat6.4下nginx安装

    安装rpm 检测是否有已安装rpm包: rpm–qa | grep pcre rpm–qa | grep zlib rpm–qa | grep openssl 若没有则需安装(这些包可以在redhat ...

  5. phantomjs 自动化测试

    最近网站的质量检查越来越严格,原来开发过程中很多隐蔽的问题,逐渐暴露出来,为提高前端的工作效率,就想到是不是可以在开发过程中能自动的对页面的中一些规范化的东西进行自动监测,这个就可以省去不少麻烦. 整 ...

  6. linux系统 备份与还原

    linux 系统备份与还原备份系统:1.成为 root 用户: su root2.进入根目录: cd /3.用tar命令打包压缩:tar cvpjf 压缩包名.tar.bz2 --exclude=/压 ...

  7. 关于Mysql数据库longblob格式数据的插入com.mysql.jdbc.PreparedStatement.setBinaryStream(ILjava/io/InputStream;J)V问题分析

    当数据库字段为blob类型时 ,我们如果使用PreparedStatement中的setBinaryStream(int,InputStream,int)方法需要注意 在向blob字段类型中插入数据时 ...

  8. Python创建list

    Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 比如,列出班里所有同学的名字,就可以用一个list表示: >>> ['Mic ...

  9. HDU 1560 DNA sequence (IDA* 迭代加深 搜索)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1560 BFS题解:http://www.cnblogs.com/crazyapple/p/321810 ...

  10. 如何在一整张背景图中,加隐形的a标签

    很常见的一个需求,就上图每个国家图标都得加上各自指定的a标签 这时,我们就可以去加上隐藏且定位准确的几个a标签 这个时候,主要用到的就是text-indent和overflow 这两个属性的配合.te ...