poj Colored Sticks(字典树 欧拉回路的判断 并查集)

解决一个有趣的问题:如何判断一组两端涂有不同颜色的木棍能否首尾相连形成直线,使得相邻木棍的颜色匹配。使用C++实现了一种数据结构来高效地存储和查询木棍的颜色信息,并通过并查集来判断所有木棍是否能够按要求排列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

E - Colored Sticks
Time Limit:5000MS     Memory Limit:128000KB     64bit IO Format:%I64d & %I64u

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

Hint

Huge input,scanf is recommended.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct  node
{
    int x;
    struct node *next[26];
};
struct node *root;
int du[501100];
int pre[501000];
int cnt;

struct node *creat()
{
   struct node *p;
   p=new node;
   p->x=0;
   for(int i=0;i<26;i++)
    p->next[i]=NULL;
};

void build(char *s,int x)
{
    struct node *p=root;

    int i;
    for(i=0;s[i];i++)
    {
        int k=s[i]-'a';
        if(p->next[k]==NULL)
        {
            p->next[k]=creat();
        }
        p=p->next[k];
    }
    p->x=x;
}

int search(char *s)
{
    struct node *p=root;
    int i;
    for(i=0;s[i];i++)
    {
        int k=s[i]-'a';

        if(p->next[k]==NULL) return 0;
        p=p->next[k];
    }
    return p->x;
}
void intit()
{
    int i;
    memset(du,0,sizeof(0));
    for(i=1;i<=51000;i++)
        pre[i]=i;
}
int find(int x)
{
    if(x!=pre[x])
    pre[x]=find(pre[x]);
    return pre[x];
}

int judge()
{
    int i,j,sum=0;
    for(i=1;i<=cnt;i++)
    {
        if(du[i]%2==1) sum++;
    }
    //printf("sum ---%d\n",sum);
    if(sum!=2&&sum!=0) return 0;
    int k=find(1);
    //printf("k=+++++%d\n",k);
    for(i=1;i<=cnt;i++)
    {  //printf("+++++%d\n",find(i));
        if(k!=find(i))
            return 0;
    }
    return 1;

}
int main()
{
    char s1[1000],s2[1000];
    root=creat();
     cnt=0;
    intit();
    while(~scanf("%s%s",s1,s2))
    {
        int x=search(s1);
        int y=search(s2);
        if(x==0) build(s1,x=++cnt);
        if(y==0) build(s2,y=++cnt);
        //printf("%d %d\n",x,y);
        du[x]++;
        du[y]++;
        int dx=find(x);
        int dy=find(y);
        //printf("cnt = %d\n",cnt);
        //printf("-=-=-=-=%d %d\n",dx,dy);
        if(dx!=dy)
            pre[dx]=dy;


    }
      if(judge())
    printf("Possible\n");
    else printf("Impossible\n");

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值