欧拉通路 字典树标号+并查集

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.

这里好像不能用map标号,这里利用字典树对每一个字符串进行标号,之后利用并查集判断图的连通性,再然后利用度数判别是不是欧拉通路。欧拉通路条件:图联通  入度=出度(对于每一个节点)或只有两个异常点 一个入-出=1,一个出-入=1

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const double PI=acos(-1.0);//
int fact[10]= {1,1,2,6,24,120,720,5040,40320,362880};
const int maxn= 5000010;
int in[maxn],ou[maxn];
int tot;
int trie[maxn][26];// 最多50w个单词  开50w*10 (居然没爆 md)
int fa[maxn];
int find(int x)
{
    return x==fa[x]?x:fa[x]=find(fa[x]);//
}
void unions(int x,int y)
{
    int fx=find(x),fy=find(y);
    fa[fx]=fy;
}
int inserts(char *s)
{
    int root=0;
    int len=strlen(s);
    for(int i=0; i<len; i++)
    {
        int id=s[i]-'a';//
        if(!trie[root][id])
        {
            trie[root][id]=++tot;
        }
        root=trie[root][id];
    }
    return root;//root表示某一个字母的标号  虽然数字分布的很宽   不过空间换时间
}
bool v[maxn];
int main()
{
    char s1[20],s2[20];
    int x,y;
    int cas=0;
    memset(v,false,sizeof(v));
    for(int i=1; i<=maxn; i++)
        fa[i]=i;
    while(scanf("%s%s",s1,s2)!=EOF)
    {
        x=inserts(s1);//得到标号
        y=inserts(s2);
        in[x]++;//统计出度  和  入度  欧拉回路判定标准
        ou[y]++;
        unions(x,y);//将可以连着的木棍并查集并起来  防止出现两个欧拉回路
        v[x]=true;//由于之前的root分布的太广了  所以用v[]来表示某一个数字是否代表了一种颜色  不是的话就直接跳过  节约时间
        v[y]=true;
    }
    int f1=0,m=-1,flag=0;
    for(int i=1; i<=tot; i++)
    {
        if(v[i])
            if(find(i)!=m)
            {
                if(!flag)
                {
                    m=find(i);
                    flag=1;
                }
                else  //如果连通图超过两个就不对了
                {
                    printf("Impossible\n");
                    return 0;
                }
            }
        if(!v[i]||in[i]==ou[i])
            continue;
        if((in[i]+ou[i])%2)
        {
            f1++;
        }
        if(f1>2)//如果奇数点超过两个就不对了
        {
            printf("Impossible\n");
            return 0;
        }
    }
    if(f1==1)
    {
        printf("Impossible\n");
        return 0;
    }
    printf("Possible\n");

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值