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 ...
随机推荐
- PHP ServerPush <转>
http://www.cnblogs.com/hnrainll/archive/2013/05/07/3064874.html
- struts2常用的常量constant(转)
原文地址:http://blog.csdn.net/wfcaven/article/details/5937548 常用的常量配置 struts.serve.static.browserCache ...
- 中国剩余定理模板poj1006
#include <cstdio> #include <iostream> #include <cstring> #include <cmath> #i ...
- ProgressBar 基本介绍
简介 ProgressBar 继承自View,用于在界面上显示一个进度指示的界面. 1.ProgressBar有两个进度,一个是android:progress,另一个是android:seconda ...
- cogs 53 多人背包
/* 要求每个最优 即累加前k优解 注意不用去重 */ #include<iostream> #include<cstdio> #include<cstring> ...
- JS的replace()的应用
替换字符串中的空格 /\s/ig 例如: var pro="ssss ssss sss ddd ss" var protext = pro.replace(/\s/ig,&qu ...
- YII数据库增删查改操作
初学YII, 整理了一些YII数据库的相关操作, 共同学习,共同进步. 一.查询数据集合 //1.该方法是根据一个条件查询一个集合 $admin=Admin::model()->findAll ...
- 对SQL Server SQL语句进行优化的10个原则
1.使用索引来更快地遍历表. 缺省情况下建立的索引是非群集索引,但有时它并不是最佳的.在非群集索引下,数据在物理上随机存放在数据页上.合理的索引设计要建立在对各种查询的分析和预测上.一般来说:①.有大 ...
- javascript基础学习(十三)
javascript之文档对象 学习要点: 文档对象 文档对象的应用 一.文档对象 Document对象是代表一个浏览器窗口或框架中的显示HTML文件的对象.javascript会为每个HTML文档自 ...
- ‘for’ loop initial declarations are only allowed in C99 mode
#include <stdio.h>int main(){ for(int i=0;i<10;i++){ printf("\n%d",i); } return 0 ...