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

解题报告

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(字典树+判断欧拉通路)

时间: 2024-10-11 01:43:23

POJ训练计划_Colored Sticks(字典树+判断欧拉通路)的相关文章

[ACM] POJ 2513 Colored Sticks (Trie树,欧拉通路,并查集)

Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 29736   Accepted: 7843 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 st

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

题目地址:POJ 2513 刚开始没想到字典树,用的map函数一直TLE,由于上一次的签到题由于没想到字典树而卡了好长时间的深刻教训,于是过了不久就想起来用字典树了,(为什么是在TLE了5次之后..T^T)然后把map改成了字典树,然后就过了. 这题居然不知不觉的用上了欧拉回路..其实当时我是这样想的..因为相互接触的必须要相同,所以除了两端外,其他的都是两两相同的,所以除了两端的颜色外其他的的个数必须为偶数.然后两端的可能相同可能不相同,相同的话,说明所有的都是偶数个数了,不相同的话那就只有这

poj 2513 Colored Sticks(欧拉通路+并查集+字典树)

题目链接:poj 2513 Colored Sticks 题目大意:有N个木棍,每根木棍两端被涂上颜色,现在给定每个木棍两端的颜色,不同木棍之间拼接需要颜色相同的 端才可以,问最后能否将N个木棍拼接在一起. 解题思路:欧拉通路+并查集+字典树.欧拉通路,每个节点的统计度,度为奇数的点不能超过2个.并查集,判断节点 是否完全联通.字典树,映射颜色. #include <cstdio> #include <cstring> #include <string> #includ

POJ 2513 Colored Sticks (Trie树+并查集+欧拉路)

Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 31490   Accepted: 8320 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 st

poj2513Colored 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

POJ2513Colored Sticks(欧拉通路)(字典树)(并查集)

Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 35612   Accepted: 9324 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 st

POJ 2513 无向欧拉通路+字典树+并查集

题目大意: 有一堆头尾均有颜色的木条,要让它们拼接在一起,拼接处颜色要保证相同,问是否能够实现 这道题我一开始利用map<string,int>来对颜色进行赋值,好进行后面的并查操作以及欧拉通路的判断,但是map效率太低,超时了 网上看了一遍发现必须得用效率更高的字典树对每个不同的颜色进行赋值 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace st

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

题目链接:Colored Sticks 一道3个知识点结合的题目,可以说单个知识点的题目,都会做,一旦知识点结合起来,题目就不简单了 思路:这个题开始看就知道是并查集,但是不好处理的不同种单词的统计,所以理所应当联想到字典树,上次做字典树的题目啸爷出的是统计相同单词数,这个题目和那个一样,把flag加个编号即可,再利用并查集. 1750ms  水过 #include <iostream> #include <cstdio> #include <cstdlib> #inc

POJ2513:Colored Sticks(字典树+欧拉路径+并查集)

http://poj.org/problem?id=2513 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 t