POJ Colored Sticks 2513(字典树+并查集+欧拉回路)

4 篇文章 0 订阅
3 篇文章 0 订阅

                                                                                                           Colored Sticks
Time Limit: 5000MS Memory Limit: 128000K
Total Submissions: 35376 Accepted: 9264

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


题意:给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的。

就是判断欧拉回路,把每个颜色映射成点,用字典树,然后用并查集判断图是否连通,最后判断欧拉回路


本题欧拉回路:图必须是连通的,奇度数点必须是0个或者2个(可以是一条直线或者是圆),这样就是欧拉回路,其他则不是

欧拉回路:无向图连通并且所有结点的度都是偶数,则存在欧拉回路,否则不存在

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <stdio.h>
using namespace std;
int top;
int hao;
int pre[510000];
int du[510000];
struct node
{
    int flag;
    int next[27];
}k[250000*5*2];
int newnode()
{
    k[top].flag=0;
    memset(k[top].next,-1,sizeof(k[top].next));
    return top++;
}
int Insert(int p,char s[])
{
    int l,i,j,t;
    l=strlen(s);
    for(i=0;i<l;i++)
    {
        t=int(s[i]-'a');
        if(k[p].next[t]==-1)
        {
            k[p].next[t]=newnode();
        }
        p=k[p].next[t];
    }
    return k[p].flag=++hao;
}
int query(int p,char s[])
{
    int l,i,j,t;
    l=strlen(s);
    for(i=0;i<l;i++)
    {
        t=int(s[i]-'a');
        if(k[p].next[t]==-1)
        {
            return 0;
        }
        p=k[p].next[t];
    }
    return k[p].flag;
}
int Find(int x)
{
    if(x!=pre[x])
    {
        pre[x]=Find(pre[x]);
    }
    return pre[x];
}
void mix(int a,int b)
{
    a=Find(a);
    b=Find(b);
    if(a!=b)
        pre[a]=b;
}
int main()
{
    top=0;
    hao=0;
    memset(du,0,sizeof(0));
    int ll=newnode();
    int i,j;
    for(i=0;i<500000;i++)
    {
        pre[i]=i;
    }
    int x,y;
    char s1[20],s2[20];
    while(scanf("%s%s",s1,s2)==2)
    {
        x=query(ll,s1);
        if(!x)
        {
            x=Insert(ll,s1);
        }
        y=query(ll,s2);
        if(!y)
        {
            y=Insert(ll,s2);
        }
        du[x]++;
        du[y]++;
        mix(x,y);
    }
    int s=Find(1);//图如果连通,所有点的祖先都是s
    int num=0;
    for(i=1;i<=hao;i++)
    {
        if(du[i]%2==1)
        {
            num++;
        }
        if(num>2)
        {
            printf("Impossible\n");
            return 0;
        }
        if(Find(i)!=s)
        {
            printf("Impossible\n");
            return 0;
        }
    }
    if(num==1)
    {
        printf("Impossible\n");
    }
    else
    {
        printf("Possible\n");
    }

    return 0;
}
















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值