POJ2513 Colored Sticks(Trie+欧拉回路)

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题意:给你n(n<=250000)根小木棒,木棒两端染有一些颜色,两根木棒相连需要保证相连端点颜色一致.求这些木棒是否可以全部相连?题解:可以将木棒理解为边,连接两种颜色,那么全部相连就变成了一笔画的问题,即判断欧拉回路.欧拉回路需要满足两个条件:1.度数为奇数的点为0个或2个2.图联通现在的问题是如何把字符串变成数字的点,用map是肯定会TLE的hash应该可以,但tire好写,所以我选择了trie树,存入trie的值为该字符串的映射图联通用并查集去判断.代码如下:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

int cnt=0,du[500010],fa[500050];

struct Trie
{
    int sz,tr[500010][26];
    int time[500010];

    void clear()
    {
        sz=0;
        memset(tr,0,sizeof(tr));
    }

    void insert(char* c,int x)
    {
        int rt=0,len=strlen(c);
        for(int i=0;i<len;i++)
        {
            if(!tr[rt][c[i]-‘a‘])
            {
                tr[rt][c[i]-‘a‘]=++sz;
            }
            rt=tr[rt][c[i]-‘a‘];
        }
        time[rt]=x;
    }

    int search_t(char *c)
    {
        int rt=0,len=strlen(c);
        for(int i=0;i<len;i++)
        {
            if(!tr[rt][c[i]-‘a‘])
            {
                return 0;
            }
            rt=tr[rt][c[i]-‘a‘];
        }
        return time[rt];
    }

}trie;

void mem(int n)
{
    for(int i=1;i<=n;i++)
    {
        fa[i]=i;
    }
}

int find(int x)
{
    if(x!=fa[x])
    {
        fa[x]=find(fa[x]);
    }
    return fa[x];
}

void union_(int x,int y)
{
    int fx=find(x);
    int fy=find(y);
    if(fx!=fy)
    {
        fa[fx]=fy;
    }
}

int euler()
{
    int sum=0,t=find(1);
    for(int i=1;i<=cnt;i++)
    {
        if(du[i]%2==1)
        {
            sum++;
        }
    }
    if(sum!=0&&sum!=2)
    {
        return 0;
    }
    for(int i=1;i<=cnt;i++)
    {
        if(find(i)!=find(t))
        {
            return 0;
        }
    }
    return 1;
}

int main()
{
    char s1[20],s2[20];
    mem(500000);
//    freopen("1.in","r",stdin);
//    freopen("1.out","w",stdout);
    while(scanf("%s %s\n",s1,s2)!=EOF)
    {
        int from,to;
        if(!trie.search_t(s1))
        {
            trie.insert(s1,++cnt);
        }
        from=trie.search_t(s1);
        du[from]++;
        if(!trie.search_t(s2))
        {
            trie.insert(s2,++cnt);
        }
        to=trie.search_t(s2);
        du[to]++;
        union_(from,to);
    }
    if(euler())
    {
        puts("Possible");
    }
    else
    {
        puts("Impossible");
    }
}
 

原文地址:https://www.cnblogs.com/stxy-ferryman/p/8496188.html

时间: 2024-10-19 12:17:21

POJ2513 Colored Sticks(Trie+欧拉回路)的相关文章

POJ 2513 Colored Sticks(欧拉回路,字典树,并查集)

题意:给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 转:kuangbing 无向图存在欧拉路的充要条件为: ①     图是连通的: ②     所有节点的度为偶数,或者有且只有两个度为奇数的节点. 图的连通可以利用并查集去判断. 度数的统计比较容易. view code//第一次用指针写trie,本来是用二维数组,发现数组开不下,只好删删改改,改成指针 //做这道题,知道了欧拉回路判定,还有用指针写trie #include

[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(字典树+并查集+欧拉)

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

POJ2513 Colored Sticks (并查集+trie)

传送门 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K       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

poj2513 Colored Sticks (欧拉通路+Trie树+并查集)

D - Colored Sticks Crawling in process... Crawling failed Time Limit:5000MS     Memory Limit:128000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2513 Appoint description: System Crawler (2016-05-05) Description You are given a bunch

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

POJ-2513 Colored Sticks 【欧拉通路+Trie】

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

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

POJ2513 Colored Sticks

Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 36032   Accepted: 9427 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 suc