BZOJ4519——[cqoi2016]不同的最小割

0、题意:求两点之间的最小割的不同的总量

1、分析:裸的分治+最小割,也叫最小割树或GH树,最后用set搞一下就好

#include <set>
#include <queue>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <cstring>
#include <algorithm>
using namespace std;
#define LL long long
#define inf 214748364
 
struct Edge{
    int from, to, cap, flow, next;
};
int head[1010], cur[1010];
Edge G[40010];
int tot;
int d[1010];
bool vis[1010];
int s, t, n, m;
int a[1010];
int b[1010];
 
inline void init(){
    memset(head, -1, sizeof(head));
    tot = -1;
    return;
}
 
inline void insert(int from, int to, int cap){
    G[++ tot] = (Edge){from, to, cap, 0, head[from]};
    head[from] = tot;
    G[++ tot] = (Edge){to, from, 0, 0, head[to]};
    head[to] = tot;
    return;
}
 
inline bool BFS(){
    memset(vis, 0, sizeof(vis));
    queue<int> Q;
    Q.push(s);
    vis[s]=1;
    d[s]=0;
    while(!Q.empty()){
        int x = Q.front(); Q.pop();
        for(int i = head[x]; i != -1; i = G[i].next){
            Edge& e = G[i];
            if(e.cap - e.flow > 0 && !vis[e.to]){
                vis[e.to] = 1;
                d[e.to]=d[x]+1;
                Q.push(e.to);
            }
        }
    }
    return vis[t];
}
 
inline int dfs(int x, int a){
    if(x == t || a == 0) return a;
    int flow = 0, f;
    for(int& i = cur[x]; i != -1; i = G[i].next){
        Edge& e = G[i];
        if(d[x]+1 == d[e.to] && (f = dfs(e.to, min(e.cap - e.flow, a))) > 0){
            e.flow += f;
            G[i ^ 1].flow -= f;
            flow += f;
            a -= f;
            if(a == 0) break;
        }
    }
    return flow;
}
 
inline int maxflow(){
    int res = 0;
    while(BFS()){
        for(int i = 1; i <= n; i ++) cur[i] = head[i];
        res += dfs(s, inf);
    }
    return res;
}
 
inline void DFS(int x){
    vis[x] = 1;
    for(int i = head[x]; i != -1; i = G[i].next) if(!vis[G[i].to] && G[i].cap > G[i].flow){
        DFS(G[i].to);
    }
}
 
inline void Clear(){
    for(int i = 0; i <= tot; i += 2){
        G[i].flow = G[i ^ 1].flow = (G[i].flow + G[i ^ 1].flow) / 2;
    }
}
 
set<int> Set;
 
inline void solve(int l, int r){
    if(l == r) return;
    s = a[l], t = a[r];
    Clear();
 
    int tw = maxflow();
    Set.insert(tw);
    int L = l, R = r;
    for(int i = l; i <= r; i ++){
        if(vis[a[i]]) b[L ++] = a[i];
        else b[R --] = a[i];
    }
    for(int i = l; i <= r; i ++) a[i] = b[i];
    solve(l, L - 1); solve(L, r);
}
 
int main(){
    scanf("%d%d", &n, &m);
    init();
    for(int i = 1; i <= m; i ++){
        int u, v, w;
        scanf("%d%d%d", &u, &v, &w);
        insert(u, v, w); insert(v, u, w);
    }
    for(int i = 1; i <= n; i ++) a[i] = i;
    solve(1, n);
    printf("%d\n", Set.size());
    return 0;
}


你可能感兴趣的:(bzoj,最小割树,CQOI2016,不同的最小割,4519)