bzoj 2095 [Poi2010]Bridges 二分答案+网络流

题面

题目传送门

解法

判断混合图中是否存在欧拉回路

  • 显然,答案满足单调性,所以我们可以想到二分答案,然后问题就转化成判断混合图中是否存在欧拉回路
  • 考虑没有定向的无向边,我们不妨直接强制定向。如果存在某一个点的入度-出度为奇数,那么无论怎么改变无向边的方向,它的入度一定不会等于出度,因为修改一条边的时候一定是将两个端点的度数 ±2 ± 2
  • 然后考虑用网络流来解决这个问题
  • 对于那些强制定向的边 (x,y) ( x , y ) ,我们把这条边反向,从 y y x x 连一条容量为1的边
  • 最后对于某一个点 i i ,假设它的度数为 d[i] d [ i ] ,如果 d[i]>0 d [ i ] > 0 ,从 S S 连一条容量为 d[i]2 d [ i ] 2 的边,否则从 i i T T 连一条容量为 d[i]2 d [ i ] 2 的边。这显然是正确的,因为 d[i]2 d [ i ] 2 就是点 i i 需要调整的次数
  • 然后用 dinic d i n i c 算法跑一遍最大流,看是否满流即可

代码

#include 
#define N 1010
using namespace std;
template <typename node> void chkmax(node &x, node y) {x = max(x, y);}
template <typename node> void chkmin(node &x, node y) {x = min(x, y);}
template <typename node> void read(node &x) {
    x = 0; int f = 1; char c = getchar();
    while (!isdigit(c)) {if (c == '-') f = -1; c = getchar();}
    while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); x *= f;
}
struct Node {
    int x, y, v;
} a[N * 4];
struct Edge {
    int next, num, c;
} e[N * N];
int n, m, s, t, cnt, l[N], cur[N], sum[N], vis[N * 4];
void add(int x, int y, int c) {
    e[++cnt] = (Edge) {e[x].next, y, c};
    e[x].next = cnt;
}
void Add(int x, int y, int c) {
    add(x, y, c), add(y, x, 0);
}
bool bfs(int s) {
    for (int i = 1; i <= t; i++) l[i] = -1;
    queue <int> q; q.push(s);
    while (!q.empty()) {
        int x = q.front(); q.pop();
        for (int p = e[x].next; p; p = e[p].next) {
            int k = e[p].num, c = e[p].c;
            if (c && l[k] == -1)
                q.push(k), l[k] = l[x] + 1;
        }
    }
    return l[t] != -1;
}
int dfs(int x, int lim) {
    if (x == t) return lim;
    int used = 0;
    for (int p = cur[x]; p; p = e[p].next) {
        int k = e[p].num, c = e[p].c;
        if (c && l[k] == l[x] + 1) {
            int w = dfs(k, min(c, lim - used));
            e[p].c -= w, e[p ^ 1].c += w, used += w;
            if (e[p].c) cur[x] = p;
            if (used == lim) return lim;
        }
    }
    if (!used) l[x] = -1; return used;
}
int dinic() {
    int ret = 0;
    while (bfs(s)) {
        for (int i = 0; i <= t; i++) cur[i] = e[i].next;
        ret += dfs(s, INT_MAX);
    }
    return ret;
}
bool check(int mid) {
    memset(vis, 0, sizeof(vis));
    memset(sum, 0, sizeof(sum));
    s = 0, t = cnt = n + 1;
    if (cnt % 2 == 0) cnt++;
    for (int i = 0; i <= t; i++) e[i].next = 0;
    for (int i = 2; i <= m; i++) {
        if (a[i].v > mid) continue;
        if (i % 2 == 0) sum[a[i].x]++, sum[a[i].y]--, vis[i ^ 1] = 1;
            else if (vis[i]) Add(a[i].x, a[i].y, 1);
                else sum[a[i].x]++, sum[a[i].y]--;
    }
    int tmp = 0;
    for (int i = 1; i <= n; i++) {
        if (abs(sum[i]) % 2 == 1) return false;
        int x = abs(sum[i]) / 2;
        if (sum[i] > 0) tmp += x, Add(i, t, x);
            else Add(s, i, x);
    }
    return dinic() == tmp;
}
int main() {
    read(n), read(m);
    int tot = 1, l = INT_MAX, r = -l, ans = r;
    for (int i = 1; i <= m; i++) {
        int x, y, tx, ty;
        read(x), read(y), read(tx), read(ty);
        a[++tot] = (Node) {x, y, tx};
        a[++tot] = (Node) {y, x, ty};
        chkmin(l, min(tx, ty));
        chkmax(r, max(tx, ty));
    }
    m = tot;
    while (l <= r) {
        int mid = (l + r) >> 1;
        if (check(mid)) r = mid - 1, ans = mid;
            else l = mid + 1;
    }
    if (ans == -INT_MAX) cout << "NIE\n";
        else cout << ans << "\n";
    return 0;
}

你可能感兴趣的:(【OJ】BZOJ,【算法】网络流)