POJ 2513 Colored Sticks(欧拉回路,字典树,并查集)
题意:给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的。
无向图存在欧拉路的充要条件为:
① 图是连通的;
② 所有节点的度为偶数,或者有且只有两个度为奇数的节点。
图的连通可以利用并查集去判断。
度数的统计比较容易。
代码实现
//第一次用指针写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(欧拉回路,字典树,并查集)的更多相关文章
- POJ 2513 Colored Sticks (欧拉回路 + 字典树 +并查集)
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27097 Accepted: 7175 ...
- [欧拉] poj 2513 Colored Sticks
主题链接: http://poj.org/problem? id=2513 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Tota ...
- poj 2513 Colored Sticks(欧拉路径+并检查集合+特里)
题目链接:poj 2513 Colored Sticks 题目大意:有N个木棍,每根木棍两端被涂上颜色.如今给定每一个木棍两端的颜色.不同木棍之间拼接须要颜色同样的 端才干够.问最后是否能将N个木棍拼 ...
- POJ 2513 Colored Sticks (欧拉回路+并查集+字典树)
题目链接 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with ...
- POJ 2513 - Colored Sticks - [欧拉路][图的连通性][字典树]
题目链接: http://poj.org/problem?id=2513 http://bailian.openjudge.cn/practice/2513?lang=en_US Time Limit ...
- POJ 2513 Colored Sticks(欧拉道路+字典树+并查集)
http://poj.org/problem?id=2513 题意: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 思路: 题目很明 ...
- POJ - 2513 Colored Sticks(欧拉通路+并查集+字典树)
https://vjudge.net/problem/POJ-2513 题解转载自:優YoU http://user.qzone.qq.com/289065406/blog/1304742541 题 ...
- poj2513 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 ...
随机推荐
- 怎样从C#中打开数据库并进行 增 删 改 查 操作
首先 在C#中引用数据库的操作! (因为我们用的是SQLserver数据库,所以是SqlClient) using System.Data.SqlClient; 1:要实现对数据库的操作,我们必须先登 ...
- c#调用word com组件 替换书签套打
安装office2007,添加com引用Microsoft Word12.0 Object Library和Microsoft Office12.0 Object Library using Syst ...
- [转载]Ubuntu14.04 LTS更新源
不同的网络状况连接以下源的速度不同, 建议在添加前手动验证以下源的连接速度(ping下就行),选择最快的源可以节省大批下载时间. 首先备份源列表: sudo cp /etc/apt/sources.l ...
- C# 进制转换参考
//十进制转二进制 Console.WriteLine(Convert.ToString(69, 2)); //十进制转八进制 Console.WriteLine(Convert.ToString(6 ...
- C#实现判断字符是否为中文
C#实现判断字符是否为中文 (2012-08-14 14:25:28) 标签: gb2312 big5编码 gbk编码 判断 汉字 杂谈 分类: 技术 protected bool IsChinese ...
- lnmp+phpmyadmin配置与出现问题
本博客归moka同学(新浪微博:moka同学)本人亲自整理,如有使用,请加链接注明出处. lnmp 安装完全后,配置phpmyadmin .其访问方式为 http://202.18.400.379/p ...
- Access restriction : The constructor BASE64Decoder() is not accessible due to restriction on required library
1.问题描述 找不到包 sun.misc.BASE64Encoder 2. 解决方案 只需要在project build path中先移除JRE System Library,再添加库JRE Sys ...
- (旧)子数涵数·Flash——影片剪辑的其他操作
一.复制影片剪辑 1.方法:duplicatemovieClip(影片实名,新实名,深度级别) 2.解释:影片实名就是你要复制的对象,新实名就是你要粘贴的对象,深度级别就是粘贴后的影片剪辑的堆叠顺序( ...
- RCA端子颜色(红、白、黄)
RCA端子(红白黄)的作用: 黄:视频 红:左声道 白:右声道 RCA为两口插头,红色代表左声道,白色为右声道,3.5(AUX口)同样为立体声接头,虽然它只有一个端口,同样也具有左右声道分开传输的功能 ...
- zeromq 学习和python实战
参考文档: 官网 http://zeromq.org/ http://www.cnblogs.com/rainbowzc/p/3357594.html 原理解读 zeromq只是一层针对socke ...