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 ...
随机推荐
- Spring(十八):Spring AOP(二):通知(前置、后置、返回、异常、环绕)
AspectJ支持5种类型的通知注解: @Before:前置通知,在方法执行之前执行: @After:后置通知,在方法执行之后执行: @AfterRunning:返回通知,在方法返回结果之后执行(因此 ...
- Flask调试
1.AttributeError: 'NoneType' object has no attribute 'app' 原因:直接在py中调用视图函数,但没有上下文,导致出错 2.不转换html代码 { ...
- [Git] Squash all of my commits into a single one and merge into master
Often you have your feature branch you’ve been working on and once it’s ready, you just want it to m ...
- C#.NET常见问题(FAQ)-命名空间namespace如何理解
Namespace就是定义一个对象是哪个门派的,比如你现在就在A门派下,A门派的所有功夫(属性和方法)都可以不加声明直接调用.如果你要用B门派的功夫,就要先声明是B门派的XXX对象的XXX实例的XXX ...
- keytab生成不了
vim /var/kerberos/krb5kdc/kadm5.acl 将*e改成* /etc/init.d/kadmin restart 重启kadmin
- Linux下使用Supervisor来管理维护程序-详解
一.场景 常常需要后台支行一个进程,或者开机自动运行等等. 首先,后台运行可以考虑使用nohup和&来实现,想实现开机运行,可以把命令写到/etc/rc.d/rc.local中. 但是,上面这 ...
- 通过16道练习学习Linq和Lambda
http://kb.cnblogs.com/page/73528/ 1. 查询Student表中的所有记录的Sname.Ssex和Class列. select sname,ssex,class fro ...
- MAC快捷方式记录
刚刚转到MAC,很多功能发现不能用,总结一下: 刷新页面:command+r 命令行,到行首:control+a 命令行,到行尾:control+e vim,到行尾:shift+$ vim,到行首:s ...
- vuejs组件交互 - 01 - 父子组件之间的数据交互
父子组件之间的数据交互遵循: props down - 子组件通过props接受父组件的数据 events up - 父组件监听子组件$emit的事件来操作数据 示例 子组件的点击事件函数中$emit ...
- 手把手教你从零实现Linux misc设备驱动一(基于友善之臂4412开发板)
关于怎样来写一个misc设备,在前面有篇文章已经介绍了大致的流程,如今就让我们来实现一个最简单的misc设备驱动. http://blog.csdn.net/morixinguan/article/d ...