首页 > 代码库 > poj 2513 Colored Sticks 并查集 字典树 欧拉回路判断

poj 2513 Colored Sticks 并查集 字典树 欧拉回路判断

点击打开链接题目链接

Colored Sticks
Time Limit: 5000MS Memory Limit: 128000K
Total Submissions: 30273 Accepted: 8002

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

给出一根棍子两端颜色 颜色一样的可以接在一起

问所有的棍子最后能否接到一起

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<algorithm>
using namespace std;
struct node
{
    int next[26];
    int id;
}trie[250010];
int head;
int father[25001    0];
int du[250010];
int ids=1;
int tree_add(char *str)
{
    int s=0;
    int len=strlen(str);
    int i;
    int w;
    for(i=0;i<len;i++)
    {
        w=str[i]-'a';
        if(trie[s].next[w]==-1)
        {
            trie[s].next[w]=++head;
        }
        s=trie[s].next[w];
    }
    if(trie[s].id==0)
    {
        trie[s].id=ids++;
        return trie[s].id;
    }
    else
        return trie[s].id;
}
int find(int x)
{
    if(x==father[x])
        return x;
    int t=father[x];
    father[x]=find(father[x]);
    return father[x];
}
int main()
{
    char str1[21];
    char str2[11];
    int i;
    int a,b;
    head=0;
    int j;
    for(i=0;i<=250010;i++)
    {
        trie[i].id=0;
        for(j=0;j<26;j++)
        {
            trie[i].next[j]=-1;
        }
        father[i]=i;
        du[i]=0;
    }
    while(gets(str1)!=NULL&&strcmp(str1,"")!=0)
    {
        for(i=0;str1[i]!='\0';i++)
        {
            if(str1[i-1]==' '&&str1[i]!=' ')
            {
                strcpy(str2,str1+i);
                break;
            }
        }
        for(i=0;str1[i]!='\0';i++)
        {
            if(str1[i]==' ')
            {
                str1[i]='\0';
                break;
            }
        }
        a=tree_add(str1);
        b=tree_add(str2);
        du[a]++;
        du[b]++;
       // printf("a = %d b = %d \n",a,b);
        int x=find(a);
        int y=find(b);
        if(x!=y)
        {
            father[x]=y;
        }
    }
    int aaa=0;
    for(i=1;i<=ids-1;i++)
    {
        if(father[i]==i)
        {
            aaa++;
        }
        if(aaa>1)
        {
            printf("Impossible\n");
            return 0;
        }
    }
    aaa=0;
    for(i=1;i<=ids-1;i++)
    {
        if(du[i]%2==1)
        {
            aaa++;
        }
    }
  //  printf("%d \n",aaa);
    if(aaa>2||aaa==1)
    {
        printf("Impossible\n");
    }
    else
    {
        printf("Possible\n");
    }
    return 0;
}