HS BDC HDU - 3472(混合欧拉路径)

时间:2023-12-28 14:42:08

题意:

  就是混合欧拉路径板题

解析:

  欧拉路径加一条t_ ---> s_  的边就变成了欧拉回路,所以利用这一点,如果存在两个奇点,那么这两个奇点出度大的是s_,入度大的是t_,加一条t_ ---> s_的容量为1的边即可

然后混合欧拉回路板题

#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 = , INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff;
int n, m, s, t, cnt;
int f[maxn], deg[maxn], in[maxn], out[maxn], vis[maxn];
int d[maxn], head[maxn], cur[maxn];
set<int> ss; int find(int x)
{
return f[x] == x ? x : (f[x] = find(f[x]));
} void init()
{
for(int i = ; i < maxn; i++) f[i] = i;
mem(in, );
mem(head, -);
mem(out, );
cnt = ;
mem(vis, );
ss.clear();
} struct edge
{
int u, v, c, next;
}Edge[maxn]; void add_(int u, int v, int c)
{
Edge[cnt].u = u;
Edge[cnt].v = v;
Edge[cnt].c = c;
Edge[cnt].next = head[u];
head[u] = cnt++;
} void add(int u, int v, int c)
{
add_(u, v, c);
add_(v, u, );
} bool bfs()
{
queue<int> Q;
mem(d, );
Q.push(s);
d[s] = ;
while(!Q.empty())
{
int u = Q.front(); Q.pop();
for(int i = head[u]; i != -; i = Edge[i].next)
{
edge e = Edge[i];
if(!d[e.v] && e.c > )
{
d[e.v] = d[e.u] + ;
Q.push(e.v);
if(e.v == t) return ;
}
}
}
return d[t] != ;
} int dfs(int u, int cap)
{
int ret = ;
if(u == t || cap == )
return cap;
for(int &i = cur[u]; i != -; i = Edge[i].next)
{
edge e = Edge[i];
if(d[e.v] == d[u] + && e.c > )
{
int V = dfs(e.v, min(cap, e.c));
Edge[i].c -= V;
Edge[i^].c += V;
ret += V;
cap -= V;
if(cap == ) break;
}
}
if(cap > ) d[u] = -;
return ret;
} int Dinic(int u)
{
int ans = ;
while(bfs())
{
memcpy(cur, head, sizeof(head));
ans += dfs(u, INF);
}
return ans;
} int main()
{
int T, kase = ;
cin >> T;
while(T--)
{
string str;
int u, v, w;
cin >> n;
init();
s = , t = ;
for(int i = ; i <= n; i++)
{
cin >> str >> w;
u = str[] - 'a' + , v = str[str.size() - ] - 'a' + ;
vis[u] = vis[v] = ;
in[v]++, out[u]++;
if(u != v && w == ) add(u, v, );
int l = find(u);
int r = find(v);
if(l != r) f[l] = r;
}
int flag = , m_sum = , s_ = INF, t_ = INF;
for(int i = ; i <= ; i++)
{
int x = find(i);
if(vis[x]) ss.insert(x);
if(abs(out[i] - in[i]) & )
{
if(out[i] > in[i]) s_ = i;
else t_ = i;
if(++flag > ) break;
}
if(out[i] > in[i]) add(s, i, (out[i] - in[i]) / ), m_sum += (out[i] - in[i]) / ;
else if(in[i] > out[i]) add(i, t, (in[i] - out[i]) / ); }
if(s_ != INF && t_ != INF) add(t_, s_, );
// cout << flag << " " << ss.size() << endl;
printf("Case %d: ", ++kase);
if(flag > || ss.size() > || flag == )
{
cout << "Poor boy!" << endl;
continue;
}
if(m_sum == Dinic(s))
cout << "Well done!" << endl;
else
cout << "Poor boy!" << endl; } return ;
}