poj 2513
http://poj.org/problem?id=2513
73348K 1438MS C++ 1614B
解题思路:欧拉路的应用 要点 :1、判断连通性 2、欧拉路的判断(所有的节点的度为偶数或者只有两个奇数节点)
连通性的判断: 并查集-----由于本题的节点是字符串,,并不好处理,所以用Trie树来获得id。。然后 find 、unin 和普通并查集一样,。
连通性判断:并查集的祖先节点 ,,只有一个,若有多个即 不是连通图,也就不是欧拉路。。
#include <iostream>
#include<cstring>
#include<cstdio>
#define maxn 500005
using namespace std; int f[maxn];
int degree[maxn];
int num;
struct node{
int id;
struct node *next[];
node(){
id =;
memset(next,,sizeof(next));
}
};
node *root = NULL; int maketrie(char *s){//用trie树获得id,,,其实就是给每个节点一个编号。。。用一般的方法不好解决,,就只能用trie树了
node *p = root;
node *temp = NULL;
for(int i=;i<strlen(s);i++){
if(p->next[s[i]-'a']==NULL){
temp = new node;
p->next[s[i]-'a']=temp;
}
p = p->next[s[i]-'a'];
}
if(p->id==)
p->id = ++num;
return p->id;
} int find(int x){
if(x!=f[x])
f[x] = find(f[x]);
return f[x];
} void unin(int x, int y){
int fx = find (x);
int fy = find(y);
if(fx==fy)
return ;
else{
f[fy] = fx;
}
}
int main()
{
for(int i=;i<maxn;i++)//注意此处。。。已经好几次。。i<=maxn了。。。。。runtime error。。。悲催啊。。
f[i] = i;
char s1[],s2[];
int id1,id2;
root = new node;
num =;
while(scanf("%s%s",s1,s2)==){
id1 = maketrie(s1);
id2 = maketrie(s2);
degree[id1]++;//记录其节点的度数
degree[id2]++;
unin(id1,id2);
}
int s = find();
int cnt =;
for(int i=;i<=num;i++){
if(degree[i]%==)
cnt++;
if(cnt>){
printf("Impossible\n");
return ;
}
if(find(i)!=s){//仅只有一个祖先节点,要不然就不是连通图。。就更不是欧拉路了。。
printf("Impossible\n");
return ;
}
}
if(cnt==){//cnt 有可能为1.。。所以特殊说明。。
printf("Impossible\n");
}
else{
printf("Possible\n");
} return ;
}
poj 2513的更多相关文章
- poj 2513 Colored Sticks (trie 树)
链接:poj 2513 题意:给定一些木棒.木棒两端都涂上颜色,不同木棒相接的一边必须是 同样的颜色.求能否将木棒首尾相接.连成一条直线. 分析:能够用欧拉路的思想来解,将木棒的每一端都看成一个结点 ...
- poj 2513 Colored Sticks(欧拉路径+并检查集合+特里)
题目链接:poj 2513 Colored Sticks 题目大意:有N个木棍,每根木棍两端被涂上颜色.如今给定每一个木棍两端的颜色.不同木棍之间拼接须要颜色同样的 端才干够.问最后是否能将N个木棍拼 ...
- poj 2513 Colored Sticks( 字典树哈希+ 欧拉回路 + 并查集)
题目:http://poj.org/problem?id=2513 参考博客:http://blog.csdn.net/lyy289065406/article/details/6647445 htt ...
- [欧拉] poj 2513 Colored Sticks
主题链接: http://poj.org/problem? id=2513 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Tota ...
- 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(Tire+欧拉回(通)路判断)
题目链接:http://poj.org/problem?id=2513 题目大意:你有好多根棍子,这些棍子的两端分都别涂了一种颜色.请问你手中的这些棍子能否互相拼接,从而形成一条直线呢? 两根棍子只有 ...
- poj 2513(欧拉路径+字典树映射)
题目链接:http://poj.org/problem?id=2513 思路:题目还是很简单的,就是判断是否存在欧拉路径,我们给每个单词的头和尾映射序号,统计度数.对于给定的无向图,当且仅当图连通并且 ...
- poj 2513 欧拉图/trie
http://poj.org/problem?id=2513 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submi ...
随机推荐
- How can I get an object's absolute position on the page in Javascript?
How can I get an object's absolute position on the page in Javascript? How can I get an object's abs ...
- 第八章 C#面向对象编程(Object-Oriented Programming,OOP)简介
.NET OOP 一.面向对象编程的含义 1.函数(过程化)编程常常导致单一的应用程序,即所有的功能都包含在几个代码模块中(常常是一个代码模块) 而使用OOP技术,常常使用许多代码模块,每个模块提供特 ...
- JavaScript基础(简介、语法)
一.JavaScript简介 1.JavaScript是个什么东西? 它是个脚本语言,需要有宿主文件,它的宿主文件是HTML文件. 2.它与Java什么关系? 没有什么直接的联系,Java是Sun公司 ...
- final, finally, finalize 的区别
1.final 用于声明属性, 方法和类, 分别表示属性不可变, 方法不可覆盖, 类不可继承.内部类要访问局部变量, 局部变量必须定义成 final 类型, 例如, 一段代码…… 2.finally ...
- 从零开始写驱动——vfd专用驱动芯片HT16514并行驱动程序编写
前言 一直看别人搞的 vfd 很漂亮,前段时间淘了个 vfd 模块来,但没有模块资料,还好芯片没有打磨的,良心商家啊.周末抽空来研究一下这个东西. 从零开始 打开外壳 测试线路 查看芯片是 HT165 ...
- maven copy 依赖jar包
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-depen ...
- [LeetCode]题解(python):095-Unique Binary Search Trees II
题目来源: https://leetcode.com/problems/unique-binary-search-trees-ii/ 题意分析: 给一个整数,返回所有中序遍历是1到n的树. 题目思路: ...
- 11-C语言指针
目录: 一.指针 二.指针与数组 回到顶部 一.指针 1 内存被分为字节,每个字节有唯一地址,指针保存的是内存中的地址. 2 保存指针的变量,就叫指针变量,(保存地址) 3 声明一个指针变量 int ...
- mybatis优化配置
在src下建立db.properties driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/mybatis name=root ...
- elk 添加节点
elk 添加节点: cluster.name: es_cluster node.name: node03 path.data: /elk/elasticsearch/data path.logs: / ...