周赛-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大的数字. 输入 多组输 ...
随机推荐
- Azure billing 分析
昨天把西欧的2012的VM删掉,在北美新建一个2008的VM,装了sql2005 express 在C盘,这样存储就变成2个位置了,西欧和美国,然后放在那里不操作一天,发现billing多了很多, S ...
- eclipse lua使用
首先安装lua eclipse,装插件或者独立版的都可以.但是在独立版的eclipse装subclipse会报错,Failed to prepare partial IU.解决办法: work aro ...
- NABCD模式
各位用户: 我们的“昵妆”是为了帮助不会化妆的用户解决困难, 他们需要有适合他们的优质的化妆品和 正确的视频或者化妆师来指导他们,但是现有的方案并没有很好地解决这些需求,我们有独特的办法, ...
- Snapchat面经(师兄的)
给一个LinkedList环,给其中任一个节点的reference,求删去LinkedList中所有value=k的点 我的想法:假设给定的点事ListNode oneNode, 设置ListNode ...
- SQL Sever 身份验证 sa用户设置
1.用windows身份验证登陆数据库找到sa用户 2.鼠标右键sa->属性->常规,设置密码. 3.选择状态->登陆选择已启用 4.选中当前数据库 鼠标右键->属性 5.选择 ...
- C# 动态链接库的创建
首先在C#工程下面安装第三方插件包 安装方法:Tools --> Library Package Manager --> Package Manager Console Install-P ...
- php CI框架目录结构及运行机制
CI目录结构 CI主要组成部分为,application(应用文件夹).system(系统文件夹)和index.php入口文件. 应用文件夹中主要是存放控制器.模型和视图等,系统文件夹中主 ...
- BETWEEN and
select * from ( select *,ROW_NUMBER() over (ORDER BY AddTime desc) RowNumber from Product where ID n ...
- Power Gating的设计(模块二)
针对lower power的验证,由cpf/upf来建模,包括: 1)power gating的功能模型(在power gate之后将output force为x) 2)isolation功能模型: ...
- getResource().getPath()返回的路径空格变成了 %20
this.getClass().getResource(“/”).getPath()使用者方法查看文件在服务器上的地址,但是地址中的空格会被转化为%20. 解决办法1: URI uri = new U ...