POJ2513 Colored Sticks(欧拉)
题目链接。
题目大意:
给很多木棍,两端被涂了颜色。任意两根木棍的相同颜色处可以拼接在一起,问有没有可能将所有的木棍都连起来,成一条直线?
分析:
考点,欧拉道路。
将一根木棍看成一条边,两端的颜色看成两个点,问题成了,能否从无向图的一个结点出发走出一条道路,每条边恰好经过一次。
求法:
如果一个无向图是连通的,且最多有两个奇点(奇点指的是度数是奇数的点),则一定存在欧拉道路。
是否连通可以通过并查集来求。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
using namespace std; const int maxn = ++; int deg[maxn], cm, cn, p[maxn]; struct node {
struct node *next[];
int num;
}mem[*+]; node *create() {
for(int i=; i<; i++) {
mem[cm].next[i] = NULL;
}
mem[cm].num = ;
return &mem[cm++];
} int get_num(node *&root, char s[]) {
int len = strlen(s);
node *p; if(!root) root = create(); p = root;
for(int i=; i<len; i++) {
int k = s[i] - 'a';
if(!p->next[k]) p->next[k] = create();
p = p->next[k];
} if(!p->num) p->num = cn++;
return p->num;
} int find(int x) { return p[x] == x ? p[x] : (p[x] = find(p[x])); } void Union(int x, int y) {
x = find(x), y = find(y);
if(x != y) p[x] = y;
} int main(){
int u, v;
char s1[], s2[];
node *root = NULL; cm = , cn = ; memset(deg, , sizeof(deg)); for(int i=; i<maxn; i++) p[i] = i; while(scanf("%s %s", s1, s2) == ) {
u = get_num(root, s1);
v = get_num(root, s2); deg[u]++;
deg[v]++; Union(u, v);
} int pn= ;
bool flag = true; int e = find(); for(int i=; i<cn; i++) { //从1开始编号
if(deg[i] % != ) {
pn++;
} if(pn > || find(i) != e) {
flag = false; break;
}
} if(flag) printf("Possible\n");
else printf("Impossible\n"); return ;
}
POJ2513 Colored Sticks(欧拉)的更多相关文章
- POJ2513——Colored Sticks(Trie树+欧拉回路+并查集)
Colored Sticks DescriptionYou are given a bunch of wooden sticks. Each endpoint of each stick is col ...
- POJ2513:Colored Sticks(字典树+欧拉路径+并查集)
http://poj.org/problem?id=2513 Description You are given a bunch of wooden sticks. Each endpoint of ...
- POJ 2513 - Colored Sticks - [欧拉路][图的连通性][字典树]
题目链接: http://poj.org/problem?id=2513 http://bailian.openjudge.cn/practice/2513?lang=en_US Time Limit ...
- POJ2513 Colored Sticks(Trie+欧拉回路)
Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some ...
- poj2513 Colored Sticks —— 字典树 + 并查集 + 欧拉回路
题目链接:http://poj.org/problem?id=2513 题解:通过这题了解了字典树.用字典树存储颜色,并给颜色编上序号.这题为典型的欧拉回路问题:将每种颜色当成一个点.首先通过并查集判 ...
- [欧拉] poj 2513 Colored Sticks
主题链接: http://poj.org/problem? id=2513 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Tota ...
- POJ 2513 Colored Sticks (离散化+并查集+欧拉通路)
下面两个写得很清楚了,就不在赘述. http://blog.sina.com.cn/s/blog_5cd4cccf0100apd1.htmlhttp://www.cnblogs.com/lyy2890 ...
- POJ 2513 Colored Sticks(欧拉道路+字典树+并查集)
http://poj.org/problem?id=2513 题意: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 思路: 题目很明 ...
- poj 2513 Colored Sticks (trie树+并查集+欧拉路)
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 40043 Accepted: 10406 ...
随机推荐
- Project Euler problem 63
这题略水啊 首先观察一下. 10 ^ x次方肯定是x + 1位的 所以底数肯定小于10的 那么我们就枚举1~9为底数 然后枚举幂级数就行了,直至不满足题目中的条件即可break cnt = 0 for ...
- FACTORY设计模式【让吃货也能理解的程序】
一个人要有思想,技术再牛的人,如果没有一点点文化气氛,那么也是个码农,不能追到女朋友滴. 我这个人文化真心不多,但是比较喜欢读古诗文.虽然读完之后,记在脑海里的不多,不过,就讨一个喜欢[读]. Lee ...
- MYSQL 体系结构图-space结构图
- Qt 学习之路 :线程简介
现代的程序中,使用线程的概率应该大于进程.特别是在多核时代,随着 CPU 主频的提升,受制于发热量的限制,CPU 散热问题已经进入瓶颈,另辟蹊径地提高程序运行效率就是使用线程,充分利用多核的优势.有关 ...
- service redis does not support chkconfig的解决办法
原文链接: http://my.oschina.net/maczhao/blog/322931 问题解决办法如下: 必须把下面两行注释放在/etc/init.d/redis文件靠前的注释中: # ch ...
- Linux开发工具之Makefile(上)
二.makefile(上) 01.make工具 利用make工具可以自动完成编译工作.这些工作包括:如果修改了某几 个源文件,则只重装新编译这几个源文件:如果某个头文件被修改了,则 重新编译所有包 ...
- Python之路,Day22 - 网站用户访问质量分析监测分析项目开发
Python之路,Day22 - 网站用户访问质量分析监测分析项目开发 做此项目前请先阅读 http://3060674.blog.51cto.com/3050674/1439129 项目实战之 ...
- ListView滑动删除
本来准备在ListView的每个Item的布局上设置一个隐藏的Button,当滑动的时候显示.但是因为每次只要存在一个Button,发现每个Item上的Button相互间不好控制.所以决定继承List ...
- FineUI属性的简单总结
.PageManager控件— 页面级别的控制(包括主题.语言 等等) 覆盖web.config中自定义结点的配置 EnablePageLoading:是否启用页面的第一次加载提示,默认居中显示加载图 ...
- windows服务(Windows Installer问题,错误5:拒绝访问)
Windows Installer问题,错误5:拒绝访问 shillan,2006-11-03 09:40:38 现象: 使用MSI文件来安装的软件在安装和卸载时系统提示:“不能访问Windows I ...