题目链接:

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. Excel 开发备忘

    1.装完读取插件才可以对EXCEL读取 Excel 2010 读取数据插件 https://www.microsoft.com/zh-CN/download/details.aspx?id=13255 ...

  2. TCP/IP、Http、Https、Socket的区别

    网络由下往上分为物理层.数据链路层.网络层( IP协议).传输层( TCP协议).会话层.表示层和应用层(HTTP协议) 接下来我来说说个人理解其中的TCP/IP.Http.Socket的区别 TCP ...

  3. 10-----BBS论坛

    BBS论坛(十) 10.1.客户端权限验证功能完成 (1)cms/cms_profile 显示当前用户的角色和权限 <tr> <td>角色:</td> <td ...

  4. js学习笔记 -- 随记

    js不区分整数和浮点数,统一用Number表示, js'=='比较会自动转换类型,会产生奇怪结果,'==='不会转换比较类型,如果不一致返回false,因此js判断始终用'===' `` 保留换行,也 ...

  5. Linux配置SSH免密码登录

    CentOS配置SSH免密码登录为例说明:SSH远程登录的安全外壳协议有两种身份认证机制: - 用户名+密码 -密钥登录 环境准备 host1:192.168.0.10host2:192.168.0. ...

  6. vue 组件之间数据传递(七)

    1.props:父组件 -->传值到子组件 app.vue是父组件 ,其它组件是子组件,把父组件值传递给子组件需要使用 =>props 在父组件(App.vue)定义一个属性(变量)sex ...

  7. CSS3 中 图标编码 icon——Font-Awesome

    在做网页开发中经常会用到图标,原来经常会到一些icon网站上找导入到项目中,现在Font-Awesome中的有很多的图标,并且还在不断更新 现在Font-Awesome最新版本是4.7,下载出来的Fo ...

  8. shell查看内存

    <1>jps<2>ps<3>free<4>df<5>top jps: 很多Java命令都在jdk的JAVA_HOME/bin/目录下面,jp ...

  9. Murano Weekly Meeting 2015.09.29

    Meeting time: 2015.September.29th 1:00~2:00 Chairperson:  Serg Melikyan, PTL from Mirantis Meeting s ...

  10. 读<<programming ruby>> 7.6节 flip-flop 理解

    书中源码是这样的 File.foreach('1.txt') do |x| if(($. == 1) || x =~ /eig/) .. (($. == 3) || x =~ /nin/) then ...