题目链接:

https://vjudge.net/problem/POJ-2513

题目大意:

给一些木棍,两端都有颜色,只有两根对应的端点颜色相同才能相接,问能不能把它们接成一根木棍

解题思路:

题意不难,典型的无向图判断是否存在欧拉通路或回路的问题。

1、欧拉通路或回路的判定条件是图联通,并且度数为奇数的点只有两个或者0个

2、颜色多,输入的字符串多,需要用字典树存储,给字符串标号用字典树标记

3、点数多,用并查集判断连通性。

第一次WA:没由开is_word标记,写成了前缀判断,此处应该是单词判断

第二次WA:判断连通的时候简单地用p[i] == i;满足该条件的数目等于1来判断连通性,但是此题输入的图可能很复杂,应该判断是否所有点的根节点是同一个点这样进行判断。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<algorithm>
#include<vector>
#include<sstream>
#define lowbot(i) (i&(-i))
using namespace std; const int maxn = 1e6 + ;
int tree[maxn][];
//字典树tree[u][v]表示编号为u的节点的下一个字母为v连接的节点的编号
int idx(char c){ return c - 'a'; }//可以写成宏定义
int tot = ;//根节点编号为1
bool is_word[maxn];
int sum[maxn];
int cnt = ;//cnt
int Insert(char s[], int u)//u表示根节点
//插入字符串s
{
for(int i = ; s[i]; i++)
{
int c = idx(s[i]);
if(!tree[u][c])
tree[u][c] = ++tot;
u = tree[u][c];
}
sum[u] = ++cnt;
is_word[u] = ;//标记单词结尾
return sum[u];
} int ID(char s[], int u)//返回单词s的编号
{
for(int i = ; s[i]; i++)
{
int c = idx(s[i]);
if(!tree[u][c])//还没有编号
return Insert(s, );
u = tree[u][c];
}
if(!is_word[u])//是前缀但是并不是单词
{
return Insert(s, );
}
return sum[u];
}
int p[maxn];//并查集
int d[maxn];//记录每个点度数之和
int Find(int x)
{
return x == p[x] ? x : p[x] = Find(p[x]);
}
int main()
{
for(int i = ; i < maxn; i++)
{
p[i] = i;
}
char s1[], s2[];
int u, v, x, y;
while(scanf("%s%s", s1, s2) != EOF)
{
u = ID(s1, );
v = ID(s2, );
//cout<<u<<" "<<v<<endl;
x = Find(u);
y = Find(v);
if(x != y)p[x] = y;
d[u]++;
d[v]++;
}
int flag = , flag1 = ;
//flag判断连通性
//flag1记录奇点度数的点的数目
int t = Find();
if(d[] & )flag1++;
for(int i = ; i <= cnt; i++)
{
if(Find(i) != t){flag = ;break;}
if(d[i] & )flag1++;
}
if(flag && (flag1 == || flag1 == ))
printf("Possible\n");
else printf("Impossible\n");
return ;
}

POJ-2513 Colored Sticks---欧拉回路+并查集+字典树的更多相关文章

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

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

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

    题目链接 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with ...

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

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

  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(欧拉回路,字典树,并查集)

    题意:给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的.   无向图存在欧拉路的充要条件为: ①     图是连通的: ②     所有节 ...

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

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27097   Accepted: 7175 ...

  7. poj 2513 Colored Sticks trie树+欧拉图+并查集

    点击打开链接 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27955   Accepted ...

  8. poj 2513 Colored Sticks (trie树+并查集+欧拉路)

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 40043   Accepted: 10406 ...

  9. POJ 2513 Colored Sticks (离散化+并查集+欧拉通路)

    下面两个写得很清楚了,就不在赘述. http://blog.sina.com.cn/s/blog_5cd4cccf0100apd1.htmlhttp://www.cnblogs.com/lyy2890 ...

  10. POJ - 2513 Colored Sticks(欧拉通路+并查集+字典树)

    https://vjudge.net/problem/POJ-2513 题解转载自:優YoU  http://user.qzone.qq.com/289065406/blog/1304742541 题 ...

随机推荐

  1. DJ

    必知必会13条 all():查询所有结果 filter(**kwargs)   过滤,取到符合条件的对象,比get方法好,找不到会返回空 get(**kwargs)      取到符合条件对象,对象有 ...

  2. Jmeter源代码学习心得

    1.TestPlan和WorkBench GUI类是直接加载的,因此左边的树形菜单开始启动Jmeter时显示也是这两个,默认写死了的!可以改源码!在MenuFactory中有相应代码. 2.其它的GU ...

  3. spring aop execution用法

    代码结构: 1. "execution(* com.ebc..*.*(..))" 与 "execution(*  com.ebc..*(..))" 2019-0 ...

  4. Zero Sum Subarray

    Given an integer array, find a subarray where the sum of numbers is zero. Your code should return th ...

  5. Window 远程连接 Ubuntu 系统

    安装XRDP 服务, 用windows远程连接ubuntu 1. Step 1 – Install xRDP sudo apt-get update sudo apt-get install xrdp ...

  6. sqlserver 2012 部署详解

    01,下载 官网下载: https://www.microsoft.com/zh-cn/download/details.aspx?id=29066 02,安装 检查系统环境配置 成功了就继续,其他的 ...

  7. sysbench测试MySQL筛选tps

    log=$1tps_array=`awk -F '[,:]' '{print $4}' ${log}`zero=0 for tps in ${tps_array}do tps=`echo ${tps} ...

  8. python3.4 x86_64-linux-gnu-gcc Error

    running install    running build    running build_py    creating build    creating build/lib.linux-x ...

  9. 《nginx 三》实现nginx的动态负载均衡——实战

    Http动态负载均衡 什么是动态负载均衡 传统的负载均衡,如果Upstream参数发生变化,每次都需要重新加载nginx.conf文件, 因此扩展性不是很高,所以我们可以采用动态负载均衡,实现Upst ...

  10. WinForm皮肤 支持.NET4.0 IrisSkin4多彩皮肤演示和下载

    IrisSkin4是一款.NET平台非常优秀的Winform皮肤,链接库文件仅544kb,使用方法也非常简单 IrisSkin4(IrisSkin4.dll + 73套皮肤)[下载地址] 使用方法: ...