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 ...
随机推荐
- Linux命令 - 删除文件(夹)
1.删除文件夹 rm –rf /var/test 将会删除/var/test目录以及其下的所有文件.文件夹 2.删除文件 rm -f /var/test/test.txt 将会强制删除/var/tes ...
- 【转】Android开发中的SQLite事务处理,即beginTransaction()方法
使用SQLiteDatabase的beginTransaction()方法可以开启一个事务,程序执行到endTransaction() 方法时会检查事务的标志是否为成功,如果程序执行到endTrans ...
- TCP/IP协议族-----10、搬家IP
- poj2239 Selecting Courses --- 二分图最大匹配
匈牙利算法模板题 有n门课程,每门课程可能有不同一时候间,不同一时候间的课程等价. 问不冲突的情况下最多能选多少门课. 建立二分图,一边顶点表示不同课程,还有一边表示课程的时间(hash一下). #i ...
- Android Bitmap开发之旅--基本操作
1 Bitmap加载方式 在介绍Bitmap--OOM 异常时,首先介绍一下Bitmap有哪几种加载方式.通常Bitmap的加载方式有Resource资源加载.本地(SDcard)加载.网络加载等加载 ...
- linux 下各errno的意义
strerror(errno):获取errno对应的错误 #include <string.h> /* for strerror */ #include <errno.h> # ...
- redis 记录
参考 : http://keenwon.com/1275.html http://blog.csdn.net/freebird_lb/article/details/7733970 http://w ...
- Rx RxJava【Operators】操作符
操作符分类 ReactiveX的每种编程语言的实现都实现了一组操作符的集合.不同的实现之间有很多重叠的部分,也有一些操作符只存在特定的实现中.每种实现都倾向于用那种编程语言中他们熟悉的上下文中相似的方 ...
- 2015 Multi-University Training Contest 5
#include <iostream> //1002 #include<set> #include<stdio.h> using namespace std; co ...
- Dedecms当前位置{dede:field name='position'/}修改
这个实在list_article.htm模板出现的,而这个模板通过loadtemplage等等一系列操作是调用的include 下的arc.archives.class.php $this->F ...