主题链接:

http://poj.org/problem?

id=2513

Colored Sticks
Time Limit: 5000MS   Memory Limit: 128000K
Total Submissions: 30955   Accepted: 8159

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

[

problem_id=2513" style="text-decoration:none">Submit]   [Go Back]   [

problem_id=2513" style="text-decoration:none">Status]  
[Discuss]

题目意思:

有n条木棒。每条木棒两端有两种颜色,求这些木棒是否能能在一起,使得前一个木棒的后端颜色和前一个木棒的前端颜色一样。

解题思路:

欧拉通路+字典树+并查集

注意是无向边。注意要推断连通性。直接用map映射会超时,自己写字典树。

代码:

//#include<CSpreadSheet.h>

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std; #define Maxn 550000 int dei[Maxn],a[Maxn],cnt,la; struct Node
{
struct Node * next[30];
int nu;
}node[Maxn*15],root; int Insert(char *a)
{
Node * p=&root; while(*a)
{
if(p->next[*a-'a'+1]==NULL)
{
node[++la].nu=0;
memset(node[la].next,NULL,sizeof(node[la].next));
p->next[*a-'a'+1]=&node[la];
}
p=p->next[*a-'a'+1];
a++;
}
if(!p->nu)
p->nu=++cnt;
return p->nu; } int Find(int x)
{
int temp=x;
while(x!=fa[x])
x=fa[x];
while(fa[temp]!=x)
{
int cur=fa[temp];
fa[temp]=x;
temp=cur;
}
return x;
}
void Unio(int a,int b)
{
a=Find(a),b=Find(b);
if(a!=b)
fa[a]=b;
}
int main()
{ memset(dei,0,sizeof(dei));
string a,b;
char sa[15],sb[15];
cnt=la=0;
for(int i=1;i<=Maxn-5000;i++)
fa[i]=i; memset(root.next,NULL,sizeof(root.next));
root.nu=0; while(~scanf("%s%s",sa+1,sb+1))
{
int a=Insert(sa+1);
int b=Insert(sb+1); dei[a]++,dei[b]++;
Unio(a,b);
}
bool ans=true;
int nui=0,nuo=0,la=-1; for(int i=1;i<=cnt;i++)
{
if(la==-1)
la=Find(i);
else if(la!=Find(i))
{
ans=false;
break;
}
if(dei[i]&1)
nui++;
}
if(!ans||nui>2||nui==1)
printf("Impossible\n");
else
printf("Possible\n");
//system("pause"); return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

[欧拉] poj 2513 Colored Sticks的更多相关文章

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

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

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

    http://poj.org/problem?id=2513 题意: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 思路: 题目很明 ...

  3. POJ 2513 Colored Sticks (离散化+并查集+欧拉通路)

    下面两个写得很清楚了,就不在赘述. http://blog.sina.com.cn/s/blog_5cd4cccf0100apd1.htmlhttp://www.cnblogs.com/lyy2890 ...

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

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

  5. POJ 2513 Colored Sticks

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 28036   Accepted: 7428 ...

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

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

  7. poj 2513 Colored Sticks (trie 树)

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

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

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

  9. POJ 2513 Colored Sticks - from lanshui_Yang

    题目大意:给定一捆木棒,每根木棒的每个端点涂有某种颜色.问:是否能将这些棒子首位项链,排成一条直线,且相邻两根棍子的连接处的颜色一样. 解题思路:此题是一道典型的判断欧拉回路或欧拉通路的问题,以木棍的 ...

随机推荐

  1. springboot 开发入门,及问题汇总

    1 . springboot简单介绍(http://projects.spring.io/spring-boot/) 现在的web项目几乎都会用到spring框架,而要使用spring难免需要配置大量 ...

  2. uva 1025

    紫皮书 非原创…… 某城市的地铁是线性的有n个车站从左到右编号为1-n,有M1辆地铁从第一站出发,有M2辆车从最后一站出发,mario从第一站出发,目的是在时刻T会见车站n的一个朋友(间谍).在车站等 ...

  3. DAG上的动态规划

    嵌套矩形问题(最长路及其字典序)有n个举行,选出尽量多的矩阵排成一排,使得除了最后一个之外,每一个矩形可以嵌套在下一个矩形内,并且打印 #include <iostream> #inclu ...

  4. linux exec和文件描述符妙用技巧(转)

    最近在看<精通unix shell脚本编程>时,看到exec<$1 exec 1>$OUTFILE,一下看的我就蒙了.网上看了大半天,终于搞定,记录如下.对于 Linux 而言 ...

  5. ActiveX控件的安全初始化和脚本操作 和 数字签名SIGN

    摘要:数字签名SIGN保证控件在下载时候的安全性.如果你的代码已经经过数字签名,即使用户IE的安全设置很高也能下载,安装并登记.但是在页面上初始化,或者用脚本运行这个控件,为了保证安全性,还需要进行M ...

  6. 利用jquery+iframe做一个ajax上传效果

    以下是自学it网--中级班上课笔记 网址:www.zixue.it html页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict ...

  7. 蜘蛛侠天堂,打死我mac键盘

    今天是周末,在我租住的房间,以一个离奇事件. 一个蜘蛛侠天堂,趴在我的假期车窗玻璃,爬下沿着玻璃.特别糟糕.可以飞檐走壁. 可惜体力不支,摔死在我的mac键盘上. 有图有视频. 视频链接:http:/ ...

  8. Mybatis+Struts2的结合:实现用户插入和查找

    总结一下今天一个成功的小实验:Mybatis+Struts2的结合:实现用户插入和查找.删除和修改如果以后写了,会继续更新. 一 准备工作. 1.新建一个java web项目. 2.在webConte ...

  9. 如何制作python安装模块(setup.py)

    Python模块的安装方法: 1. 单文件模块:直接把文件拷贝到$python_dir/lib 2. 多文件模块,带setup.py:python setup.py install 3. egg文件, ...

  10. 移动app接口编程技术-学习实现之PHP进阶 数组

    数组创建,初始化 <?php //请创建一个数组变量arr,并尝试创建一个索引数组 /** * 不带初始值的创建.创建后赋予值 * 注:下标一定是整数 */ $arr = array(); $a ...