Poj 2513 colored sticks and query the Euler's loop judgment of the Set dictionary tree

Source: Internet
Author: User

Click to open the link.

Colored sticks
Time limit:5000 Ms   Memory limit:128000 K
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 redred violetcyan blueblue magentamagenta cyan

Sample output

Possible

The two ends of a stick can be connected in the same color.

Ask if all the sticks can be connected together.

#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;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.