题目链接:http://poj.org/problem?id=2513

Colored Sticks

Time Limit: 5000MS   Memory Limit: 128000K
Total Submissions:40949   Accepted: 10611

Description

You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a straight line such that the colors of the endpoints that touch are of the same color?

Input

Input is a sequence of lines, each line contains two words, separated by spaces, giving the colors of the endpoints of one stick. A word is a sequence of lowercase letters no longer than 10 characters. There is no more than 250000 sticks.

Output

If the sticks can be aligned in the desired way, output a single line saying Possible, otherwise output Impossible.
 
题目大意:给出以下字符串,代表每根棍子的两端颜色,只能将颜色相同的端点连接起来。问是否能连成一欧拉路径。
思路:
1.棍子两端无方向要求,故是无向图的欧拉路径,无向图判断欧拉路径的充要条件是:图连通(无向图的连通性可以用并查集来判断,但有向图不可以)+ 点的度全为偶/当且仅当只有2个点的度为奇
2.关键在于将颜色字符串映射成整型编号,有两种思路:用map直接映射(TLE),用trie树给字符串上编号。
3.具体实现在代码中,值得注意的一点是 G++才能AC。
 #include<stdio.h>
#include<string.h>
#define mem(a, b) memset(a, b, sizeof(a))
const int MAXN = * + ;//最多MAXN种颜色 即最多MAXN种不同的点 char s1[], s2[];
int deg[MAXN];
int trie[MAXN][], cnt, tot;
int id[MAXN], pre[MAXN]; int insert(char s[])
{
int flag = ;
int len = strlen(s);
int root = ;
for(int i = ; i < len; i ++)
{
int id = s[i] - 'a';
if(!trie[root][id])
{
flag = ; //单词之前没出现过
trie[root][id] = ++ cnt;
}
root = trie[root][id];
}
if(!flag)
{
tot ++;
id[root] = tot;
return id[root];
}
else
return id[root];
} int find(int x)
{
if(pre[x] == x)
return x;
else
{
int root = find(pre[x]);
pre[x] = root;
return pre[x];
}
} int main()
{
for(int i = ; i <= MAXN + ; i ++)
pre[i] = i;
while(scanf("%s%s", s1, s2) != EOF)
{
int a = insert(s1), b = insert(s2);//返回的是颜色所对应的序号
deg[a] ++, deg[b] ++; //点的度数 判断是否存在欧拉路径
int x = find(a), y = find(b); //查找根节点
if(x != y)
pre[y] = x;
}
int flag = ;
for(int i = ; i < tot; i ++) //总共有tot个不同的点
if(find(i) != find(i + ))
{
flag = ;
break;
}
if(flag == )
printf("Impossible\n");
else
{
int xx = ;
for(int i = ; i <= tot; i ++)
if(deg[i] % )
xx ++;
if(xx == || xx == )
printf("Possible\n");
else
printf("Impossible\n");
}
return ;
}

