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. jQuery中获取特定顺序子元素(子元素种类不定)的方法

    提出问题:只已知父元素和父元素中子元素的次序,怎么通过jQuery方法获得该元素? <p>第一部分:</p> <ul> <li>1</li> ...

  2. 安装和使用jupyter

    安装 pip install jupyter 使用 jupyter notebook

  3. Way to OpenSource

    @1:Follow any instructions in the readme and make sure that you can configure and run the project on ...

  4. Maven- 使用Maven构建一个可执行jar

    How to Create an Executable JAR with Maven 1.最重要的是使用jar类型,<packaging>jar</packaging>.当然不 ...

  5. javascript;先弹出提示框,再跳转到其他页面。

    context.Response.Write("<script>alert('删除成功!" + r.ToString() + "条');window.loca ...

  6. $SVN代码版本管理工具的使用

    SVN是一种代码版本管理工具,具有可视化的操作界面,使用简便,和git的功能类似.下面总结一下SVN的基本用法: 1.安装SVN软件,和安装一般的软件的步骤差不多,这里使用的版本是TortoiseSV ...

  7. octotree神器 For Github and GitLab 火狐插件

    Code tree for GitHub and GitLabExtension to show code tree for GitHub and GitLab. Useful for develop ...

  8. mysql性能优化的一些建议

    mysql性能优化的一些建议 1.EXPLAIN 你的 SELECT 查询 查看rows列可以让我们找到潜在的性能问题. 2.为关键字段添加索引,比如:where, order by, group b ...

  9. HTML5模拟衣服撕扯动画

    在线演示 本地下载

  10. Android电容屏(一)【转】

    本文转载自:http://blog.csdn.net/xubin341719/article/details/7820492 关键词:Android  电容屏 tp  ITO 平台信息:内核:linu ...