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 ...
随机推荐
- 【deep learning学习笔记】Recommending music on Spotify with deep learning
主要内容: Spotify是个类似酷我音乐的音乐站点.做个性化音乐推荐和音乐消费.作者利用deep learning结合协同过滤来做音乐推荐. 详细内容: 1. 协同过滤 基本原理:某两个用户听的歌曲 ...
- hdu 4738 Caocao's Bridges(2013杭州网络赛丶神坑)
就是求最小权值的桥..不过有好几个坑... 1:原图不连通,ans=0. 2: m<=n^2 显然有重边,重边必然不是桥,处理重边直接add(u, v, INF). 3: 最小桥边权为0的时 ...
- The Building Blocks-Components of EA part 1- Information and Strategy
1. Zachman Framework Presented as matrix of Rows and Columns representing domain of interest and lev ...
- hibernate -inverse
one to many inverse=false只能设置维护关联关系的多的一方, inverse属性: 默认为false,表示本方维护关联关系. 如果为true,表示本方不维护关联关系(并不意味着对 ...
- BZOJ 1996: [Hnoi2010]chorus 合唱队(dp)
简单的dp题..不能更水了.. --------------------------------------------------------------- #include<cstdio&g ...
- C/C++指针和数组的关系
首先一个列子说明指针和数组真的不是一回事: 浅谈C中的指针和数组(一) 如果用硬是把数组和指针放在一个,一个放声明里一个放定义中,这里就能看到指针和数组名是不同的.这篇文章从符号表的角度分析一下指针和 ...
- Java中Calender引用类型
某些时候需要使用深拷贝: Calendar startTime = (Calendar) this._paramModel.getStartTime().clone(); 这样对startTime.a ...
- android手机关于google play商店闪退的解决办法
部分android手机没有安装google play商店,这个可以通过类似“机锋”.“360手机助手”等应用市场下载. 安装google play商店后,点击打开却一闪而过:这个问题是因为手机没有安装 ...
- 5.6.3 String类型
String类型是字符串的对象包装类型,可以像下面这样使用String构造函数来创建. var stringObject = new String("hello world"); ...
- MYSQL存储过程事务列子
CREATE DEFINER=`root`@`localhost` PROCEDURE `createBusiness`(parameter1 int) BEGIN #Routine body goe ...