POJ 2513 Colored Sticks 解题报告
第一次接触欧拉回路。虽然在离散数学里学过,敲代码还是第一次。
本题是说端点颜色相同的两根木棒可连接,能否将所有的木棒连成一条直线。
将颜色视为节点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 解题报告的更多相关文章
- poj 2513 Colored Sticks(欧拉路径+并检查集合+特里)
题目链接:poj 2513 Colored Sticks 题目大意:有N个木棍,每根木棍两端被涂上颜色.如今给定每一个木棍两端的颜色.不同木棍之间拼接须要颜色同样的 端才干够.问最后是否能将N个木棍拼 ...
- [欧拉] poj 2513 Colored Sticks
主题链接: http://poj.org/problem? id=2513 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Tota ...
- poj 2513 Colored Sticks trie树+欧拉图+并查集
点击打开链接 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27955 Accepted ...
- POJ 2513 Colored Sticks
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 28036 Accepted: 7428 ...
- poj 2513 Colored Sticks (trie 树)
链接:poj 2513 题意:给定一些木棒.木棒两端都涂上颜色,不同木棒相接的一边必须是 同样的颜色.求能否将木棒首尾相接.连成一条直线. 分析:能够用欧拉路的思想来解,将木棒的每一端都看成一个结点 ...
- POJ 2513 Colored Sticks (欧拉回路 + 字典树 +并查集)
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27097 Accepted: 7175 ...
- poj 2513 Colored Sticks (trie树+并查集+欧拉路)
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 40043 Accepted: 10406 ...
- POJ 2513 - Colored Sticks - [欧拉路][图的连通性][字典树]
题目链接: http://poj.org/problem?id=2513 http://bailian.openjudge.cn/practice/2513?lang=en_US Time Limit ...
- POJ 2513 Colored Sticks (欧拉回路+并查集+字典树)
题目链接 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with ...
随机推荐
- static的应用以及静态与非静态的区别
先前看到一个技术大牛写了一个关于静态成员与非静态成员,静态方法和非静态方法的各自区别,觉得挺好的,在这里写一个小程序来说明这些区别. package com.liaojianya.chapter5; ...
- Unity中使物体自动寻路的方法
在做一个FPS游戏时,需要敌方自动找到玩家方位并向玩家移动,在查找资料(并走了不少坑)后,我试了三个方法,经测试,这三个方法都能实现自动寻路功能. 方法一:使用Mathf.Lerp()方法 代码很简单 ...
- Basic MSI silent install
Articles and post about silent install for Basic MSI, InstallScript, InstallScript MSI: Silent-mode ...
- MySQL生产库主从重新同步操作注意事项
因为一些原因,我们会遇到生产主从库重新同步的时候.重新同步MYSQL主从的时候有有一些注意的地方. 从库还原前一定要记得reset,因为重启mysql并不影响复制进程,如果忘记reset,会导致你一边 ...
- C 字符/字符串常用函数
string.h中常用函数 char * strchr(char * str ,char ch); 从字符串str中查找首次出现字符ch的位置,若存在返回查找后的地址,若不存在则返回NULL void ...
- python mysqldb连接数据库
今天无事想弄下python做个gui开发,最近发布的是python 3k,用到了数据库,通过搜索发现有一个mysqldb这样的控件,可以使用,就去官方看了下结果,没有2.6以上的版本 没办法就下了一个 ...
- Cakephp 创建无模型的Controller
控制器(Controller)如果没有特定的表/模型关联的话,哪怕建测试都会出错,但你可以加一行到控制器(Controller)里就好了public $uses=array(); 或者 public ...
- Oracle wrap 测试的一些细节问题
今天在做 wrap 的测试实验的时候,出现一个很奇怪的现象,就是加密不成功.具体表现为:1.加密后的文件大小为0kb. 2.加密后的文件仍然可视. 具体测试步骤如下: D:\Just4work\som ...
- stream的Read、Write方法实例
, bytes.Length)) > ) , readBytes-);//8为偏移量,10为数量 } } ...
- Logback 简单使用
1.Logback为取代log4j而生 Logback是由log4j创始人Ceki Gülcü设计的又一个开源日志组件.logback当前分成三个模块:logback-core,logback- cl ...