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

原题链接:http://poj.org/problem?id=2513

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 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

The UofA Local 2000.10.14

解题思路

使用字典树存储各个颜色的英文,并用数字一一对应。木棍两边的颜色用并查集维护,最后如果发现有两个及以上连通分量,那一定是不符合题意的。本题接下来转化成欧拉图问题,图中所有的节点中,度数为奇数的节点只能有0个或2个。

AC代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<string>
#include<queue>
#include<sstream>
#include<list>
#include<stack>
#define ll long long
#define ull unsigned long long
#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)
#define rrep(i,a,b) for(int i=(a),_ed=(b);i>=_ed;i--)
#define fil(a,b) memset((a),(b),sizeof(a))
#define cl(a) fil(a,0)
#define PI 3.1415927
#define inf 0x3f3f3f3f
#define MAXN 500005
#define MAX 26
template<typename N>N gcd(N a, N b) { return b ? gcd(b, a%b) : a; }
using namespace std;
 typedef struct TrieNode   //字典树节点
{
    bool isword;          //当前节点是否为颜色单词的结尾
    TrieNode * next[MAX];
    int id;
}Trie;

int f[MAXN];
int degree[MAXN];
int color;

int find(int a)
{
    if (f[a] == -1) return a;
    else return f[a] = find(f[a]);
}
void uni(int a, int b)
{
    int x, y;
    x = find(a);
    y = find(b);
    if (x != y) f[b] = a;
    return;
}
int insert(Trie * root,char *word)    //插入单词
{
    Trie * p = root;
    int i = 0;
    while(word[i]!='\0')
    {
        if (p->next[word[i] - 'a'] == NULL)
        {
            Trie *temp = new Trie;
            temp->id = 0;
            temp->isword = false;
            rep(j, 0, MAX - 1) temp->next[j] = NULL;

            p->next[word[i] - 'a'] = temp;
        }
        p = p->next[word[i] - 'a'];
        i++;
    }
    if (!p->isword)
    {
        p->isword = true;
        p->id = color++;
    }
    return p->id;

}

int main()
{
    char str1[15];
    char str2[15];
    int color1, color2;
    int link = 0, oddDegree = 0;
    Trie *root = new Trie;
    fil(f, -1);
    cl(degree);
    root->id = 0;
    root->isword = false;
    rep(i, 0, MAX - 1) root->next[i] = NULL;
    while (scanf("%s%s", str1, str2) != EOF)
    {
        color1 = insert(root, str1);
        color2 = insert(root, str2);
        degree[color1]++;
        degree[color2]++;
        uni(color1, color2);
    }
    rep(i, 0, color - 1)
    {
        if (f[i] == -1) link++;
        if (degree[i] & 1) oddDegree++;
        if (link > 1 || oddDegree > 2) break;
    }
    if ((link == 0 || link == 1) && (oddDegree == 0 || oddDegree == 2)) printf("Possible\n");
    else printf("Impossible\n");

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值