POJ———2513Colored Sticks(2)

Colored Sticks
Time Limit: 5000MS Memory Limit: 128000K
Total Submissions: 34995 Accepted: 9146

提示:字典树+并查集+欧拉回路;

注意存在空的情况,应输出possib

欧拉回路:如果一个无向图是连通的,且最多只有两个奇点(度数为奇数),则一定存在欧拉回路。

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.

Source

The UofA Local 2000.10.14

三种开内存的方法;
一·动态开内存,动态处理;
#include <iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
#include<algorithm>
#define N 250000
using namespace std;
int  g,ans,k;
struct node
{
    int cnt;
    int id;
    node *next[28];
} ;
 int fa[N*2];
node *creat()
{
    node *p=new node;
    p->cnt=0;
    p->id=0;
    memset(p->next,0,sizeof(p->next));
    return p;
}
int build(node *rt,char *s)
{
    int len=strlen(s),j;
    node *p=rt;
    for(int i=0;i<len;i++)
    {
        j=s[i]-'a';
        if(p->next[j]==NULL)
        {
            p->next[j]=creat();
        }
        p=p->next[j];
    }
    if(p->id)
    {
        p->cnt++;
        if(p->cnt%2==0)
            ans--;
        else
            ans++;
        return p->id;
    }
    else
    {
        p->id=++k;
        p->cnt++;
        ans++;
        return p->id;
    }
}
int fint(int x)
{
    return fa[x]!=x?fa[x]=fint(fa[x]):x;
}
void bing(int x,int y)
{
    int xx,yy;
    xx=fint(x);
    yy=fint(y);
    if(xx!=yy)
    {
        if(yy>xx)
            fa[yy]=xx;
        else
            fa[xx]=yy;
    }
}
int main()
{
    char s[11],c[11];
    for(int i=0;i<=500000;i++)
    {
        fa[i]=i;
    }
    ans=0,k=0,g=0;
    int pos_1,pos_2;
    node* rt=creat();
    while(~scanf("%s%s",s,c))
    {
        pos_1=build(rt,s);
        pos_2=build(rt,c);
        bing(pos_1,pos_2);
    }
    int ant=0;
    for(int i=1;i<=k;i++)
    {
        if(fa[i]==i)
        {
            ant++;
            if(ant>=2)
                break;
        }
    }
    if(ant==0||(ant==1&&ans<=2))
        printf("Possible\n");
    else
        printf("Impossible\n");
    return 0;
}
二,静态开内存,(假装动态处理)
#include <iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
#include<algorithm>
#define N 250000
using namespace std;
int  g,ans,k;
struct node
{
    int cnt;
    int id;
    node *next[28];
} ;
 int fa[N*2];
node *creat()
{
    node *p=new node;
    p->cnt=0;
    p->id=0;
    memset(p->next,0,sizeof(p->next));
    return p;
}
int build(node *rt,char *s)
{
    int len=strlen(s),j;
    node *p=rt;
    for(int i=0;i<len;i++)
    {
        j=s[i]-'a';
        if(p->next[j]==NULL)
        {
            p->next[j]=creat();
        }
        p=p->next[j];
    }
    if(p->id)
    {
        p->cnt++;
        if(p->cnt%2==0)
            ans--;
        else
            ans++;
        return p->id;
    }
    else
    {
        p->id=++k;
        p->cnt++;
        ans++;
        return p->id;
    }
}
int fint(int x)
{
    return fa[x]!=x?fa[x]=fint(fa[x]):x;
}
void bing(int x,int y)
{
    int xx,yy;
    xx=fint(x);
    yy=fint(y);
    if(xx!=yy)
    {
        if(yy>xx)
            fa[yy]=xx;
        else
            fa[xx]=yy;
    }
}
int main()
{
    char s[11],c[11];
    for(int i=0;i<=500000;i++)
    {
        fa[i]=i;
    }
    ans=0,k=0,g=0;
    int pos_1,pos_2;
    node* rt=creat();
    while(~scanf("%s%s",s,c))
    {
        pos_1=build(rt,s);
        pos_2=build(rt,c);
        bing(pos_1,pos_2);
    }
    int ant=0;
    for(int i=1;i<=k;i++)
    {
        if(fa[i]==i)
        {
            ant++;
            if(ant>=2)
                break;
        }
    }
    if(ant==0||(ant==1&&ans<=2))
        printf("Possible\n");
    else
        printf("Impossible\n");
    return 0;
}

三,纯静态处理

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#define LL long long
using namespace std;
struct node
{
    bool vis;
    int id;
    int cnt;
    int next[28];
}T[1010000];
const int N=3e5+10;
int top;
int pos;
int ans;
int creat()
{
    T[top].vis=0;
    memset(T[top].next,-1,sizeof(T[top].next));
    return top++;
}
int build(char *s,int rt)
{
    int j;
    for(int i=0;s[i];i++)
    {
        j=s[i]-'a';
        if(T[rt].next[j]==-1)
            T[rt].next[j]=creat();
        rt=T[rt].next[j];
    }
    if(!T[rt].vis)
    {
        T[rt].vis=1;
        T[rt].id=pos++;
        T[rt].cnt=1;
        ans++;
    }
    else
    {
        T[rt].cnt++;
        if(T[rt].cnt%2)
            ans++;
        else
            ans--;

    }
    return T[rt].id;
}
int fa[N<<1];
int fint(int x)
{
    return fa[x]!=-1?fa[x]=fint(fa[x]):x;
}
void bing(int x,int y)
{
    int xx=fint(x),yy=fint(y);
    if(xx!=yy)
    {
        fa[xx]=yy;
    }
}
int main()
{
    char s[12],t[12];
    int u,v;
    top=pos=ans=0;
    int rt=creat();
    memset(fa,-1,sizeof(fa));
    while(~scanf("%s%s",s,t))
    {
         u=fint(build(s,rt));
         v=fint(build(t,rt));
         bing(u,v);
    }
    int flag=0;
    for(int i=0;i<pos;i++)
    {
        if(fa[i]==-1)
            {
                flag++;
                if(flag>1)
                    break;
            }
    }
   if(flag==0||flag==1&&(ans<=2))
        printf("Possible\n");
   else
        printf("Impossible\n");
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值