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
题意:给你n(n<=250000)根小木棒,木棒两端染有一些颜色,两根木棒相连需要保证相连端点颜色一致.求这些木棒是否可以全部相连?
题解:可以将木棒理解为边,连接两种颜色,那么全部相连就变成了一笔画的问题,即判断欧拉回路.
欧拉回路需要满足两个条件:
1.度数为奇数的点为0个或2个
2.图联通
现在的问题是如何把字符串变成数字的点,用map是肯定会TLE的
hash应该可以,但tire好写,所以我选择了trie树,存入trie的值为该字符串的映射
图联通用并查集去判断.
代码如下:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; int cnt=,du[],fa[]; struct Trie
{
int sz,tr[][];
int time[]; void clear()
{
sz=;
memset(tr,,sizeof(tr));
} void insert(char* c,int x)
{
int rt=,len=strlen(c);
for(int i=;i<len;i++)
{
if(!tr[rt][c[i]-'a'])
{
tr[rt][c[i]-'a']=++sz;
}
rt=tr[rt][c[i]-'a'];
}
time[rt]=x;
} int search_t(char *c)
{
int rt=,len=strlen(c);
for(int i=;i<len;i++)
{
if(!tr[rt][c[i]-'a'])
{
return ;
}
rt=tr[rt][c[i]-'a'];
}
return time[rt];
} }trie; void mem(int n)
{
for(int i=;i<=n;i++)
{
fa[i]=i;
}
} int find(int x)
{
if(x!=fa[x])
{
fa[x]=find(fa[x]);
}
return fa[x];
} void union_(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
{
fa[fx]=fy;
}
} int euler()
{
int sum=,t=find();
for(int i=;i<=cnt;i++)
{
if(du[i]%==)
{
sum++;
}
}
if(sum!=&&sum!=)
{
return ;
}
for(int i=;i<=cnt;i++)
{
if(find(i)!=find(t))
{
return ;
}
}
return ;
} int main()
{
char s1[],s2[];
mem();
// freopen("1.in","r",stdin);
// freopen("1.out","w",stdout);
while(scanf("%s %s\n",s1,s2)!=EOF)
{
int from,to;
if(!trie.search_t(s1))
{
trie.insert(s1,++cnt);
}
from=trie.search_t(s1);
du[from]++;
if(!trie.search_t(s2))
{
trie.insert(s2,++cnt);
}
to=trie.search_t(s2);
du[to]++;
union_(from,to);
}
if(euler())
{
puts("Possible");
}
else
{
puts("Impossible");
}
}
 

POJ2513 Colored Sticks(Trie+欧拉回路)的更多相关文章

  1. POJ2513——Colored Sticks(Trie树+欧拉回路+并查集)

    Colored Sticks DescriptionYou are given a bunch of wooden sticks. Each endpoint of each stick is col ...

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

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

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

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

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

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

  5. poj2513 Colored Sticks —— 字典树 + 并查集 + 欧拉回路

    题目链接:http://poj.org/problem?id=2513 题解:通过这题了解了字典树.用字典树存储颜色,并给颜色编上序号.这题为典型的欧拉回路问题:将每种颜色当成一个点.首先通过并查集判 ...

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

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

  7. POJ2513:Colored Sticks(字典树+欧拉路径+并查集)

    http://poj.org/problem?id=2513 Description You are given a bunch of wooden sticks. Each endpoint of ...

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

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

  9. poj 2513 Colored Sticks (trie 树)

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

随机推荐

  1. Crypto 加密解密

    import binascii from Crypto.Cipher import AES #秘钥,此处需要将字符串转为字节 from utils import config from utils.e ...

  2. mysql导入导出表结构

    mysql导入导出表结构 导出整个库的表结构如下:mysqldump -uroot -p -d databasename > createtab.sql 如果只想导出表: test1,test2 ...

  3. Eclipse 中 No java virtual machine was found... 解决方法

    这个链接说的不错,http://www.mafutian.net/123.html,,但是还有一种可能是64位和32位的问题,也就是eclipse32位只能用32位的jdk,eclipse64位的只能 ...

  4. Php header()函数及其常见使用

    语法: Void header(string $string[,bool $replace=true [, int $http_response_code) 向客户端发送原始的HTTP报头 需注意: ...

  5. AIX PowerHA (HACMP) Commands

    PowerHA(HACMP) Commands How to start cluster daemons (options in that order:  clstrmgr, clsmuxpd, br ...

  6. HDU 4547 CD操作 (LCA最近公共祖先Tarjan模版)

    CD操作 倍增法  https://i.cnblogs.com/EditPosts.aspx?postid=8605845 Time Limit : 10000/5000ms (Java/Other) ...

  7. Python web框架 Tornado(二)异步非阻塞

    异步非阻塞 阻塞式:(适用于所有框架,Django,Flask,Tornado,Bottle) 一个请求到来未处理完成,后续一直等待 解决方案:多线程,多进程 异步非阻塞(存在IO请求): Torna ...

  8. java常见的几种调用机制(同步调用,异步调用,回调)

    1.同步调用 同步调用是最基本的调用方式,对象b中的方法直接调用对象a的方法,这个时候程序会等待对象a的方法执行完返回结果之后才会继续往下走. 代码如下: public class A {public ...

  9. Setting up logs in NetBackup

    For a given issue, it may be necessary to gather multiple logs.  This MUST cover the time the issue ...

  10. MSComm 串口

    MSComm 串口 http://www.docin.com/p-761416611.html http://blog.sina.com.cn/s/blog_50cfd0fc0102v27p.html