周赛-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大的数字. 输入 多组输 ...
随机推荐
- weka特征选择(IG、chi-square)
一.说明 IG是information gain 的缩写,中文名称是信息增益,是选择特征的一个很有效的方法(特别是在使用svm分类时).这里不做详细介绍,有兴趣的可以googling一下. chi-s ...
- Python:操作文件
python操作文件库不需要安装其他module,文件操作类库是python语言自身支持的操作. 判定文件是否存在:os.path.isfile(filePath) import os import ...
- win7无线网连接了,但是图标显示未连接
第一步: 打开控制面板,找到“管理工具”->“计算机管理” 第二步: 在控制台左边栏,选择“设备管理器”,然后在右侧展开“网络适配器” 第三步: 在每一个网络设备上点鼠标右键,然后选择“卸载”. ...
- [reprint]malloc与calloc的区别
[http://blog.163.com/crazy20070501@126/] 转自某自由人的博客: malloc与calloc的区别 函数malloc()和calloc()都可以用来动态分配内存空 ...
- [摘录]quarts :overview
最近项目用了quarts 这个 任务调度器.今天开始详细的学习学习.参考链接:http://www.quartz-scheduler.org/# Quartz Enterprise Job Sched ...
- 用Appium去操作移动设备上的chrome
最近在积极努力的学习Appium,今天成功运行了官网上的demo,在此做一个小小的总结: 前期准备工作: (1)在要运行的真机或模拟器上安装chrome. 注意:x86的虚拟机是不支持的,但是经过本人 ...
- 1019: A+B和C比大小
1019: A+B和C比大小 时间限制: 1 Sec 内存限制: 128 MB提交: 518 解决: 300[提交][状态][讨论版] 题目描述 给定区间[-231, 231]内的3个整数A.B和 ...
- 夺命雷公狗ThinkPHP项目之----企业网站8之栏目的添加完善(无限极分类的完成)
我们刚才只是完成了添加的一部分,但是我们的上级分类也不能永远都是只有一个死的嘛,所以我们需要对她进行修改: 我们先将add方法里面的数据查出来再说: 然后在模板页进行遍历: 展示效果如下所示: 虽然是 ...
- PTPX中的activity文件以及mapping文件
在不同的simulation中的capturing switching activity: SAIF:Switching Activity Interface Format,包含toggle coun ...
- PAT乙级 1007. 素数对猜想 (20)
1007. 素数对猜想 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 让我们定义 dn 为:dn = ...