0、题目大意:求两点之间的最小割之和

1、分析:很明显,最小割树,我们发现这个题并不能用n^3的方法来求答案。。

所以我们记录下所有的边,然后把边从大到小排序,然后跑一边类似kruskal的东西,顺便统计答案

TAT,这个题我被卡常数了,贴上TLE的代码吧。。。

#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[3010], cur[3010];
Edge G[20010];
int tot;
int d[3010];
bool vis[3010];
int s, t, n, m;
int a[3010];
int b[3010];

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(){
    for(int i = 1; i <= n; i ++) vis[i] = 0;
    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 Clear(){
    for(int i = 0; i <= tot; i += 2){
        G[i].flow = G[i ^ 1].flow = (G[i].flow + G[i ^ 1].flow) / 2;
    }
}

struct node{
    int u, v, w;
    inline bool operator < (const node& rhs) const{
        return w > rhs.w;
    }
} T[20010];
int num;

inline void add(int u, int v, int w){
    T[++ num].u = u;
    T[num].v = v;
    T[num].w = w;
}

inline void solve(int l, int r){
    if(l == r) return;
    int rl = rand() % (r - l + 1) + l;
    int rr = rand() % (r - l + 1) + l;
    if(rl == rr) rl ++;
    if(rl > r) rl -= 2;
    s = a[rl], t = a[rr];
    Clear();

    int tw = maxflow();
    //puts("fuck");
    add(a[rl], a[rr], 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 ff[20010], size[20010];

inline int find(int x){
    return ff[x] == x ? x : ff[x] = find(ff[x]);
}

int main(){
   // srand(time(NULL));
    scanf("%d%d", &n, &m);
    init();
    for(int i = 1; i <= m; i ++){
        int u, v;
        scanf("%d%d", &u, &v);
        insert(u, v, 1); insert(v, u, 1);
    }
    for(int i = 1; i <= n; i ++) a[i] = i;
    solve(1, n);
    LL ret = 0;
    sort(T + 1, T + num + 1);
    for(int i = 1; i <= n; i ++) size[i] = 1, ff[i] = i;
   // puts("fuck");
    for(int i = 1; i <= num; i ++){
        int tx = find(T[i].u), ty = find(T[i].v);
        if(size[tx] < size[ty]) swap(tx, ty);
        ret += (LL)T[i].w * size[tx] * size[ty];
        ff[ty] = tx;
        size[tx] += size[ty];
    }
    printf("%lld\n", ret);
    return 0;
}


BZOJ4435——[Cerc2015]Juice Junctions的更多相关文章

  1. BZOJ4435 : [Cerc2015]Juice Junctions

    最大流=最小割,而因为本题点的度数不超过3,所以最小割不超过3,EK算法的复杂度为$O(n+m)$. 通过分治求出最小割树,设$f[i][j][k]$表示最小割为$i$时,$j$点在第$k$次分治过程 ...

  2. bzoj4435: [Cerc2015]Juice Junctions(最小割树+hash)

    传送门 首先最大流等于最小割,那么可以转化为最小割树来做(不知道什么是最小割树的可以看看这题->这里) 具体的做法似乎是$hash[i][j]$表示最小割为$i$时点$j$是否与$S$连通 然后 ...

  3. 【BZOJ4435】[Cerc2015]Juice Junctions Tarjan+hash

    [BZOJ4435][Cerc2015]Juice Junctions Description 你被雇佣升级一个旧果汁加工厂的橙汁运输系统.系统有管道和节点构成.每条管道都是双向的,且每条管道的流量都 ...

  4. 【BZOJ-4435】Juice Junctions 最小割树(分治+最小割)+Hash

    4435: [Cerc2015]Juice Junctions Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 20  Solved: 11[Submi ...

  5. BZOJ 4435 [Cerc2015]Juice Junctions 分治最小割+hash

    分治最小割的题目,要求n2. 之前用的n3的方法自然不能用了. 于是用hash,设hash[i][j]表示在最小割为i的时候,j是否与S联通. 看懂这个需要理解一下最小割树的构造. 这种题建议用EK写 ...

  6. [CERC2015]Juice Junctions(边双连通+字符串hash)

    做法 考虑边数限制的特殊条件,显然答案仅有\(\{0,1,2,3\}\) 0:不联通 1:连通 2:边双连通 3:任意删掉一条边都为边双连通 考虑每次删边后记录各点的边双染色情况来特判\(3\):是否 ...

  7. Juice Junctions

    Juice Junctions 题目描述 你被雇佣升级一个旧果汁加工厂的橙汁运输系统.系统有管道和节点构成.每条管道都是双向的,且每条管道的流量都是11升每秒.管道可能连接节点,每个节点最多可以连接3 ...

  8. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  9. Gym - 101480 CERC 15:部分题目题解(队内第N次训练)

    -------------------题目难度较难,但挺有营养的.慢慢补. A .ASCII Addition pro:用一定的形式表示1到9,让你计算加法. sol:模拟. solved by fz ...

随机推荐

  1. 关于input/textarea提交内容空格回车转换问题,以及ng-model去除空格问题

    input/textarea提交内容空格回车转换问题 /*my-enter-bind.js*/ /*回车换行显示转义*/ 'use strict'; angular.module('app') .di ...

  2. SSH项目与SSM项目的进入首页的方法

    SSH项目中: jsp页面一般都是存放在WEB-INF下面的目录下,这样我们就不能直接访问到这些jsp页面了,保证了页面的安全性. 在struts的管理中,是利用action来实现页面的跳转,进入in ...

  3. Cross-Site Scripting(XSS)简介

    最近才开始研究HTML以及安全问题.如果有什么说得不对的地方,望请指出. 在网络应用安全中,XSS可能是最常见,范围最大,所包含攻击方法最多,同时也是最难以理解的一种攻击.在OWASP所列出的十大网络 ...

  4. php5.1以上版本时间戳_时间戳与日期格式转换_相差8小时 的解决方案

    php5.1以上时间戳会与实际时间相差8小时,解决办法如下 .最简单的方法就是不要用php5.1以上的版本--显然这是不可取的方法!!! .修改php.ini.打开php.ini查找date.time ...

  5. Java 组播

    MulticastSocketServer.java package cn.edu.buaa.multicast; import java.io.IOException; import java.ne ...

  6. Java 并发编程之volatile关键字解析

    摘录 1. 计算机在执行程序时,每条指令都是在CPU中执行的,而执行指令过程中,势必涉及到数据的读取和写入.由于程序运行过程中的临时数据是存放在主存(物理内存)当中的,这时就存在一个问题,由于CPU执 ...

  7. [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify uniq

    angularjs 使用ng-repeat报错 <div ng-init="words = ['高校','高校','高校']" ng-repeat="word in ...

  8. Python 之匿名函数和偏函数

    匿名函数与偏函数 匿名函数 Python允许使用lambda关键字创造匿名函数,lambda表达式用于定义匿名函数,它返回可调用的函数对象,语法如下: lambda arg1, arg2, … : e ...

  9. 微信jssdk录音功能开发记录

    原文地址:http://www.cnblogs.com/liujunyang/p/4962423.html

  10. Yii2 yii2-imagine的使用

    <?php /** * 图片常用处理 * * 需要 yii/yii2-imagine 的支持 * php composer.phar require --prefer-dist yiisoft/ ...