1. 程式人生 > >Colored Sticks POJ - 2513(trie樹歐拉路)

Colored Sticks POJ - 2513(trie樹歐拉路)

ans pll pac urn bitset freopen rep -- algo

題意:

  就是無向圖歐拉路

解析:

  不能用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樹歐拉路)