P4123 [CQOI2016] 不同的最小割

发布时间 2023-12-11 15:21:56作者: cxqghzj

题意

\(n\) 个点两两求最小割,问不同的最小割的数量。

Sol

最小割树。

每次最小割完,对于源点集和汇点集分别再做一遍最小割。

这样递归下去对于每次的源点和汇点连边,边权为最小割的值。

Code

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
#include <queue>
#include <vector>
#include <bitset>
#include <tuple>
#include <unordered_map>
#define pii pair <int, int>
using namespace std;
#ifdef ONLINE_JUDGE

#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf;

#endif
int read() {
    int p = 0, flg = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-') flg = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        p = p * 10 + c - '0';
        c = getchar();
    }
    return p * flg;
}
void write(int x) {
    if (x < 0) {
        x = -x;
        putchar('-');
    }
    if (x > 9) {
        write(x / 10);
    }
    putchar(x % 10 + '0');
}

#define fi first
#define se second

const int N = 1005, M = 4e4 + 5, inf = 2e9;

namespace G {

array <int, N> fir;
array <int, M> nex, to, cap;
int cnt = 1;
void add(int x, int y, int z) {
    cnt++;
    nex[cnt] = fir[x];
    to[cnt] = y;
    cap[cnt] = z;
    fir[x] = cnt;
}
void _add(int x, int y, int z) {
    add(x, y, z);
    add(y, x, 0);
}

}

namespace Mfl {

array <int, N> dis, cur;
queue <int> q;

bool bfs(pii st) {
    dis.fill(-1), dis[st.fi] = 0;
    q.push(st.fi);
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (int i = G::fir[u]; i; i = G::nex[u]) {
            if (!G::cap[i] || ~dis[G::to[i]]) continue;
            dis[G::to[i]] = dis[u] + 1, q.push(G::to[i]);
        }
    }
    return ~dis[st.se];
}  

int dfs(int x, int augcd, pii st) {
    if (x == st.se) return augcd;
    int augc = augcd;
    for (int &i = cur[x]; i; i = G::nex[i]) {
        if (!G::cap[i] || dis[G::to[i]] <= dis[x]) continue;
        int flow = dfs(G::to[i], min(augc, G::cap[i]), st);
        augc -= flow;
        G::cap[i] -= flow, G::cap[i ^ 1] += flow;
        if (!augc) break;
    }
    return augcd - augc;
}

int dinic(pii st) {
    int ans = 0;
    while (bfs(st)) {
        // copy(G::fir.begin(), G::fir.end(), cur.begin());
        cur = G::fir;
        ans += dfs(st.fi, inf, st);
    }
    return ans;
}

}

array <tuple <int, int, int>, M> edge;


namespace Mft {

bitset <N> vis;

void dfs(int x) {
    vis[x] = 1;
    for (int i = G::fir[x]; i; i = G::nex[i]) {
        if (!G::cap[i] || vis[G::to[i]]) continue;
        dfs(G::to[i]);
    }
}

unordered_map <int, int> mp;

void solve(vector <int> isl, int n, int m) {
    if (isl.size() <= 1) return;    
    pii st = make_pair(isl.front(), isl.back());
    G::fir.fill(0), G::cnt = 1;
    for (int i = 1; i <= m; i++) {
        G::_add(get <0>(edge[i]), get <1>(edge[i]), get <2>(edge[i]));
        G::_add(get <1>(edge[i]), get <0>(edge[i]), get <2>(edge[i]));
    }
    int ans = Mfl::dinic(st);
    vis = 0;
    mp[ans]++, dfs(st.fi);
    write(ans), puts("");
    vector <int> lpr, rpl;
    for (auto x : isl) {
        if (x == st.fi || x == st.se) continue;
        vis[x] ? lpr.push_back(x) : rpl.push_back(x);
    }
    for (auto x : isl)
        write(x), putchar(32);
    puts("");
    lpr.push_back(st.fi), rpl.push_back(st.se);
    solve(lpr, n, m), solve(rpl, n, m);
}

}

int main() {
    int n = read(), m = read();
    for (int i = 1; i <= m; i++) {
        int u = read(), v = read(), w = read();
        edge[i] = make_tuple(u, v, w);
    }
    vector <int> isl; for (int i = 1; i <= n; i++) isl.push_back(i);
    Mft::solve(isl, n, m);
    write(Mft::mp.size()), puts("");
    return 0;
}