题意:给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的。

 

无向图存在欧拉路的充要条件为:

①     图是连通的;

②     所有节点的度为偶数,或者有且只有两个度为奇数的节点。

图的连通可以利用并查集去判断。

度数的统计比较容易。

代码实现

//第一次用指针写trie,本来是用二维数组,发现数组开不下,只好删删改改,改成指针
//做这道题,知道了欧拉回路判定,还有用指针写trie
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 500010;
char s1[15], s2[15];
int fa[N], u, v, in[N], color =1; struct Trie
{
Trie *ch[26];
int val;
void init()
{
for(int i=0; i<26; i++) ch[i] = NULL;
val = 0;
} Trie()
{
for(int i=0; i<26; i++) ch[i] = NULL;
val = 0;
} int idx(char c){return c-'a'; } int ins_find(char *s, Trie *t)
{
int n = strlen(s);
for(int i=0; i<n; i++)
{
int c = idx(s[i]);
if(!t->ch[c])
{
t->ch[c] = new Trie();
}
t = t->ch[c];
}
if(!t->val) t->val = color++;
return t->val;
}
}*tr; int find(int x)
{
if(fa[x]<0) return x;
return fa[x]=find(fa[x]);
} int main()
{
// freopen("in.txt", "r", stdin);
memset(fa, -1, sizeof(fa));
tr = new Trie();
while(scanf("%s%s", s1, s2)>0)
{
u = tr->ins_find(s1, tr), v = tr->ins_find(s2, tr);
in[u]++, in[v]++;
u = find(u), v = find(v);
if(u!=v) fa[u] = v;
}
int num1=0, num2=0; for(int i=1; i<color; i++)
{
if(in[i]&1) num1++;
if(fa[i]==-1) num2++;
}
if((num1==0 || num1==2) && num2<2)
puts("Possible");
else puts("Impossible");
return 0;
}

POJ 2513 Colored Sticks(欧拉回路,字典树,并查集)的更多相关文章

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

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27097   Accepted: 7175 ...

  2. [欧拉] poj 2513 Colored Sticks

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

  3. poj 2513 Colored Sticks(欧拉路径+并检查集合+特里)

    题目链接:poj 2513 Colored Sticks 题目大意:有N个木棍,每根木棍两端被涂上颜色.如今给定每一个木棍两端的颜色.不同木棍之间拼接须要颜色同样的 端才干够.问最后是否能将N个木棍拼 ...

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

    题目链接 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with ...

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

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

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

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

  7. POJ - 2513 Colored Sticks(欧拉通路+并查集+字典树)

    https://vjudge.net/problem/POJ-2513 题解转载自:優YoU  http://user.qzone.qq.com/289065406/blog/1304742541 题 ...

  8. poj2513 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. linq之join子句

    前面我们总结Linq查询子句总共有8个,join子句是我们讲解的最后一个子句.join子句也是相对比较复杂的,所以最后来讲.join子句可以处理两个数据源之间的联系,当然这两个数据源之间必须存在相关联 ...

  2. Asp.net 字符(二)

    using System; using System.Collections; using System.Collections.Generic; using System.IO; using Sys ...

  3. sphinx配置文件sphinx.conf参数详细说明

    sphinx配置文件sphinx.conf参数详细说明 sphinx.conf各个参数详细说明 # # Sphinx configuration file sample # # WARNING! Wh ...

  4. MVC 5 + EF6 入门完整教程14 -- 动态生成面包屑导航

    上篇文章我们完成了 动态生成多级菜单 这个实用组件. 本篇文章我们要开发另一个实用组件:面包屑导航. 面包屑导航(BreadcrumbNavigation)这个概念来自童话故事"汉赛尔和格莱 ...

  5. php中的字符串常用函数(三) str_replace() 子字符串替换

    mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] ); 该函数返回一个字 ...

  6. No.002:Add Two Numbers

    问题: You are given two linked lists representing two non-negative numbers.  The digits are stored in ...

  7. $(document).ready()即$()方法和window.onload方法的比较

    以浏览器装载文档为例,我们都知道在页面完毕后,浏览器会通过JavaScript为DOM元素添加事件.在常规的JavaScript代码中,通常使用window.onload方法,而在jQuery中,使用 ...

  8. 【GOF23设计模式】代理模式

    来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_代理模式.静态代理 package com.test.proxy.staticProxy; public interfac ...

  9. [ASP.NET MVC] Model Binding With NameValueCollectionValueProvider

    [ASP.NET MVC] Model Binding With NameValueCollectionValueProvider 范例下载 范例程序代码:点此下载 问题情景 一般Web网站,都是以H ...

  10. 【基础】PHP变量及变量作用域

    新学PHP,比较有意思的语法,记录下. 1. 变量的作用域 作用域只分两个Global和Local,Global相对于整个.php文件来讲,Local是本地最小范围,是距离变量最近的范围,如:在函数中 ...