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. 在python中编写socket服务端模块(二):使用poll或epoll

    在linux上编写socket服务端程序一般可以用select.poll.epoll三种方式,本文主要介绍使用poll和epoll编写socket服务端模块. 使用poll方式的服务器端程序代码: i ...

  2. HDU 5755 Gambler Bo(高斯消元)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5755 [题目大意] 一个n*m由0,1,2组成的矩阵,每次操作可以选取一个方格,使得它加上2之后对 ...

  3. 《windows程序设计》学习_3.3:利用xp扫雷资源

    #include<windows.h> #include "resource.h" LRESULT CALLBACK WndProc (HWND, UINT, WPAR ...

  4. BZOJ 2253: [2010 Beijing wc]纸箱堆叠

    题目 2253: [2010 Beijing wc]纸箱堆叠 Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 239  Solved: 94 Descr ...

  5. poj 2774 Long Long Message,后缀数组,求最长公共子串 hdu1403

    题意:给出两个字符串,求最长公共子串的长度. 题解:首先将两个字符串连在一起,并在中间加一个特殊字符(字串中不存在的)切割,然后两个串的最长公共字串就变成了全部后缀的最长公共前缀.这时就要用到heig ...

  6. 一步一步学c#(五):泛型

    泛型 性能 泛型的一个重要的优点是性能.system.collections和system.collections.generic名称空间的泛型和非泛型集和类.对值类型使用非泛型集合类,在把值类型转换 ...

  7. Sql 字符串操作类COALESCE

    SqlServer中肯定有过将表中某列的值拼接成字符串,以","或者其他符号隔开的情况吧,一般情况我们会这样做: declare @returnValue nvarchar(max ...

  8. Cookie获取、设置值

    设置: HttpCookie cookie = new HttpCookie("cookieName"); cookie.Value = "name1" Htt ...

  9. Junit 测试常见错误

    1. java.lang.Exception: No tests found matching Method deleteUser(mybatis.dao.Usertest) from org.jun ...

  10. mac outlook无法发送邮件

    工具-帐户 第一步把SSL钩挑上 第二步 下面的更多选项,验证选择“使用接收服务器信息” 搞定了!记得个赞!