Colored Sticks

Time Limit: 5000MS Memory Limit: 128000K

Total Submissions: 32423 Accepted: 8556

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

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

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <list>
#include <algorithm>
#define LL long long
#define RR freopen("output.txt","r",stdoin)
#define WW freopen("input.txt","w",stdout) using namespace std; const int MAX = 2500000; struct node
{
int num;
node *next[26];
}head; int pre[MAX]; int Du[MAX]; int top; char s[15],c[15]; node *Creat()
{
node *p;
p=new node;
p->num=0;
for(int i=0;i<26;i++)
{
p->next[i] = NULL;
}
return p;
} int Build_Tree(char *str)//建立字典树
{
int len=strlen(str);
int a;
node *p=&head;
for(int i=0;i<len;i++)
{
a=str[i]-'a';
if(p->next[a])
{
p=p->next[a];
}
else
{
p->next[a]=Creat();
p=p->next[a];
}
}
if(!p->num)//返回单词所在的位置
{
p->num=++top;
}
return p->num;
}
int Find(int x)//并查集+压缩路径
{
int i=x,j=x,s;
while(pre[i]!=-1)
{
i=pre[i];
}
while(pre[j]!=-1)
{
s=pre[j];
pre[j]=i;
j=s;
}
return i;
}
void Link(int x,int y)
{
int FX=Find(x);
int FY=Find(y);
if(FX!=FY)
{
pre[FX]=FY;
}
}
int main()
{
memset(pre,-1,sizeof(pre));
memset(Du,0,sizeof(Du));
top=0;
for(int i=0;i<26;i++)
{
head.next[i]=NULL;
}
// int n;
// scanf("%d",&n);
//for(int i=0;i<n;i++)
while(~scanf("%s %s",s,c))
{
// scanf("%s %s",s,c);
int u=Build_Tree(s);
int v=Build_Tree(c);
Du[u]++;
Du[v]++;
Link(u,v);
}
int ans=0,ant=0;
for(int i=1;i<=top;i++)
{
if(Du[i]%2)
{
ans++;
}
if(pre[i]==-1)
{
ant++;
}
if(ant>1||ans>2)//如果入度为奇数的超过2个,或者根大于一个就不能连成一条直线
{
break;
}
}
if(ant>1||ans>2)
{
printf("Impossible\n");
}
else
{
printf("Possible\n");
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

周赛-Colored Sticks 分类: 比赛 2015-08-02 09:33 7人阅读 评论(0) 收藏的更多相关文章

  1. ASIHTTPRequest异步请求 分类: ios技术 2015-03-01 09:33 48人阅读 评论(0) 收藏

    我们运行程序,如果网速很慢,查询的时候会一直黑屏,直到请求结束画面才出现,这样用户体验很不好.因此同步请求一般只是在某个子线 程中使用,而不在主线程中使用.异步请求的用户体验要比同步请求好,因此一般情 ...

  2. Hdu 1507 Uncle Tom's Inherited Land* 分类: Brush Mode 2014-07-30 09:28 112人阅读 评论(0) 收藏

    Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  3. iOS开源库--最全的整理 分类: ios相关 2015-04-08 09:20 486人阅读 评论(0) 收藏

    youtube下载神器:https://github.com/rg3/youtube-dl 我擦咧 vim插件:https://github.com/Valloric/YouCompleteMe vi ...

  4. 基于命令行编译打包phonegap for android应用 分类: Android Phonegap 2015-05-10 10:33 73人阅读 评论(0) 收藏

    也许你习惯了使用Eclipse编译和打包Android应用.不过,对于使用html5+js开发的phonegap应用,本文建议你抛弃Eclipse,改为使用命令行模式,绝对的快速和方便. 一直以来,E ...

  5. Jquery easy UI 上中下三栏布局 分类: ASP.NET 2015-02-06 09:19 368人阅读 评论(0) 收藏

    效果图: 源代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...

  6. C# IIS应用程序池辅助类 分类: C# Helper 2014-07-19 09:50 249人阅读 评论(0) 收藏

    using System.Collections.Generic; using System.DirectoryServices; using System.Linq; using Microsoft ...

  7. PIGS 分类: POJ 图论 2015-08-10 09:15 3人阅读 评论(0) 收藏

    PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18209 Accepted: 8277 Description Mir ...

  8. Babelfish 分类: 哈希 2015-08-04 09:25 2人阅读 评论(0) 收藏

    Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 36398 Accepted: 15554 Descripti ...

  9. 排序练习——找出前m大的数字 分类: 排序 2015-06-08 09:33 21人阅读 评论(0) 收藏

    排序练习--找出前m大的数字 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 给定n个数字,找出前m大的数字.   输入  多组输 ...

随机推荐

  1. nodejs发起HTTPS请求并获取数据

    摘要:在网站中有时候需要跨域请求数据,直接用Ajax无法实现跨域,采用其他方式需要根据不同的浏览器做相应的处理.用Nodejs可以很好的解决这些问题,后台引用HTTPS模块,发送和返回的数据均为JSO ...

  2. struts 2.0部署

    环境:linux centos 64位. 1)下载JDK6.0,具体文件名是:jdk-6u45-linux-x64.bin 安装:chmod 755 jdk*.bin ./jdk....bin 设置环 ...

  3. G面经prepare: Data Stream Average

    给一个datastream和一个fixed window size, 让我design一个class可以完成add number还有find average in the window. 就是不能用v ...

  4. 显示和隐藏Mac下的 隐藏文件

    显示:defaults write com.apple.finder AppleShowAllFiles -bool true隐藏:defaults write com.apple.finder Ap ...

  5. 转:python webdriver API 之浏览器的操作

    1.1.浏览器最大化在统一的浏览器大小下运行用例,可以比较容易的跟一些基于图像比对的工具进行结合,提升测试的灵活性及普遍适用性.比如可以跟 sikuli 结合,使用 sikuli 操作 flash.# ...

  6. [Reprint]c++ 析构函数的调用

    析构函数在调用默认的析构函数和用户自己覆写的析构函数的时候有点意识模糊呢.写段代码总结下 #include <iostream> using namespace std; class Bo ...

  7. cell点击按钮崩的一种情况

    这个错误表示:该类方法没有addBtnClick对应的方法: bug的复现:创建一个cell,并且添加一个类方法来赋值,在方法中,给按钮添加一个点击事件(addBtnClick),但是,再实现这个方法 ...

  8. [原创]java WEB学习笔记63:Struts2学习之路--表单标签 用户注册模块

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  9. ibatis学习过程

    ibatis 中map in查询的做法 1:如果传过来的对象就是直接的map数组   list<hashmap<key,value>>的形式  [{1:1},{1:1}}的形式 ...

  10. yii框架各种防止sql注入,xss攻击,csrf攻击

    PHP中常用到的方法有: /*  防sql注入,xss攻击  (1)*/    function actionClean($str)    {        $str=trim($str);      ...