周赛-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大的数字. 输入 多组输 ...
 
随机推荐
- 通用窗口类 Inventory Pro 2.1.2 Demo1(下)
			
本篇想总结的是Inventory Pro中通用窗口的具体实现,但还是要强调下该插件的重点还是装备系统而不是通用窗口系统,所以这里提到的通用窗口类其实是通用装备窗口类(其实该插件中也有非装备窗口比如No ...
 - TTabControl、TMemo组件(制作一个简单的多文本编辑框)
			
TTabControl包含一列字符串标签的tabs 每个标签控制一个对象 首先创建一个TForm;接下来添加TTabControl组件和一个文件对话框TOpenDialog(用于添加文件),然后在TT ...
 - poj: 2739
			
挺简单,生成素数表之后建个全素数的vector,然后..随便玩咯 #include <iostream> #include <stdio.h> #include <str ...
 - eclipse中修改maven仓储
			
1.找到maven的setting文件,修改setting文件: 2.打开eclipce,window->Preference->maven->user Setting
 - Codeforces Round #325 (Div. 1) D. Lizard Era: Beginning
			
折半搜索,先搜索一半的数字,记录第一个人的值,第二个人.第三个人和第一个人的差值,开个map哈希存一下,然后另一半搜完直接根据差值查找前一半的答案. 代码 #include<cstdio> ...
 - paper 19 :机器学习算法(简介)
			
本来看了一天的分类器方面的代码,乱乱的,索性再把最基础的概念拿过来,现总结一下机器学习的算法吧! 1.机器学习算法简述 按照不同的分类标准,可以把机器学习的算法做不同的分类. 1.1 从机器学习问题角 ...
 - scan design flow(一)
			
一个典型的scan实现的flow: clock mux和一些rst,在Scan中都被bypass掉,是不能测到的.所以DFT的test coverage一般就在97%或98%. scan design ...
 - SQLServer查询速度慢的原因
			
查询速度慢的原因很多,常见如下几种: 1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) 2.I/O吞吐量小,形成了瓶颈效应. 3.没有创建计算列导致查询不优化. 4.内存 ...
 - JVM复习笔记
			
1. JVM是什么? JVM是Java Virtual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来 ...
 - 【python cookbook】【数据结构与算法】15.根据字段将记录分组
			
问题:想根据字典或者对象实例的某个特定的字典(比如日期)来分组迭代数据 解决方案:itertools.groupby()函数在对数据进行分组时特别有用(前提是先以目标字典进行排序) rows = [ ...