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的更多相关文章

  1. poj 2513 Colored Sticks (trie 树)

    链接:poj 2513 题意:给定一些木棒.木棒两端都涂上颜色,不同木棒相接的一边必须是 同样的颜色.求能否将木棒首尾相接.连成一条直线. 分析:能够用欧拉路的思想来解,将木棒的每一端都看成一个结点 ...

  2. poj 2513 Colored Sticks(欧拉路径+并检查集合+特里)

    题目链接:poj 2513 Colored Sticks 题目大意:有N个木棍,每根木棍两端被涂上颜色.如今给定每一个木棍两端的颜色.不同木棍之间拼接须要颜色同样的 端才干够.问最后是否能将N个木棍拼 ...

  3. poj 2513 Colored Sticks( 字典树哈希+ 欧拉回路 + 并查集)

    题目:http://poj.org/problem?id=2513 参考博客:http://blog.csdn.net/lyy289065406/article/details/6647445 htt ...

  4. [欧拉] poj 2513 Colored Sticks

    主题链接: http://poj.org/problem? id=2513 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Tota ...

  5. POJ 2513 - Colored Sticks - [欧拉路][图的连通性][字典树]

    题目链接: http://poj.org/problem?id=2513 http://bailian.openjudge.cn/practice/2513?lang=en_US Time Limit ...

  6. POJ 2513 Colored Sticks(欧拉道路+字典树+并查集)

    http://poj.org/problem?id=2513 题意: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 思路: 题目很明 ...

  7. POJ 2513 Colored Sticks(Tire+欧拉回(通)路判断)

    题目链接:http://poj.org/problem?id=2513 题目大意:你有好多根棍子,这些棍子的两端分都别涂了一种颜色.请问你手中的这些棍子能否互相拼接,从而形成一条直线呢? 两根棍子只有 ...

  8. poj 2513(欧拉路径+字典树映射)

    题目链接:http://poj.org/problem?id=2513 思路:题目还是很简单的,就是判断是否存在欧拉路径,我们给每个单词的头和尾映射序号,统计度数.对于给定的无向图,当且仅当图连通并且 ...

  9. poj 2513 欧拉图/trie

    http://poj.org/problem?id=2513 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submi ...

随机推荐

  1. Java的static详解

    static ['stætɪk] n. 静电:静电干扰 adj. 静态的:静电的:静力的 在计算机上我们译为:静态的.在Java种根据它修饰对象不同,我们可以划分为 1. static对象 2. st ...

  2. c#中常用的异常类型

    c#中异常捕获catch{}常用的异常类型 Exception 类   描述 SystemException 其他用户可处理的异常的基本类  ArgumentException 方法的参数是非法的  ...

  3. HDU3853-LOOPS(概率DP求期望)

    LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Su ...

  4. python wsgi

    什么是wsgi? wsgi是一个web组件的接口防范,wsgi将web组件分为三类:web服务器,web中间件,web应用程序 wsgi基本处理模式为:wsgi Server -> wsgi m ...

  5. Ajax异步请求XMLHttpRequest对象Get请求

    $(function () { $("#btnGetDate").click(function () { var xhr; //第一步:创建异步请求的核心的对象: if (XMLH ...

  6. Javascript 递归函数

    递归函数就是在函数内部调用它自己.在Javascript 中有很多写法,值得我们学习一下(Javascript太灵活了).还是用n的 阶乘 来写例子吧. 1. 首先,来看一个最普通 最正常的写法. f ...

  7. BZOJ 3156: 防御准备( dp + 斜率优化 )

    dp(i)表示处理完[i,n]且i是放守卫塔的最小费用. dp(i) = min{dp(j) + (j-i)(j-i-1)/2}+costi(i<j≤N) 然后斜率优化 ------------ ...

  8. MVC数据提交

    关于请求方式(form表单) .form的几个属性 <form name="input" action="http://www.baidu.com" me ...

  9. IntelliJ IDEA中创建并运行scala应用程序

    1.安装scala插件 2.创建scala项目 下载scala SDK,如果你已经下载了,选择你所下载的版本,点击OK

  10. 分组求和SQL示例

        1.ROLLUP和CUBE函数,自动汇总数据      select * from test_tbl的数据这样的      col_a col_b col_c      ---- ----- ...