题意:词语接龙,给出n个词语,一些词语可以反转,问是否存在欧拉路径。
先判定图是否连通,再判断是否存在欧拉路径
欧拉路径的条件:满足欧拉回路或只存在两个点为奇数。
然后建图:令c=(出度-入度)/2,若出度大于入度,连边s,i容量为c,反之连i,t容量为c。
若求欧拉路径,设两个奇数的点为k1,k2,out[k1]++,in[k2]++,再连边k1,k2,容量为1.
#include <iostream> #include <queue> #include <stack> #include <map> #include <set> #include <bitset> #include <cstdio> #include <algorithm> #include <cstring> #include <climits> #include <cstdlib> #include <cmath> #include <time.h> #define maxn 55 #define maxm 4005 #define eps 1e-7 #define mod 1000000007 #define INF 0x3f3f3f3f #define PI (acos(-1.0)) #define lowbit(x) (x&(-x)) #define mp make_pair #define ls o<<1 #define rs o<<1 | 1 #define lson o<<1, L, mid #define rson o<<1 | 1, mid+1, R #define pii pair<int, int> #pragma comment(linker, "/STACK:16777216") typedef long long LL; typedef unsigned long long ULL; //typedef int LL; using namespace std; LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;} LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;} // head struct Edge { int v, c, next; Edge () {} Edge(int v, int c, int next) : v(v), c(c), next(next) {} }E[maxm]; queue<int> q; int H[maxn], cntE; int dis[maxn]; int cur[maxn]; int cnt[maxn]; int pre[maxn]; int in[maxn]; int out[maxn]; int flow, t, s, nv; void addedges(int u, int v, int c) { E[cntE] = Edge(v, c, H[u]); H[u] = cntE++; E[cntE] = Edge(u, 0, H[v]); H[v] = cntE++; } void bfs() { memset(cnt, 0, sizeof cnt); memset(dis, -1, sizeof dis); cnt[0] = 1, dis[t] = 0; q.push(t); while(!q.empty()) { int u = q.front(); q.pop(); for(int e = H[u]; ~e; e = E[e].next) { int v = E[e].v; if(dis[v] == -1) { dis[v] = dis[u] + 1; cnt[dis[v]]++; q.push(v); } } } } int isap() { memcpy(cur, H, sizeof cur); flow = 0; bfs(); int u = pre[s] = s, minv, e, f, pos; while(dis[s] < nv) { if(u == t) { f = INF; for(int i = s; i != t; i = E[cur[i]].v) if(E[cur[i]].c < f) { f = E[cur[i]].c; pos = i; } for(int i = s; i != t; i = E[cur[i]].v) { E[cur[i]].c -= f; E[cur[i] ^ 1].c += f; } flow += f; u = pos; } for(e = H[u]; ~e; e = E[e].next) if(E[e].c && dis[E[e].v] + 1 == dis[u]) break; if(~e) { cur[u] = e; pre[E[e].v] = u; u = E[e].v; } else { if(--cnt[dis[u]] == 0) break; for(e = H[u], minv = nv; ~e; e = E[e].next) if(E[e].c && minv > dis[E[e].v]) { minv = dis[E[e].v]; cur[u] = e; } dis[u] = minv + 1; cnt[dis[u]]++; u = pre[u]; } } return flow; } const int Mc = 'a'; int n, cc; int f[maxn]; bool vis[maxn]; char ss[maxn]; int find(int u) { return u == f[u] ? f[u] : find(f[u]); } bool merge(int a, int b) { int aa = find(a), bb = find(b); if(aa != bb) { f[aa] = bb; return true; } else return false; } void init() { cntE = 0, cc = 1; memset(H, -1, sizeof H); memset(in, 0, sizeof in); memset(out, 0, sizeof out); memset(vis, 0, sizeof vis); } void read() { int k, a, b; scanf("%d", &n); for(int i = 0; i <= 26; i++) f[i] = i; for(int i = 0; i < n; i++) { scanf("%s%d", ss, &k); int len = strlen(ss); int a = ss[0] - Mc; int b = ss[len-1] - Mc; cc += merge(a, b); vis[a] = true; vis[b] = true; out[a]++, in[b]++; if(k && a != b) addedges(a, b, 1); } } void work() { int t2 = 0; for(int i = 0; i < 26; i++) t2 += vis[i]; if(t2 != cc) { printf("Poor boy!\n"); return; } s = 27, t = 28, nv = cc + 2; t2 = cc = 0; int k1, k2; for(int i = 0; i < 26; i++) if(vis[i]) { if((out[i] - in[i]) % 2) { t2++; if(t2 == 1) k1 = i; else k2 = i; } } if(t2 == 2) { out[k1]++, in[k2]++; addedges(k1, k2, 1); } for(int i = 0; i < 26; i++) if(vis[i]) { int tt = (out[i] - in[i]) / 2; if(tt) { if(tt > 0) cc += tt, addedges(s, i, tt); else addedges(i, t, -tt); } } if(t2 != 0 && t2 != 2) { printf("Poor boy!\n"); return; } if(isap() != cc) printf("Poor boy!\n"); else printf("Well done!\n"); } int main() { int _, __; while(scanf("%d", &_)!=EOF) { __ = 0; while(_--) { init(); read(); printf("Case %d: ", ++__); work(); } } return 0; }