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

POJ训练计划_Colored Sticks(字典树+判断欧拉通路)

时间:2014-07-29 14:36:19      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   strong   io   

解题报告

http://blog.csdn.net/juncoder/article/details/38236333

题目传送门

题意:

问给定一堆的棒,两端有颜色,相同的颜色的棒可以头尾相接,能否连在一条直线。

思路:

把每一根棒两端看成两个点,之间连着线,判断这样的一个图中是否有欧拉通路

欧拉通路:

在联通无向图中,经过G的每一条边一次并且仅有一次的路径为欧拉通路。

求欧拉通路的充分条件:图为联通图,并且仅有两个奇度数的结点或无奇度结点。

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
struct node {
    int v;
    node *next[30];
};
int d[251000*2],cnt,B[251000*2];
node *newnode()
{
    node *p=new node;
    p->v=0;
    for(int i=0; i<26; i++) {
        p->next[i]=NULL;
    }
    return p;
}
void addnode(node *root,char *str,int nn)
{
    node *p=root;
    int l=strlen(str),i;
    for(i=0; i<l; i++) {
        int t=str[i]-'a';
        if(p->next[t]==NULL)
            p->next[t]=newnode();
        p=p->next[t];
    }
    p->v=nn;
}
int findd(node *root,char *str)
{
    node *p=root;
    int l=strlen(str),i;
    for(i=0; i<l; i++) {
        int t=str[i]-'a';
        if(p->next[t]==NULL)
            return 0;
        p=p->next[t];
    }
    return p->v;
}
int bfind(int x)
{
    if(B[x]!=x)
        B[x]=bfind(B[x]);
    return B[x];
}
void bin(int x,int y)
{
    int xx=bfind(x);
    int yy=bfind(y);
    if(xx!=yy) {
        B[xx]=yy;
    }
}
int main()
{
    int a,b,i,j;
    node *root=newnode();
    char str1[100],str2[100];
    cnt=1;
    for(i=0; i<=250000*2; i++)
        B[i]=i;
    while(~scanf("%s%s",str1,str2)) {
        a=findd(root,str1);
        b=findd(root,str2);
        if(!a) {
            addnode(root,str1,cnt);
            a=cnt++;
        }
        d[a]++;
        if(!b) {
            addnode(root,str2,cnt);
            b=cnt++;
        }
        d[b]++;
    }
    int sum=0,sum2=0;
    for(i=1; i<cnt; i++) {
        if(d[i]%2!=0)
            sum2++;
        if(bfind(i)==cnt)
            sum++;
    }
    if(sum>1) {
        printf("Impossible\n");
    } else {
        if(sum2==2||sum2==0)
            printf("Possible\n");
        else
            printf("Impossible\n");
    }
}

Colored Sticks
Time Limit: 5000MS   Memory Limit: 128000K
Total Submissions: 29962   Accepted: 7909

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.


POJ训练计划_Colored Sticks(字典树+判断欧拉通路),布布扣,bubuko.com

POJ训练计划_Colored Sticks(字典树+判断欧拉通路)

标签:des   style   blog   http   color   os   strong   io   

原文地址:http://blog.csdn.net/juncoder/article/details/38236333

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