【BZOJ1797】【AHOI2009】最小割(网络流,SCC)

Description

给定一个网络流的图,求哪些边可能在最小割中,哪些一定在最小割中。


Solution

Conclusion:
可能在最小割中:满流且两点在残余网络中不属于同一个SCC;
一定在最小割中:一个点在 S 点所在的SCC,另一个点在 T 所属的SCC。


Code

/************************************************
 * Au: Hany01
 * Date: Aug 14th, 2018
 * Prob: BZOJ1797 AHOI2009 Mincut
 * Email: [email protected]
 * Inst: Yali High School
************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read() {
    static int _, __; static char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const int maxn = 4e3 + 5, maxm = 12e4 + 5;

int S, T, beg[maxn], u[maxm], v[maxm], nex[maxm], f[maxm], e, clk, dfn[maxn], low[maxn], cnt, co[maxn], lev[maxn], n, m, stk[maxn], top, isin[maxn];

inline void add(int uu, int vv, int ff, int ty = 1) {
    u[++ e] = uu, v[e] = vv, f[e] = ff, nex[e] = beg[uu], beg[uu] = e;
    if (ty) add(vv, uu, 0, 0);
}

inline int BFS() {
    static queue<int> q; q.push(S), Set(lev, 0), lev[S] = 1;
    while (!q.empty()) {
        register int u = q.front(); q.pop();
        for (register int i = beg[u]; i; i = nex[i])
            if (f[i] && !lev[v[i]]) lev[v[i]] = lev[u] + 1, q.push(v[i]);
    }
    return lev[T];
}

int DFS(int u, int flow) {
    if (u == T) return flow;
    int t, res = flow;
    for (register int i = beg[u]; i; i = nex[i])
        if (f[i] && lev[v[i]] == lev[u] + 1) {
            f[i] -= (t = DFS(v[i], min(f[i], res))), f[i ^ 1] += t;
            if (!(res -= t)) return flow;
        }
    return flow - res;
}

inline void Tarjan(int u) {
    dfn[u] = low[u] = ++ clk, stk[++ top] = u, isin[u] = 1;
    for (register int i = beg[u]; i; i = nex[i]) if (f[i]) {
        if (!dfn[v[i]]) Tarjan(v[i]), chkmin(low[u], low[v[i]]);
        else if (isin[v[i]]) chkmin(low[u], dfn[v[i]]);
    }
    if (dfn[u] == low[u]) {
        ++ cnt; do co[stk[top]] = cnt, isin[stk[top]] = 0; while (stk[top --] != u);
    }
}

int main()
{
#ifdef hany01
    freopen("bzoj1797.in", "r", stdin);
    freopen("bzoj1797.out", "w", stdout);
#endif

    static int uu, vv, ff;

    n = read(), m = read(), S = read(), T = read(), e = 1;
    while (m --) uu = read(), vv = read(), ff = read(), add(uu, vv, ff);
    while (BFS()) DFS(S, INF);

    For(i, 1, n) if (!dfn[i]) Tarjan(i);
    for (register int i = 2; i < e; i += 2) {
        putchar((co[v[i]] != co[v[i ^ 1]] && !f[i]) ^ 48), putchar(' ');
        putchar(((co[v[i ^ 1]] == co[S] && co[v[i]] == co[T] || co[v[i ^ 1]] == co[T] && co[v[i]] == co[S]) && !f[i]) ^ 48);
        putchar('\n');
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/hhaannyyii/article/details/81711766