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

来源:互联网 发布:js中select标签 编辑:程序博客网 时间:2024/05/17 23:37
Colored Sticks
Time Limit: 5000MS Memory Limit: 128000KTotal 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 redred violetcyan blueblue magentamagenta 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 500010using 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;}


                                             
0 0
原创粉丝点击