周赛-Colored Sticks 分类: 比赛 2015-08-02 09:33 7人阅读 评论(0) 收藏
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) 收藏的更多相关文章
- ASIHTTPRequest异步请求 分类: ios技术 2015-03-01 09:33 48人阅读 评论(0) 收藏
我们运行程序,如果网速很慢,查询的时候会一直黑屏,直到请求结束画面才出现,这样用户体验很不好.因此同步请求一般只是在某个子线 程中使用,而不在主线程中使用.异步请求的用户体验要比同步请求好,因此一般情 ...
- 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 ...
- iOS开源库--最全的整理 分类: ios相关 2015-04-08 09:20 486人阅读 评论(0) 收藏
youtube下载神器:https://github.com/rg3/youtube-dl 我擦咧 vim插件:https://github.com/Valloric/YouCompleteMe vi ...
- 基于命令行编译打包phonegap for android应用 分类: Android Phonegap 2015-05-10 10:33 73人阅读 评论(0) 收藏
也许你习惯了使用Eclipse编译和打包Android应用.不过,对于使用html5+js开发的phonegap应用,本文建议你抛弃Eclipse,改为使用命令行模式,绝对的快速和方便. 一直以来,E ...
- Jquery easy UI 上中下三栏布局 分类: ASP.NET 2015-02-06 09:19 368人阅读 评论(0) 收藏
效果图: 源代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...
- C# IIS应用程序池辅助类 分类: C# Helper 2014-07-19 09:50 249人阅读 评论(0) 收藏
using System.Collections.Generic; using System.DirectoryServices; using System.Linq; using Microsoft ...
- PIGS 分类: POJ 图论 2015-08-10 09:15 3人阅读 评论(0) 收藏
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18209 Accepted: 8277 Description Mir ...
- Babelfish 分类: 哈希 2015-08-04 09:25 2人阅读 评论(0) 收藏
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 36398 Accepted: 15554 Descripti ...
- 排序练习——找出前m大的数字 分类: 排序 2015-06-08 09:33 21人阅读 评论(0) 收藏
排序练习--找出前m大的数字 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 给定n个数字,找出前m大的数字. 输入 多组输 ...
随机推荐
- 脚本:SQLServer 2008 生成某数据库中的所有索引创建脚本
--1. get all indexes from current db, place in temp table select schemaName = s.name, tablename = ob ...
- UEditor和CKEditor配置上传图片,视频,附件
UEditor: http://blog.sina.com.cn/s/blog_8bb128230102v12x.html CKEditor:http://blog.sina.com.cn/s/blo ...
- ACM之Java速成(3)
ACM中Java.大数处理 先上个代码: import java.math.*; import java.util.*; class Main{ public static void main(Str ...
- zjuoj 3609 Modular Inverse
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3609 Modular Inverse Time Limit: 2 Seco ...
- UML: 活动图
摘自http://www.umlonline.org/school/thread-36-1-1.html 活动图和流程图很类似,我们看看一个流程图的例子: 活动图是用来描述流程的一种图,它与流程图的不 ...
- .net 中 ref out params的区别
C#中有三个关键字-ref,out ,params,虽然本人不喜欢这三个关键字,因为它们疑似破坏面向对象特性.但是既然m$把融入在c#体系中,那么我们就来认识一下参数修饰符ref,out ,param ...
- thinkphp介绍
1.thinkphp是一个免费的开源的轻量级的高效的国产的php框架 2.现在主流的框架有: zend framwork 框架,功能十分齐全,是php官网开发的一个框架 yii框架 十分轻巧的 ...
- const修饰
const int A() //const // ====>int A(const this) { //观点1:const是修饰a,但是通过测试,我们发现,b++也不能编译通过 //这说明:co ...
- android waiting for debugger
在Studio开发环境中,使用真机开发经常会出现waiting for debugger,卡死不动了,让人很崩溃啊,现在我就总结下几种解决方法,希望能帮到出现同样情况的朋友! 问题出现及解决办法: 多 ...
- cmd进入某个目录
love you my pig java枚举类 cmd进入某个目录 2011-04-06 15:49:38| 分类: 小知识 | 标签: |字号大中小 订阅 1.开始->运行->C ...