码迷,mamicode.com
首页 > 其他好文 > 详细

Colored Sticks POJ - 2513(trie树欧拉路)

时间:2018-10-09 18:09:10      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:sig   欧拉路   find   lld   build   amp   vector   unsigned   strlen   

题意:

  就是无向图欧拉路

解析:

  不能用map。。超时

  在判断是否只有一个联通的时候,我比较喜欢用set,但也不能用set,会超时,反正不能用stl

emm

用trie树来编号就好了

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define pd(a) printf("%d\n", a);
#define plld(a) printf("%lld\n", a);
#define pc(a) printf("%c\n", a);
#define ps(a) printf("%s\n", a);
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 500010, INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff;
char str1[maxn], str2[maxn];
int rt, ans, tot;
int f[maxn], deg[maxn];
int trie[maxn << 1][26], vis[maxn << 1];
//map<string, int> vis;
set<int> ss;

int find(int x)
{
    return f[x] == x ? x : (f[x] = find(f[x]));
}

void init()
{
    for(int i = 0; i < maxn; i++) f[i] = i;
    tot = ans = 0;
    mem(deg, 0);
}

int build(char* s)
{
    int len = strlen(s);
    rt = 0;
    for(int i = 0; i < len; i++)
    {
        int x = s[i] - a;
        if(trie[rt][x] == 0) trie[rt][x] = ++tot;
        rt = trie[rt][x];
    }
    if(!vis[rt]) vis[rt] = ++ans;
    return vis[rt];
}

int main()
{
    init();
    int u, v;
    int cnt = 0;
    while(scanf("%s%s", str1, str2) != EOF)
    {
        u = build(str1);
        v = build(str2);
        deg[u]++;
        deg[v]++;
        int l = find(u);
        int r = find(v);
        if(l != r) f[l] = r;
    }
    int ater = find(1);
    int flag = 0, cnt1 = 0, cnt2 = 0;
    for(int i = 1; i <= ans; i++)
    {
        int x = find(i);
        if(x != ater)
        {
            printf("Impossible\n");
            return 0;
        }
        if(deg[i] & 1)
            cnt1++;
    }
    if(cnt1 == 0 || cnt1 == 2)
        printf("Possible\n");
    else
        printf("Impossible\n");


    return 0;
}

 

Colored Sticks POJ - 2513(trie树欧拉路)

标签:sig   欧拉路   find   lld   build   amp   vector   unsigned   strlen   

原文地址:https://www.cnblogs.com/WTSRUVF/p/9761524.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!