Poj 2513 colored sticks (Euler's path + query set + dictionary tree)

Source: Internet
Author: User

Link: poj 2513 colored sticks

There are n wooden sticks, each of which is painted with a color. Now, the color of each wooden stick is given.

Finally, I asked if I could splice n wooden sticks together.

Solution: Euler's path + query set + dictionary tree. Euler's path. The statistical degree of each node. The number of vertices with an odd degree cannot exceed 2. Check the set and determine the node

Whether it is fully connected. Dictionary tree, ing color.

#include <cstdio>#include <cstring>#include <string>#include <vector>#include <iostream>#include <algorithm>using namespace std;const int maxn = 5000005;const int sigma_size = 26;typedef pair<int,int> pii;int N, E, far[maxn], cnt[maxn];pii ed[maxn];struct Tire {    int sz, g[maxn][sigma_size];    int val[maxn];    void init();    int idx(char ch);    int insert(char* s);}T;inline int getfar(int x) {    return x == far[x] ? x : far[x] = getfar(far[x]);}bool judge() {    int ret = 0;    for (int i = 1; i <= N; i++) {        if (cnt[i]&1)            ret++;    }    if (ret > 2)        return false;    for (int i = 0; i < E; i++) {        int p = getfar(ed[i].first);        int q = getfar(ed[i].second);        if (p != q) {            N--;            far[p] = q;        }    }    return N <= 1;}int main () {    T.init();    N = E = 0;    char a[11], b[11];    while (scanf("%s%s", a, b) == 2) {        int p = T.insert(a);        int q = T.insert(b);        cnt[p]++, cnt[q]++;        ed[E].first = p;        ed[E].second = q;        E++;    }    printf("%s\n", judge() ? "Possible" : "Impossible");    return 0;}void Tire::init() {    sz = 1;    val[0] = 0;    memset(g[0], 0, sizeof(g[0]));}int Tire::idx(char ch) {    return ch - ‘a‘;}int Tire::insert(char* s) {    int u = 0, n = strlen(s);    for (int i = 0; i < n; i++) {        int v = idx(s[i]);        if (g[u][v] == 0) {            g[u][v] = sz;            memset(g[sz], 0, sizeof(g[sz]));            val[sz++] = 0;        }        u = g[u][v];    }    if (val[u] == 0) {        val[u] = ++N;        far[N] = N;        cnt[N] = 0;    }    return val[u];}

Poj 2513 colored sticks (Euler's path + query set + dictionary tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.