POJ 2513 Colored Sticks——字典树+并查集+欧拉回路

比较综合的一道题,套用字典树以及并查集模板。

注意:

1.字典树应该有提供单词编号的功能,这也是解题必须的要素

2.输入为空返回0,这个跟写法有关,我的写法不用特殊判断

3注意各种细节,本题综合性较强,代码量较大,稍不留神就会出现细节错误,注意及时调试

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#define ID(x) x - 'a'

using namespace std;

const int maxn = 1e6;

int next;

struct Trie {
    int next[26];
    int val;
}tree[maxn];

int add() {
    memset(&tree[next], 0, sizeof(Trie));
    return next++;
}

void Insert(char *s) {
    int cur = 0, len = strlen(s);
    for (int i = 0; i < len; i++) {
        int id = ID(s[i]);
        if (!tree[cur].next[id]) {
            tree[cur].next[id] = add();
        }
        cur = tree[cur].next[id];
    }
    tree[cur].val++;
}

int Find(char *s) {
    int rt = 0, len = strlen(s);
    for (int i = 0; i < len; i++) {
        int id = ID(s[i]);
        if (!tree[rt].next[id]) return 0;
        rt = tree[rt].next[id];
    }
    if (tree[rt].val) return rt;
    return 0;
}

int par[maxn], ran[maxn];

void init() {
    for (int i = 0; i < maxn; i++) {
        par[i] = i, ran[i] = 0;
    }
}

int seek(int x) {
    if (par[x] == x) return x;
    else return par[x] = seek(par[x]);
}

void unite(int x, int y) {
    x = seek(x), y = seek(y);
    if (x == y) return;
    if (ran[x] < ran[y]) par[x] = y;
    else {
        par[y] = x;
        if (ran[x] == ran[y]) ran[x]++;
    }
}

int du[maxn] = {0};

int main()
{
    next = 1;
    memset(&tree[0], 0, sizeof(Trie));
    init();

    char s1[20], s2[20];
    while (scanf("%s %s", s1, s2) == 2) {
        Insert(s1), Insert(s2);
        int x = Find(s1), y = Find(s2);
        unite(x, y);
        du[x]++, du[y]++;
    }
    int temp = seek(Find(s1));
    bool ok = true;
    for (int i = 1; i < next; i++) {
        int s = seek(i);
        if (s != i && s != temp) { ok = false; break; }
    }

    if (ok) {
        int cnt = 0;
        for (int i = 1; i < next; i++) {
            if (du[i] % 2) cnt++;
            if (cnt >= 3) break;
        }
        if (cnt >= 3 || cnt == 1) printf("Impossible\n");
        else printf("Possible\n");
    }
    else printf("Impossible\n");

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值