首页 > 其他分享 >P4123 [CQOI2016] 不同的最小割

P4123 [CQOI2016] 不同的最小割

时间:2023-12-11 15:24:16浏览次数:34  
标签:cnt include int 最小 st edge P4123 CQOI2016 dis

题意

给 \(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;
}

标签:cnt,include,int,最小,st,edge,P4123,CQOI2016,dis
From: https://www.cnblogs.com/cxqghzj/p/17894506.html

相关文章

  • 111. 二叉树的最小深度
    目录题目完美踩坑题解题目给定一个二叉树,找出其最小深度。最小深度是从根节点到最近叶子节点的最短路径上的节点数量。示例1:输入:root=[3,9,20,null,null,15,7]输出:2示例2:输入:root=[2,null,3,null,4,null,5,null,6]输出:5完美踩坑之前好几个题做过求树的高......
  • [LeetCode Hot 100] LeetCode155. 最小栈
    题目描述思路一:使用辅助栈定义一个[数据栈]来支持push、pop、top操作定义一个[辅助栈],其栈顶为当前的最小值,以支持常数时间复杂度的getMin操作思路二:使用ArrayDeque栈元素中除了保存当前值之外,额外保存当前最小值使用静态内部类方法一:对应思路一classMinStack{......
  • 【教3妹学编程-算法题】需要添加的硬币的最小数量
    3妹:2哥2哥,你有没有看到新闻,有人中了2.2亿彩票大奖!2哥 :看到了,2.2亿啊,一生一世也花不完。3妹:为啥我就中不了呢,不开心呀不开心。2哥 :得了吧,你又不买彩票,还是脚踏实地的好~3妹:小富靠勤,中富靠德,大富靠命,可能是我命不好。2哥 :哎,想我口袋只有几个硬币,叮咚作响。3妹:说到硬币,我......
  • 2023-12-09:用go语言,给你两个整数数组 arr1 和 arr2, 返回使 arr1 严格递增所需要的最小
    2023-12-09:用go语言,给你两个整数数组arr1和arr2,返回使arr1严格递增所需要的最小「操作」数(可能为0)。每一步「操作」中,你可以分别从arr1和arr2中各选出一个索引,分别为i和j,0<=i<arr1.length和0<=j<arr2.length,然后进行赋值运算arr1[i]=arr2[j]。如果......
  • 最小费用组最大流——EK算法
    时间复杂度O(nm^2),理论上限//n,m,s,t,分别代表该网络的点数n,网络的边数m,源点编号s,汇点编号t。constintN=5010,M=100010,INF=1e8;intn,m,S,T;structedge{intv,c,w,ne;}e[M];inth[N],idx=1;//从2,3开始配对intd[N],mf[N],pre[N],vis[N];intflow,cost;voidadd(inta,......
  • 最小生成树
    假设\(n\)表示图中点数,\(m\)表示图中边数。Prim算法适用于稠密图,时间复杂度\(O(n^2)\)。核心思想:每次挑一条与当前集合相连的最短边。C++代码//st[i]表示点i是否在当前生成树集合中//dist[i]表示点i到当前集合的最短边的长度//g[i][j]表示点i和点j之间边的长度......
  • n个数算最小公倍数(优化版)
    #include<stdio.h>intret(intx,inty){  inti=1;  if(x%y==0||y%x==0)    return(x>y?x:y);  else  {   for(i=1;i<=x*y;i++)    {      i=x*i;      if(i%y==0) ......
  • QT最小化程序到托盘运行
    MinTray说明实现程序关闭时最小化托盘的功能托盘实现显示主页面和退出的功能支持扩展,直接引用TrayIcon类即可,对外暴露接口单例实现,可复用警告注:博主所有资源永久免费,若有帮助,请点赞转发是对我莫大的帮助注:博主本人学习过程的分享,引用他人的文章皆会标注原......
  • WPF 最大化,最小化,关闭,拖拽,双击事件
    十年河东,十年河西,莫欺少年穷学无止境,精益求精代码如下publicMainView(){InitializeComponent();//最小化btnMin.Click+=(s,e)=>{this.WindowState=WindowState.Minimized;};//最大化b......
  • 基于ATMega16的最小系统及其开发环境简介
    AVR实验例程用的最小系统如下图所示,芯片采用ATMega16A,主晶振频率为8MHz,异步晶振频率为32768Hz,系统采用JTAG接口调试及下载程序。以上仅是最小系统的电路图,后续例程中使用到的额外电路会在例程中给出相应的模块电路。AVRStudio集成开发环境(IDE)是专门用于开发AVR单片机的开发软......