POJ 2513 Colored Sticks【欧拉通路】

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

 

并查集①

code:

View Code
#include<stdio.h>
#include<string.h>
#define maxn 500005
int de[maxn];
int f[maxn];
int v[maxn];
int find(int x)
{
int r=x;
while(r!=f[r])
r=f[r];
int i=x;
int j;
while(i!=r)
{
j=f[i];
f[i]=r;
i=j;
}
return r;
}
void join(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
{
if(fx<fy)
f[fy]=fx;
else f[fx]=fy;
}
}
int Hash(char s[])
{
int i,hash=1;
int len=strlen(s);
for(i=0;i<len;i++)
hash=(hash*29+s[i]-'a')%maxn;
return hash;
}
int main()
{
char a[11],b[11];
int i,p,q,tot;
memset(de,0,sizeof(de));
memset(v,0,sizeof(v));
for(i=0;i<maxn;i++)
f[i]=i;
while(scanf("%s%s",a,b)!=EOF)
{
p=Hash(a);
q=Hash(b);
de[p]++;
de[q]++;
v[p]=1;
v[q]=1;
join(p,q);
}
tot=0;
for(i=1;i<maxn;i++)
{
if(de[i]&1)
tot++;
if(tot>2)
{
printf("Impossible\n");
return 0;
}
}
tot=0;
for(i=1;i<maxn;i++)
{
if(v[i]&&find(i)==i)
tot++;
if(tot>1)
{
printf("Impossible\n");
return 0;
}
}
printf("Possible\n");
return 0;
}

并查集②:

 

View Code
#include<stdio.h>
#include<string.h>
#define maxn 500005
int de[maxn];
int f[maxn];
int find(int x)
{
int r=x;
while(r!=f[r])
r=f[r];
int i=x;
int j;
while(i!=r)
{
j=f[i];
f[i]=r;
i=j;
}
return r;
}
void join(int x,int y)
{
if(find(x)==0)
f[x]=x;
if(find(y)==0)
f[y]=y;
f[find(x)]=find(y);
}
int Hash(char s[])
{
int i,hash=1;
int len=strlen(s);
for(i=0;i<len;i++)
hash=(hash*29+s[i]-'a')%maxn;
return hash;
}
int main()
{
char a[11],b[11];
int i,p,q,tot,flag,fx;
memset(de,0,sizeof(de));
memset(f,0,sizeof(f));
while(scanf("%s%s",a,b)!=EOF)
{
p=Hash(a);
q=Hash(b);
de[p]++;
de[q]++;
join(p,q);
}
tot=0;
for(i=1;i<maxn;i++)
{
if(de[i]%2)
tot++;
if(tot>2)
{
printf("Impossible\n");
return 0;
}
}
i=1;
for(i=1;i<maxn;i++)
{
if(find(i))
break;
}
flag=find(i);
for(;i<maxn;i++)
{
fx=find(i);
if(fx&&fx!=flag)
{
printf("Impossible\n");
return 0;
}
}
printf("Possible\n");
return 0;
}

 

字典树:

 

View Code
#include<stdio.h>
#include<string.h>
#define maxn 500005
int de[maxn];
int f[maxn];
int num;
struct node
{
int id,i;
node *next[26];
node()
{
id=-1;
for(i=0;i<26;i++)
next[i]=NULL;
}
}*root;
int find(int x)
{
int r=x;
while(r!=f[r])
r=f[r];
int i=x;
int j;
while(i!=r)
{
j=f[i];
f[i]=r;
i=j;
}
return r;
}
void join(int x,int y)
{
if(find(x)==0)
f[x]=x;
if(find(y)==0)
f[y]=y;
f[find(x)]=find(y);
}
int insert(char s[])
{
int i;
node *p=root;
int len=strlen(s);
for(i=0;i<len;i++)
{
if(p->next[s[i]-'a']==NULL)
p->next[s[i]-'a']=new node;
p=p->next[s[i]-'a'];
}
if(p->id==-1)
p->id=num++;
return p->id;
}
int main()
{
num=0;
root=new node;
char a[11],b[11];
int p,q,flag;
int i,sum;
for(i=0;i<maxn;i++)
{
de[i]=0;
f[i]=i;
}
while(scanf("%s%s",a,b)!=EOF)
{
p=insert(a);
q=insert(b);
de[p]++;
de[q]++;
join(p,q);
}
sum=0;
flag=find(0);
for(i=0;i<num;i++)
{
if(de[i]&1)
sum++;
if(sum>2||flag!=find(i))
{
printf("Impossible\n");
return 0;
}
}
printf("Possible\n");
return 0;
}



posted @ 2012-03-16 00:37  'wind  阅读(206)  评论(0编辑  收藏  举报