POJ2513 【并查集+欧拉路径+trie树】的更多相关文章

  1. 【BZOJ2054】疯狂的馒头(并查集,线段树)

    [BZOJ2054]疯狂的馒头(并查集,线段树) 题面 BZOJ 然而权限题,随便找个离线题库看看题吧. 题解 线段树就是个暴力,如果数据可以构造就能卡掉,然而不能构造,要不然复杂度瓶颈成为了读入了. ...

  2. POJ 2513 字典树+并查集+欧拉路径

    Description: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 解题思路: 可以用图论中欧拉路的知识来解这道题,首先可以把木 ...

  3. 【并查集】【树】最近公共祖先LCA-Tarjan算法

    最近公共祖先LCA 双链BT 如果每个结点都有一个指针指向它的父结点,于是我们可以从任何一个结点出发,得到一个到达树根结点的单向链表.因此这个问题转换为两个单向链表的第一个公共结点(先分别遍历两个链表 ...

  4. UVa 10129 (并查集 + 欧拉路径) Play on Words

    题意: 有n个由小写字母的单词,要求判断是否存在某种排列使得相邻的两个单词,前一个单词末字母与后一个单词首字母相同. 分析: 将单词的两个字母看做节点,则一个单词可以看做一条有向边.那么题中所求的排列 ...

  5. POJ 1944 Fiber Communications (枚举 + 并查集 OR 线段树)

    题意 在一个有N(1 ≤ N ≤ 1,000)个点环形图上有P(1 ≤ P ≤ 10,000)对点需要连接.连接只能连接环上相邻的点.问至少需要连接几条边. 思路 突破点在于最后的结果一定不是一个环! ...

  6. hdu 3172 Virtual Friends(并查集,字典树)

    题意:人与人交友构成关系网,两个人交友,相当于两个朋友圈的合并,问每个出两人,他们目前所在的关系网中的人数. 分析:用并查集,其实就是求每个集合当前的人数.对于人名的处理用到了字典树. 注意:1.题目 ...

  7. ACM学习历程—SNNUOJ 1110 传输网络((并查集 && 离线) || (线段树 && 时间戳))(2015陕西省大学生程序设计竞赛D题)

    Description Byteland国家的网络单向传输系统可以被看成是以首都 Bytetown为中心的有向树,一开始只有Bytetown建有基站,所有其他城市的信号都是从Bytetown传输过来的 ...

  8. hdu6200 mustedge mustedge mustedge (并查集+dfs序树状数组)

    题意 给定一个n个点m条边无向图(n,m<=1e5) 支持两个操作 1.添加一条边 2.询问点u到点v的所有路径中必经边的条数 操作数<=1e5 分析 第一眼看起来像是要动态维护无向图的边 ...

  9. 2019牛客第八场多校 E_Explorer 可撤销并查集(栈)+线段树

    目录 题意: 分析: @(2019牛客暑期多校训练营(第八场)E_Explorer) 题意: 链接 题目类似:CF366D,Gym101652T 本题给你\(n(100000)\)个点\(m(1000 ...

随机推荐

  1. Python 10.2

    time 模块: strftime('%y%m%s',yesterday) ==>返回一个字符串代表的日期

  2. php使用ZipArchive提示Fatal error: Class ZipArchive not found in的解决方法

    使用压缩包函数必须要安装zip扩展,否则会报错 $ apt install php-zip

  3. shell 统计行数,单词个数,字符个数

    如果我们想知道1.txt中有多少行,多少个单词,多少个字符.我们可以使用wc命令.选项与参数-l:今列出行-w:今列出多少字(英文单词)-m:多少字符[zhang@localhost ~]$ cat ...

  4. springboot的jar在linux运行

    springboot项目使用maven打包成jar包,如何在linux优雅部署?平时启动项目使用java -jar命令,关闭程序需要查询pid再查杀进程,这样都太麻烦了,今天发现一个博客已经写好的脚本 ...

  5. CF1208A

    CF1208A 题意: 就是把斐波那契数列的+改成异或,求第n项的值. 解法: 又是一个人类智慧题,打表找规律. 可以发现答案在 $ a,b,a⊕b $ 三个数中循环 CODE: #include&l ...

  6. nginx安装第三方模块echo-nginx-module

    cd ~ wget -S https://github.com/agentzh/echo-nginx-module/archive/master.zip mv master echo-nginx-mo ...

  7. elasticsearch sql插件配置(5.0及以上版本)

    github官方参考地址:https://github.com/NLPchina/elasticsearch-sql/ 采用 git + node 的方式,所以安装前需要先安装好node,node n ...

  8. 如何确定哪个SMB客户端/会话在Server 2008R2 Windows文件服务器上打开了特定文件?

    参考: http://www.kbase101.com/question/54969.html NetworkOpenedFiles v1.25  https://www.nirsoft.net/ut ...

  9. postgresql interval 字段拼接

    无拼接时: SELECT scan_time + '5 day' FROM tbl_temp_record SELECT scan_time + '-5 day' FROM tbl_temp_reco ...

  10. You can't specify target table 'a' for update in FROM clause

    项目中有一个功能变动上线,其中有一张表ttt的字段cc,历史数据需要处理,把字段cc中为'xxx'的值替换为'yyy'. 表A结构如下: CREATE TABLE `ttt` ( `id` bigin ...