主题链接:

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. Spring MVC Controller与jquery ajax请求处理json

    在用 spring mvc 写应用的时候发现jquery传递的[json数组对象]参数后台接收不到,多订单的处理,ajax请求: "}]}]} $.ajax({ url : url, typ ...

  2. U3D——Unity3D的脚本-script入门

     Unity3D的基本操作非常easy就能掌握了,接下来就是游戏系统的核心部分:脚本. 什么是Script(脚本)?简而言之,就是使用代码来运行一系列动作命令的特殊文本,它须要编译器来从新解读.U ...

  3. Office 2013 正式版 下载地址 带正版验证

    万众期待的正式版Office 2013 降临---英文版/中文简体版 英文版软件下载地址: office_professional_plus_2013_x86_dvd en_office_profes ...

  4. 一个解析RTSP 的URL函数

    写了一个解析URL的函数,可以提取URL中的IP 和 port. 如:url = "rtsp://192.168.1.43:2554/realmp3.mp3"; url = &qu ...

  5. bat执行java程序的脚本解析

    使用java执行带Package的class文件java package1.package2.className  或java -cp .  package1.package2.className - ...

  6. Swift - 给表格添加编辑功能(删除,插入)

    1,下面的样例是给表格UITableView添加编辑功能: (1)给表格添加长按功能,长按后表格进入编辑状态 (2)在编辑状态下,第一个分组处于删除状态,第二个分组处于插入状态 (3)点击删除图标,删 ...

  7. 超声波模块SRF05

    //////////////////////////////////////////////////////////////////////////////// // //     PIC16F877 ...

  8. Delphi数据类型转换(有几个字符串函数没见过,比如StringToWideChar和WideCharToString)

    DateTimeToFileDate                  函数                     将DELPHI的日期格式转换为DOS的日期格式         DateTimeT ...

  9. 魔棒工具--RegionGrow算法简介

    原地址:http://www.cnblogs.com/easymind223/archive/2012/07/04/2576964.html ps里面的魔棒工具非常好用,是图像处理中非常常用的一个工具 ...

  10. elf格式分析

    近期研究了一下elf文件格式,发现好多资料写的都比較繁琐,可能会严重打击学习者的热情,我把自己研究的结果和大家分享,希望我的描写叙述可以简洁一些. 一.基础知识 elf是一种文件格式,用于存储Linu ...