POJ 2513 Colored Sticks (欧拉回路 + 字典树 +并查集)
|
Colored Sticks
Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a straight line such that the colors of the endpoints that touch are of the same color?
Input Input is a sequence of lines, each line contains two words, separated by spaces, giving the colors of the endpoints of one stick. A word is a sequence of lowercase letters no longer than 10 characters. There is no more than 250000 sticks.
Output If the sticks can be aligned in the desired way, output a single line saying Possible, otherwise output Impossible.
Sample Input blue red Sample Output Possible Hint Huge input,scanf is recommended.
Source |
详细解释:http://blog.csdn.net/lyy289065406/article/details/6647445
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector> using namespace std; struct Trie{
int x;
Trie *next[];
}*root,memory[]; int deg[],father[],cnt,tot; Trie *create(){
Trie *p=&memory[cnt++];
for(int i=;i<;i++)
p->next[i]=NULL;
return p;
} void InsertTrie(char *str,int x){
Trie *loc=root;
for(int i=;str[i]!='\0';i++){
int id=str[i]-'a';
if(loc->next[id]==NULL)
loc->next[id]=create();
loc=loc->next[id];
}
loc->x=x;
} int SearchTrie(char *str){
Trie *loc=root;
for(int i=;str[i]!='\0';i++){
int id=str[i]-'a';
if(loc->next[id]==NULL)
return ;
loc=loc->next[id];
}
return loc->x;
} void init(){
for(int i=;i<=;i++)
father[i]=i;
memset(deg,,sizeof(deg));
cnt=;
tot=;
} int findSet(int x){
if(x!=father[x]){
father[x]=findSet(father[x]);
}
return father[x];
} int judge(){ //判断是否满足欧拉
int odd=;
for(int i=;i<=tot;i++)
if(deg[i]%==)
odd++;
if(odd!= && odd!=)
return ;
int k=findSet();
for(int i=;i<=tot;i++)
if(k!=findSet(i))
return ;
return ;
} int main(){ //freopen("input.txt","r",stdin); char s1[],s2[];
init();
root=create();
while(~scanf("%s%s",s1,s2)){
int x=SearchTrie(s1); //映射求编号速度太慢
int y=SearchTrie(s2); //用字典树来求编号
if(x==)
InsertTrie(s1,x=++tot);
if(y==)
InsertTrie(s2,y=++tot);
deg[x]++;
deg[y]++;
int fx=findSet(x);
int fy=findSet(y);
if(fx!=fy)
father[fx]=fy;
}
if(judge())
printf("Possible\n");
else
printf("Impossible\n");
return ;
}
POJ 2513 Colored Sticks (欧拉回路 + 字典树 +并查集)的更多相关文章
- [欧拉] 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(欧拉回路,字典树,并查集)
题意:给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 无向图存在欧拉路的充要条件为: ① 图是连通的: ② 所有节 ...
- 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 ...
随机推荐
- 整数对A满足二叉查找树,B满足最大堆
1 题目 给出一组整数对 { (a[0], b[0]), (a[1], b[1]) ... (a[n-1], b[n-1]) },全部 a 值和 b 值分别不反复(随意 i != j 满足 a[i] ...
- CreateFont函数为什么改变不了字体?该怎么解决
CreateFont函数为什么改变不了字体?CFont * f; f = new CFont; f-> CreateFont( ...
- Install Hyper-V on Windows 10
Enable Hyper-V to create virtual machines on Windows 10.Hyper-V can be enabled in many ways includ ...
- Sql Server重复数据删除
--在sql2005下可以 ,sql2000不可以 create table tb(id int,name varchar(4))insert tb select 1,'aa'union all s ...
- Google Java Style 中文版
Google Java Style 中文版 基于官方文档2013.12.19最后一次改动. 翻译人:Weir Zhang (zh.weir) 旁白:水平有限,很多地方只是意译.不准确的地方 ...
- logistic回归具体解释(二):损失函数(cost function)具体解释
有监督学习 机器学习分为有监督学习,无监督学习,半监督学习.强化学习.对于逻辑回归来说,就是一种典型的有监督学习. 既然是有监督学习,训练集自然能够用例如以下方式表述: {(x1,y1),(x2,y2 ...
- 电子商务 B2C 结构图【转载+整理】
本文内容 商品展示 内容展示 订单确认 支付系统 用户中心 商品&促销 CRM 订单处理 WMS 采购管理 财务管理 报表管理 系统设置 WA系统 商品展示 按照 Ebay 内部分类,任何 ...
- 在线创建springboot项目
在线创建srpingboot项目的网址:https://start.spring.io/ 我只选了一个web,然后开始生成:
- Swift语言精要 - 浅谈结构体(Struct)
CGRect, CGSize, CGPoint这些是 . String, Int, Array, Dictionary这些我们经常用的也是结构体(Struct). 那么结构体(Struct)到底是什么 ...
- HDS TrueCopy-数据远程容灾白皮书-IOPS数据
http://wenku.it168.com/d_000767925.shtml Truecopy 安装实施-包含图 http://www.docin.com/p-261693079.html 来自: ...