码迷,mamicode.com
首页 > 其他好文 > 详细

[欧拉回路] poj 2513 Colored Sticks

时间:2014-10-03 13:05:24      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   io   os   ar   for   strong   

题目链接:

http://poj.org/problem?id=2513

Colored Sticks
Time Limit: 5000MS   Memory Limit: 128000K
Total Submissions: 30955   Accepted: 8159

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.

Source

[Submit]   [Go Back]   [Status]   [Discuss]

题目意思:

有n条木棒,每条木棒两端有两种颜色,求这些木棒能否能在一起,使得前一个木棒的后端颜色和前一个木棒的前端颜色一样。

解题思路:

欧拉通路+字典树+并查集

注意是无向边,注意要判断连通性,直接用map映射会超时,自己写字典树。

代码:

//#include<CSpreadSheet.h>

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

#define Maxn 550000

int dei[Maxn],a[Maxn],cnt,la;

struct Node
{
    struct Node * next[30];
    int nu;
}node[Maxn*15],root;


int Insert(char *a)
{
    Node * p=&root;

    while(*a)
    {
        if(p->next[*a-'a'+1]==NULL)
        {
            node[++la].nu=0;
            memset(node[la].next,NULL,sizeof(node[la].next));
            p->next[*a-'a'+1]=&node[la];
        }
        p=p->next[*a-'a'+1];
        a++;
    }
    if(!p->nu)
        p->nu=++cnt;
    return p->nu;

}

int Find(int x)
{
    int temp=x;
    while(x!=fa[x])
        x=fa[x];
    while(fa[temp]!=x)
    {
        int cur=fa[temp];
        fa[temp]=x;
        temp=cur;
    }
    return x;
}
void Unio(int a,int b)
{
    a=Find(a),b=Find(b);
    if(a!=b)
        fa[a]=b;
}
int main()
{

   memset(dei,0,sizeof(dei));
   string a,b;
   char sa[15],sb[15];
   cnt=la=0;
   for(int i=1;i<=Maxn-5000;i++)
        fa[i]=i;

   memset(root.next,NULL,sizeof(root.next));
   root.nu=0;

   while(~scanf("%s%s",sa+1,sb+1))
   {
       int a=Insert(sa+1);
       int b=Insert(sb+1);

       dei[a]++,dei[b]++;
       Unio(a,b);
   }
   bool ans=true;
   int nui=0,nuo=0,la=-1;

   for(int i=1;i<=cnt;i++)
   {
       if(la==-1)
            la=Find(i);
       else if(la!=Find(i))
       {
           ans=false;
           break;
       }
       if(dei[i]&1)
            nui++;
   }
   if(!ans||nui>2||nui==1)
        printf("Impossible\n");
   else
        printf("Possible\n");
   //system("pause");

    return 0;
}



[欧拉回路] poj 2513 Colored Sticks

标签:des   style   http   color   io   os   ar   for   strong   

原文地址:http://blog.csdn.net/cc_again/article/details/39754209

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!