bzoj 1797 [Ahoi2009]Mincut 最小割 网络流+tarjan

题面

题目传送门

解法

某条边是否属于最小割,可以通过有向图连通性来解决

  • 首先,我们可以先通过 d i n i c 算法求出这张图的最小割,即最大流
  • 然后对于每一条原来的边 ( x , y ) 分别考虑
  • 先判定 ( x , y ) 是否可以在最小割上
  • 显然,如果 ( x , y ) 这条边仍然有流量残余,那么它显然不会在最小割上,因为如果这条边可能在最小割上,那么这一定不会是最优解
  • 然后我们在残余网络上跑tarjan算法,只经过那些还有流量的边
  • 如果 x , y 在同一个强连通分量中,那么这条边一定不会出现在最小割中,因为考虑缩点后得到的新图一定只有满流的边,任意一个割一定是原图中对应的最小割,如果 x , y 在同一个强联通分量中,那么割去这条边一定不会影响新图的连通性,然而这个显然是矛盾的
  • 那么,根据上面两点,我们就可以判断这条边是否可能在最小割中
  • 然后考虑这条边是否一定在最小割中,当且仅当 x S 在同一个强连通分量中, y T 在同一个强联通分量中
  • 因为如果将 ( x , y ) 的容量增加,那么残余网络中就会出现 S x y T 的一条增广路,从而使最小割变大
  • 那么,这道题就做完了,只要在 d i n i c 求完最大流之后跑tarjan算法即可

代码

#include <bits/stdc++.h>
#define N 100010
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 Edge {
    int next, num, c, id;
} e[N * 3];
int n, m, s, t, cnt, top, tot, Time;
int l[N], cur[N], dfn[N], low[N], col[N], st[N], in[N], ans[N][2];
void add(int x, int y, int c, int id) {
    e[++cnt] = (Edge) {e[x].next, y, c, id};
    e[x].next = cnt;
}
void Add(int x, int y, int c, int id) {
    add(x, y, c, id), add(y, x, 0, 0);
}
bool bfs(int s) {
    for (int i = 1; i <= n; i++) l[i] = -1;
    l[s] = 0; 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)
                l[k] = l[x] + 1, q.push(k);
        }
    }
    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 (l[k] == l[x] + 1 && c) {
            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 = 1; i <= n; i++) cur[i] = e[i].next;
        ret += dfs(s, INT_MAX);
    }
    return ret;
}
void tarjan(int x) {
    dfn[x] = low[x] = ++Time;
    st[++top] = x, in[x] = 1;
    for (int p = e[x].next; p; p = e[p].next) {
        int k = e[p].num, c = e[p].c;
        if (!c) continue;
        if (!dfn[k]) tarjan(k), chkmin(low[x], low[k]);
            else if (in[k]) chkmin(low[x], dfn[k]);
    }
    if (low[x] == dfn[x]) {
        col[x] = ++tot;
        while (st[top] != x)
            col[st[top]] = tot, in[st[top--]] = 0;
        in[st[top--]] = 0;
    }
}
int main() {
    read(n), read(m), read(s), read(t);
    cnt = n + 1 + (n % 2 == 1);
    for (int i = 1; i <= m; i++) {
        int x, y, v; read(x), read(y), read(v);
        Add(x, y, v, i);
    }
    int tmp = dinic();
    for (int i = 1; i <= n; i++)
        if (!dfn[i]) Time = top = 0, tarjan(i);
    for (int i = 1; i <= n; i++)
        for (int p = e[i].next; p; p = e[p].next) {
            if (!e[p].id) continue;
            int id = e[p].id, k = e[p].num;
            if (e[p].c || col[i] == col[k]) continue;
            ans[id][0] = 1;
            if (col[s] == col[i] && col[k] == col[t]) ans[id][1] = 1;
        }
    for (int i = 1; i <= m; i++) cout << ans[i][0] << ' ' << ans[i][1] << "\n";
    return 0;
}

猜你喜欢

转载自blog.csdn.net/emmmmmmmmm/article/details/82193615