zoukankan      html  css  js  c++  java
  • 生活的艰辛(最小割,最大密度子图)

    题意

    求不带点权和边权的最大密度子图

    思路

    最大密度子图模板题

    代码

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    
    #define x first
    #define y second
    
    using namespace std;
    
    typedef pair<int, int> pii;
    
    const int N = 110, M = 2410;
    const double eps = 1e-8, inf = 1e8;
    
    int n, m, S, T;
    int h[N], e[M], ne[M], idx;
    double f[M];
    int cur[N], d[N];
    int dg[N];
    bool st[N];
    int ans;
    pii edge[M];
    
    void add(int a, int b, double c1, double c2)
    {
        e[idx] = b, f[idx] = c1, ne[idx] = h[a], h[a] = idx ++;
        e[idx] = a, f[idx] = c2, ne[idx] = h[b], h[b] = idx ++;
    }
    
    void build(double mid)
    {
        memset(h, -1, sizeof(h));
        idx = 0;
        for(int i = 0; i < m; i ++) {
            int a = edge[i].x, b = edge[i].y;
            add(a, b, 1, 1);
        }
        for(int i = 1; i <= n; i ++) {
            add(S, i, m, 0);
            add(i, T, 2 * mid - dg[i] + m, 0);
        }
    }
    
    bool bfs()
    {
        memset(d, -1, sizeof(d));
        queue<int> que;
        que.push(S);
        d[S] = 0, cur[S] = h[S];
        while(que.size()) {
            int t = que.front();
            que.pop();
            for(int i = h[t]; ~i; i = ne[i]) {
                int ver = e[i];
                if(d[ver] == -1 && f[i] > 0) {
                    d[ver] = d[t] + 1;
                    cur[ver] = h[ver];
                    if(ver == T) return true;
                    que.push(ver);
                }
            }
        }
        return false;
    }
    
    double find(int u, double limit)
    {
        if(u == T) return limit;
        double flow = 0;
        for(int i = cur[u]; ~i && limit > flow; i = ne[i]) {
            cur[u] = i;
            int ver = e[i];
            if(d[ver] == d[u] + 1 && f[i] > 0) {
                double t = find(ver, min(f[i], limit - flow));
                if(t <= 0) d[ver] = -1;
                f[i] -= t, f[i ^ 1] += t, flow += t;
            }
        }
        return flow;
    }
    
    double dinic(double mid)
    {
        build(mid);
        double res = 0, flow;
        while(bfs()) {
            while(flow = find(S, inf)) {
                res += flow;
            }
        }
        return res;
    }
    
    void dfs(int u)
    {
        st[u] = true;
        if(u != S) ans ++;
        for(int i = h[u]; ~i; i = ne[i]) {
            int ver = e[i];
            if(!st[ver] && f[i] > 0) {
                dfs(ver);
            }
        }
    }
    
    int main()
    {
        scanf("%d%d", &n, &m);
        S = 0, T = n + 1;
        for(int i = 0; i < m; i ++) {
            int a, b;
            scanf("%d%d", &a, &b);
            edge[i] = {a, b};
            dg[a] ++, dg[b] ++;
        }
        double l = 0, r = 1000;
        while(r - l > eps) {
            double mid = (l + r) / 2;
            double res = dinic(mid);
            if(n * m - res > 0) l = mid;
            else r = mid;
        }
        dinic(l);
        dfs(S);
        if(!ans) puts("1
    1");
        else {
            printf("%d
    ", ans);
            for(int i = 1; i <= n; i ++) {
                if(st[i]) {
                    printf("%d
    ", i);
                }
            }
        }
        return 0;
    }
    
  • 相关阅读:
    我谈编程语言竞争
    从基础学起----xuld版高手成长手记[1]
    自己动手开发语言.笔记@2014-1-13
    删除 QQ 最新版右键菜单 通过QQ发送文件到手机
    客观评价C#的优点和缺点
    一个会做你就是高手的问题
    计划开发的语言及一些细节求吐槽
    面向接口设计思想
    计划添加的复杂语法
    面向对象中的设计陷阱
  • 原文地址:https://www.cnblogs.com/miraclepbc/p/14407947.html
Copyright © 2011-2022 走看看