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

Colored Sticks
Time Limit: 5000MS Memory Limit: 128000K
Total Submissions: 32466 Accepted: 8570

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
 
给你n个木棒,两端都有颜色,问相同颜色连在一起,所有木棒能否连成一条直线
map试了许多次,一直TLE,还是字典树吧,最后还有空数据,又送了几遍WA。。。
先用并查集判一下图是否连通,然后一个条件是全部点的度都为偶数 或 只有两个点的度为奇数
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <stdlib.h>
#include <algorithm>
#include <cmath>
#include <map>
#define inf 0x3f3f3f3f
#define N 500010
using namespace std;
struct node
{
    int s[27];
    int id;
} q[N*2];
int n,m;
int a[N],v[N];
int bin[N];
int findx(int x)
{
    int j;
    int r=x;
    while(r!=bin[r])
    {
        r=bin[r];
    }
    int i=x;
    while(i!=r)
    {
        j=bin[i];
        bin[i]=r;
        i=j;
    }
    return r;
}
void merge(int x,int y)
{
    int fx,fy;
    fx=findx(x);
    fy=findx(y);
    if(fx!=fy)
        bin[fx]=fy;
}
int Trie(char a[])
{
    int len=strlen(a);
    int x=0;
    for(int i=0; i<len; i++)
    {
        if(q[x].s[a[i]-'a']==0)
        {
            q[x].s[a[i]-'a']=++m;
            for(int j=0; j<26; j++)
                q[m].s[j]=0;
            x=m;
            q[x].id=0;
        }
        else
            x=q[x].s[a[i]-'a'];
    }
    if(q[x].id==0)
    {
        q[x].id=++n;
        bin[n]=n;
    }
    return q[x].id;
}
int main()
{

    char s1[20],s2[20];
    memset(a,0,sizeof(a));
    memset(v,0,sizeof(v));
    for(int i=0; i<26; i++)
        q[0].s[i]=0;
    bool flag1=false;
    n=0;
    m=0;
    while(~scanf("%s%s",s1,s2))
    {
        int xx=Trie(s1);
        int yy=Trie(s2);
        a[xx]++;
        a[yy]++;
        v[xx]=1;
        v[yy]=1;
        merge(xx,yy);
        flag1=true;
    }
    if(!flag1)
    {
        printf("Possible\n");
        return 0;
    }
    bool flag2;
    for(int i=0; i<n; i++)
    {
        flag2=false;
        if(v[i])
        {
            for(int j=i+1; j<n; j++)
            {
                if(v[j])
                {
                    if(findx(i) != findx(j))
                    {
                        flag2=true;;
                        break;
                    }
                }
            }
            break;
        }
    }
    if(flag2)
    {
        printf("Impossible\n");
        return 0;
    }
    int k1=0;
    bool flag3=false;
    for(int i=0; i<n; i++)    
    {
        if(v[i])
        {
            if(a[i]%2==1)
            {
                k1++;
                if(k1>2)
                {
                    flag3=true;
                    break;
                }
            }
        }
    }
    if(flag3 || k1==1) printf("Impossible\n");
    else printf("Possible\n");
    return 0;
}



                
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、下4载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、下4载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值