BZOJ2095 [Poi2010]Bridges

时间:2022-08-28 19:58:46

首先二分答案。。。然后这张图变成了有一些有向边,有一些无向边

然后就是混合图欧拉回路的判断

我们知道如果是有向图,它存在欧拉回路的等价条件是所有点的出度等于入度

对于混合图。。。先不管有向边,把无向边随意定向

首先要满足条件就是当前图的点的度数都是偶数,因为把一条边反向端点的出度入度之差改变了2,奇偶性不变

我们只要判断是否把部分已经定向的无向边反向以后可以满足度都是偶数这个条件

用网络流来判断

对于每条边,如果定向为$x$到$y$,则$y$向$x$连边,流量为1

对于每个点$x$,如果出度 - 入度大于0,源点向$x$连边,否则$x$向汇点连边,流量为度数差除以2

如果满流则说明可以

 /**************************************************************
Problem: 2095
User: rausen
Language: C++
Result: Accepted
Time:136 ms
Memory:952 kb
****************************************************************/ #include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
const int N = 1e3 + ;
const int M = 2e3 + ;
const int inf = 1e9; inline int read(); struct Edge {
int x, y;
int v1, v2; inline void get() {
x = read(), y = read(), v1 = read(), v2 = read();
if (v1 > v2) swap(x, y), swap(v1, v2);
}
} E[M]; struct edge {
int next, to, f;
edge() {}
edge(int _n, int _t, int _f) : next(_n), to(_t), f(_f) {}
} e[M << ]; int n, m, S, T;
int first[N], tot;
int deg[N], tot_deg;
int d[N]; inline void Add_Edges(int x, int y, int f) {
e[++tot] = edge(first[x], y, f), first[x] = tot;
e[++tot] = edge(first[y], x, ), first[y] = tot;
} #define y e[x].to
#define p q[l]
bool bfs() {
static int l, r, x, q[N];
memset(d, -, sizeof(d));
d[q[] = S] = ;
for (l = r = ; l != r + ; ++l)
for (x = first[p]; x; x = e[x].next)
if (!~d[y] && e[x].f) {
d[q[++r] = y] = d[p] + ;
if (y == T) return ;
}
return ;
}
#undef p int dfs(int p, int lim) {
if (p == T || !lim) return lim;
int x, tmp, rest = lim;
for (x = first[p]; x && rest; x = e[x].next)
if (d[y] == d[p] + && ((tmp = min(e[x].f, rest)) > )) {
rest -= (tmp = dfs(y, tmp));
e[x].f -= tmp, e[x ^ ].f += tmp;
if (!rest) return lim;
}
if (rest) d[p] = -;
return lim - rest;
}
#undef y int Dinic() {
static int res, i;
for (res = , i = ; i <= n; ++i)
if (deg[i] & ) return -;
while (bfs())
res += dfs(S, inf);
return res;
} void rebuild_graph(int t) {
static int i;
tot = , tot_deg = ;
for (i = ; i <= n + ; ++i)
deg[i] = first[i] = ;
for (i = ; i <= m; ++i) {
if (E[i].v1 <= t) --deg[E[i].x], ++deg[E[i].y];
if (E[i].v2 <= t) Add_Edges(E[i].y, E[i].x, );
}
for (i = ; i <= n; ++i)
if (deg[i] > ) tot_deg += deg[i] >> , Add_Edges(S, i, deg[i] >> );
else Add_Edges(i, T, (-deg[i]) >> );
} int main() {
int i, l = inf, r = , tmp;
n = read(), m = read(), S = n + , T = S + ;
for (i = ; i <= m; ++i) {
E[i].get();
l = min(l, E[i].v1), r = max(r, E[i].v2);
}
l -= , tmp = (r += );
#define mid (l + r >> 1)
while (l + < r) {
rebuild_graph(mid);
if (Dinic() == tot_deg) r = mid;
else l = mid;
}
#undef mid
if (tmp == r) puts("NIE");
else printf("%d\n", r);
return ;
} inline int read() {
static int x;
static char ch;
x = , ch = getchar();
while (ch < '' || '' < ch)
ch = getchar();
while ('' <= ch && ch <= '') {
x = x * + ch - '';
ch = getchar();
}
return x;
}