http://poj.org/problem?id=2513

Colored Sticks
Time Limit: 5000MS   Memory Limit: 128000K
Total Submissions: 37812   Accepted: 9907

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.

Sample Input

blue red
red violet
cyan blue
blue magenta
magenta cyan

Sample Output

Possible

Hint

Huge input,scanf is recommended.

Source

   对每一对颜色进行连接一条无向边然后跑欧拉图验证是否可行,用了Trie来哈希字符串,并查集判断是否是连通图,坑点是有空串,,,所以可能有0个集合,此时输出"Possible";

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
using namespace std;
int N;
struct node
{
node *next[];
int v;
node(){v=;memset(next,NULL,sizeof(next));}
}*root;
int gec(char *s)
{
int len=strlen(s);
node *p=root;
for(int i=;i<len;++i)
{
if(p->next[s[i]-'a']==NULL) p->next[s[i]-'a']=new node();
p=p->next[s[i]-'a'];
}
if(p->v==) p->v=++N;
return p->v;
}
int cnt[];
int f[];
int getf(int v){return f[v]==v?v:f[v]=getf(f[v]);}
int main()
{
char s1[],s2[];
int i;
for(i=;i<=;++i)f[i]=i;
root=new node();
while(scanf("%s%s",s1,s2)!=EOF){
int u=gec(s1);
int v=gec(s2);
int fu=getf(u),fv=getf(v);
if(fu!=fv) f[fv]=fu;
cnt[u]++;
cnt[v]++;
}
int s=,x=;
for(i=;i<=N;++i)
{
if(cnt[i]%==) s++;
if(i==getf(i))x++;
}
if(x<=&&(s==||s==)) puts("Possible");
else puts("Impossible");
return ;
}

poj 2513 欧拉图/trie的更多相关文章

  1. Colored Sticks POJ - 2513(trie树欧拉路)

    题意: 就是无向图欧拉路 解析: 不能用map..超时 在判断是否只有一个联通的时候,我比较喜欢用set,但也不能用set,会超时,反正不能用stl emm 用trie树来编号就好了 #include ...

  2. poj 2513 Colored Sticks (trie 树)

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

  3. poj 3321 Apple Trie

    /* poj 3321 Apple Trie 这道题的关键是如何将一个树建成一个一维数组利用树状数组来解题! 可以利用dfs()来搞定,我们在对一个节点深搜后,所经过的节点的数目就是该节点的子树的数目 ...

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

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

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

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

  6. poj 2513 欧拉回路+并查集推断是否联通+Trie树

    http://poj.org/problem? id=2513 最初看到 第一感觉---map  一看250000的数据量 果断放弃 然后记得曾经看过.trie取代map.尤其当数据量特别大的时候 学 ...

  7. POJ 2513 trie树+并查集判断无向图的欧拉路

    生无可恋 查RE查了一个多小时.. 原因是我N define的是250500 应该是500500!!!!!!!!! 身败名裂,已无颜面对众人.. 吐槽完了 我们来说思路... 思路: 判有向图能否形成 ...

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

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

  9. poj 2513

    http://poj.org/problem?id=2513 73348K        1438MS        C++        1614B解题思路:欧拉路的应用 要点 :1.判断连通性   ...

随机推荐

  1. ftp 协议分析

    File Transfer Protocol(文件传输协议) 使用SOCKET实现 FTP的客户端协议规则: .h #pragma once #include <string> #incl ...

  2. 解释*args和**kwargs的含义

    当我们不知道向函数传递多少参数时,比如我们向传递一个列表或元组,我们就使用*args def func(*args): for i in args: print(i) func(3,2,1,4,7) ...

  3. CuteEditor.Editor+a+a+c+a+a.a() System.RuntimeType.get_Assembly() 问题解决方法

    问题: Server Error in '/' Application. Attempt by method 'CuteEditor.Editor+a+a+c+a+a.a()' to access m ...

  4. Ubuntu 16.04 php卸载

    1.卸载 apache2 sudo apt-get --purge remove apache2* sudo apt-get autoremove apache2 (--purge 是完全删除并且不保 ...

  5. MFC输出调试信息

    刚学mfc时只知道用MessageBox输出,可是MessageBox只能输出字符串, 对于习惯于printf的我来说非常不便,后来查了一下mfc可以像printf一样输出, 就是TRACE这个宏,用 ...

  6. Git版本控制系统VCS

    Git版本控制系统VCS 一.版本控制系统基本情况说明 版本控制是一种记录一个或者若干个文件内容的变化,以便将来查阅特定版本修订情况的系统 1.作用 记录文件的所有历史变化 随时可回复到任何一个历史状 ...

  7. [POI2009]Slw

    题目 神题!!只有\(POI\)出得出来的神题!! 只能说好像懂了,不想听蒟蒻废话就右转\(dalao\)的博客 目前网上除官方外仅三篇题解,由于推论无法直观得出且有点复杂,难免不好理解,手玩数据最重 ...

  8. HDU 4783 Clumsy Algorithm

    题意不提. 我们可以发现,可以将最终序列分为对于第i个位置i-pi>=0与i-pi<0种两个子序列.且如果f[n]==g[n],则有两个子序列都递增. 原因是f[n]表示1-n这个排列的逆 ...

  9. 老司机也该掌握的MySQL优化指南

    当MySQL单表记录数过大时,增删改查性能都会急剧下降,所以我们本文会提供一些优化参考,大家可以参考以下步骤来优化: 除非单表数据未来会一直不断上涨,否则不要一开始就考虑拆分,拆分会带来逻辑.部署.运 ...

  10. numpy模块之创建矩阵、矩阵运算

    本文参考给妹子讲python  https://zhuanlan.zhihu.com/p/34673397 NumPy是Numerical Python的简写,是高性能科学计算和数据分析的基础包,他是 ...