poj2513

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.

这题其实也是求欧拉回路!只不过变成了字符串!所以怎么办?

办法只有一个:将字符串变数字!!

目前大一的话最好的办法就是字典树了!!我们应该想到字典树的查询时对应的数字是独一无二的吧!所以我们用字典树的好处:1,帮助存储。2,便于查询。3!!将每个字符串化为了一个独一无二的数字!!第3点是最利于我们求欧拉回路的!!!因为有了数就可以开数组!有了数组求个欧拉函数算个啥呀~~~~~~~~~~~~~~~~



在此再次向大牛们提出一个疑问:

这题我按照正常求欧拉回路的方法就是死活不a!!但是看了别人的题解,归纳是:

由图论知识可以知道,无向图存在欧拉路的充要条件为:

①     图是连通的;

②     所有节点的度为偶数,或者有且只有两个度为奇数的节点。

我按照归纳一写就过了发火,可是!!我自己写的我觉得是一个意思!!!就hh()函数里面一点点差别~~~~~~~望大牛们看到了指点12!

下面wa代码:

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int NODE = 10000000,CH = 26;
int ch[NODE][CH],sz,val[NODE];
int idx(char c)
{
        return c-'a';
}
int node()
{
        memset(ch[sz],0,sizeof(ch[sz]));
        val[sz]=0;
        return sz++;
}
void init()
{
        sz=0;
        node();
}
void insert(char *s,int v)
{
        int u=0;
        for(;*s;s++)
        {
            int c=idx(*s);
            if(!ch[u][c])
                ch[u][c]=node();
            u=ch[u][c];
        }
        val[u]=v;
}
int find(char *s)
{
        int u=0;
        for(;*s;s++)
        {
            int c=idx(*s);
            if(!ch[u][c])
                return 0;
            u=ch[u][c];
        }
        return val[u];
}

int f[1000000],rd[1000000],cd[1000000];

int n;

int ff(int x)
{
    if(x==f[x])return x;
    return ff(f[x]);
}

void hh(int n)
{
    int s=0;
    for(int i=1;i<=n;i++)
    if(f[i]==i)s++;
   // cout<<s<<endl;
    if(s>1)
        {
            printf("Impossible\n");
            return ;
        }
    else
    {
        int z=0,y=0;
        for(int i=1;i<=n;i++)
        if(rd[i]!=cd[i])
        {
        if(rd[i]-cd[i]==1)
        y++;
        else if(rd[i]-cd[i]==-1)
        z++;
        else
        {
            printf("Impossible\n");
            return ;
        }
        }
        if((y==0&&z==0)||(y==1&&z==1))
        {
            printf("Possible\n");
            return ;
        }
        else
        {
            printf("Impossible\n");
            return;
        }
    }
}


int main()
{
       memset(rd,0,sizeof(rd));
       memset(cd,0,sizeof(cd));
       for(int i=0;i<1000000;i++)
       f[i]=i;
       init();
       int i=0;
       char c1[20],c2[20];
       while(~scanf("%s%s",c1,c2))
       {

           if(find(c1)==0)
           insert(c1,++i);
           if(find(c2)==0)
           insert(c2,++i);
           int k1=find(c1),k2=find(c2);
           f[k1]=f[k2]=ff(k1);
           rd[k1]++,cd[k2]++;
       }
    hh(i);
    return 0;
}

下面是正确代码:

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int NODE = 10000000,CH = 26;
int ch[NODE][CH],sz,val[NODE];
int idx(char c)
{
        return c-'a';
}
int node()
{
        memset(ch[sz],0,sizeof(ch[sz]));
        val[sz]=0;
        return sz++;
}
void init()
{
        sz=0;
        node();
}
void insert(char *s,int v)
{
        int u=0;
        for(;*s;s++)
        {
            int c=idx(*s);
            if(!ch[u][c])
                ch[u][c]=node();
            u=ch[u][c];
        }
        val[u]=v;
}
int find(char *s)
{
        int u=0;
        for(;*s;s++)
        {
            int c=idx(*s);
            if(!ch[u][c])
                return 0;
            u=ch[u][c];
        }
        return val[u];
}

int f[1000000],d[1000000];

int n;

int ff(int x)
{
    if(x==f[x])return x;
    return ff(f[x]);
}

void hh(int n)
{
    int s=0;
    for(int i=1;i<=n;i++)
    if(f[i]==i)s++;
   // cout<<s<<endl;
    if(s>1)
    {
        printf("Impossible\n");
        return;
    }
    else
    {
        int y=0;
        for(int i=1;i<=n;i++)
        if(d[i]&1)
            y++;
        if(y==2||y==0)
        {
            printf("Possible\n");
            return ;
        }
        else
        {
            printf("Impossible\n");
            return;
        }
    }
}


int main()
{
       memset(d,0,sizeof(d));
       for(int i=0;i<1000000;i++)
       f[i]=i;
       init();
       int i=0;
       char c1[20],c2[20];
       while(~scanf("%s%s",c1,c2))
       {
           if(find(c1)==0)
           insert(c1,++i);
           if(find(c2)==0)
           insert(c2,++i);
           int k1=find(c1),k2=find(c2);
           f[k1]=f[k2]=ff(k1);
           d[k1]++,d[k2]++;
       }
    hh(i);
    return 0;
}


你可能感兴趣的:(ACM,图,大一)