题目链接

题目大意:

给很多木棍,两端被涂了颜色。任意两根木棍的相同颜色处可以拼接在一起,问有没有可能将所有的木棍都连起来,成一条直线?

分析:

考点,欧拉道路。

将一根木棍看成一条边,两端的颜色看成两个点,问题成了,能否从无向图的一个结点出发走出一条道路,每条边恰好经过一次。

求法:

如果一个无向图是连通的,且最多有两个奇点(奇点指的是度数是奇数的点),则一定存在欧拉道路。

是否连通可以通过并查集来求。

#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(欧拉)的更多相关文章

  1. POJ2513——Colored Sticks(Trie树+欧拉回路+并查集)

    Colored Sticks DescriptionYou are given a bunch of wooden sticks. Each endpoint of each stick is col ...

  2. POJ2513:Colored Sticks(字典树+欧拉路径+并查集)

    http://poj.org/problem?id=2513 Description You are given a bunch of wooden sticks. Each endpoint of ...

  3. POJ 2513 - Colored Sticks - [欧拉路][图的连通性][字典树]

    题目链接: http://poj.org/problem?id=2513 http://bailian.openjudge.cn/practice/2513?lang=en_US Time Limit ...

  4. POJ2513 Colored Sticks(Trie+欧拉回路)

    Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some ...

  5. poj2513 Colored Sticks —— 字典树 + 并查集 + 欧拉回路

    题目链接:http://poj.org/problem?id=2513 题解:通过这题了解了字典树.用字典树存储颜色,并给颜色编上序号.这题为典型的欧拉回路问题:将每种颜色当成一个点.首先通过并查集判 ...

  6. [欧拉] poj 2513 Colored Sticks

    主题链接: http://poj.org/problem? id=2513 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Tota ...

  7. POJ 2513 Colored Sticks (离散化+并查集+欧拉通路)

    下面两个写得很清楚了,就不在赘述. http://blog.sina.com.cn/s/blog_5cd4cccf0100apd1.htmlhttp://www.cnblogs.com/lyy2890 ...

  8. POJ 2513 Colored Sticks(欧拉道路+字典树+并查集)

    http://poj.org/problem?id=2513 题意: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 思路: 题目很明 ...

  9. poj 2513 Colored Sticks (trie树+并查集+欧拉路)

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 40043   Accepted: 10406 ...

随机推荐

  1. 技术型创业者easy遇到的三大问题

    关于创业这事儿.由于经历的事情实在是太多了,所以真是想到哪儿写到哪儿. 这一篇算是<杂记>的番外篇.我今天想说的东西太多了,实在是非常想和很多其它人讨论讨论关于创业的事情. 这样的感觉就像 ...

  2. 从客户端(content="<span class="Apple-s...")中检测到有潜在危险的 Request.Form 值。

    从客户端(content="<span class="Apple-s...")中检测到有潜在危险的 Request.Form 值. 说明: 请求验证过程检测到有潜在 ...

  3. C++对象模型学习笔记

    1. 全局变量是如何初始化的 //global var A a; int main() { cout<<a<<endl; ; } 如上述例子,全局变量a是在main()函数之前 ...

  4. C#自定义事件:属性改变引发事件示例

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  5. sublime 3 3083验证码

    Sublime Text 3注册码两枚: ----- BEGIN LICENSE ----- K- Single User License EA7E- 3A099EC1 C0B5C7C5 33EBF0 ...

  6. java 类处理工具

    public class ClassUtils { private static final Logger LOGGER = LoggerFactory.getLogger(ClassUtils.cl ...

  7. updatepanel的属性

    updatepanel的属性 1.childrenastriggers:内容模板内的子控件的回发是否更新本模板(和updatemode的conditional有关) 2.updatemode:内容模板 ...

  8. 解决 iOS View Controller Push/Pop 时的黑影

    那么如何解决这个问题呢? 实际上很简单,如果这个 ViewController 是在 TabBarViewController 的 NavigationController 上 Push/Pop 的, ...

  9. Web服务图片压缩,nginx+lua生成缩略图

    背景 目前而言,用移动端访问Web站点的用户越来越多,图片对流量的消耗是比较大的,之前一个用户用我们网站的app浏览的时候,2个小时耗去了2个G的流量,这是个很严重的问题,需要对图片进行压缩,减少对用 ...

  10. A simple stack

    // simple stack.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h"#include<iostream>using na ...