poj3155 Hard Life 二分+最大权闭合子图

37 篇文章 0 订阅
17 篇文章 0 订阅

Description


John is a Chief Executive Officer at a privately owned medium size company. The owner of the company has decided to make his son Scott a manager in the company. John fears that the owner will ultimately give CEO position to Scott if he does well on his new manager position, so he decided to make Scott’s life as hard as possible by carefully selecting the team he is going to manage in the company.

John knows which pairs of his people work poorly in the same team. John introduced a hardness factor of a team — it is a number of pairs of people from this team who work poorly in the same team divided by the total number of people in the team. The larger is the hardness factor, the harder is this team to manage. John wants to find a group of people in the company that are hardest to manage and make it Scott’s team. Please, help him.

In the example on the picture the hardest team consists of people 1, 2, 4, and 5. Among 4 of them 5 pairs work poorly in the same team, thus hardness factor is equal to 5⁄4. If we add person number 3 to the team then hardness factor decreases to 6⁄5.

1 ≤ n ≤ 100
0 ≤ m ≤ 1000

Solution


apio听课讲的一道题,觉得没写过又比较可写就写了,写完发现坑还是挺多的
翻译题意就是裸的最大密度子图
设选出子图点集为V’,边集为E’,那么我们要做的就是最大化 |E||V| | E ′ | | V ′ |
考虑二分一个答案mid,移项后变成 |E|mid|V| | E ′ | − m i d | V ′ | 。判断这个东西的符号就能知道怎么移动二分的上下界了
怎么判断可以把边看成点,边权赋为1,点权赋为-mid,利用‘选择一条边则必定选择边两端的两个点’这个性质建图跑最大权闭合子图即可

Code


#include <stdio.h>
#include <string.h>
#include <algorithm>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)
#define fill(x,t) memset(x,t,sizeof(x))

const double EPS=0.0000001;
const int INF=0x3f3f3f3f;
const int N=200005;
const int E=4000005;

struct edge {int x,y; double w; int next;} e[E];

int a[N],b[N];
int queue[N],dis[N];
int cur[N],ls[N],edCnt;

bool vis[N];

void add_edge(int x,int y,double w) {
    e[++edCnt]=(edge) {x,y,w,ls[x]}; ls[x]=edCnt;
    e[++edCnt]=(edge) {y,x,0,ls[y]}; ls[y]=edCnt;
}

bool bfs(int st,int ed) {
    fill(dis,-1); dis[st]=1;
    int head=1,tail=0;
    queue[++tail]=st;
    while (head<=tail) {
        int x=queue[head++];
        for (int i=ls[x];i;i=e[i].next) {
            if (e[i].w>0&&dis[e[i].y]==-1) {
                dis[e[i].y]=dis[x]+1;
                queue[++tail]=e[i].y;
                if (e[i].y==ed) return true;
            }
        }
    }
    return false;
}

double find(int x,int ed,double mn) {
    if (x==ed||!mn) return mn;
    double ret=0;
    for (int i=ls[x];i;i=e[i].next) {
        if (e[i].w>0&&dis[x]+1==dis[e[i].y]) {
            double d=find(e[i].y,ed,std:: min(mn-ret,e[i].w));
            e[i].w-=d; e[i^1].w+=d; ret+=d;
            if (ret==mn) break;
        }
    }
    return ret;
}

double dinic(int st,int ed) {
    double ret=0;
    while (bfs(st,ed)) {
        rep(i,st,ed) cur[i]=ls[i];
        ret+=find(st,ed,INF);
    }
    return ret;
}

double check(int n,int m,double mid) {
    fill(ls,0); edCnt=1;
    int st=0,ed=n+m+1;
    rep(i,1,n) add_edge(i,ed,mid);
    rep(i,1,m) add_edge(st,i+n,1);
    rep(i,1,m) {
        add_edge(i+n,a[i],INF);
        add_edge(i+n,b[i],INF);
    }
    return m-dinic(st,ed);
}

void bfsSol(){
    fill(vis,0); vis[0]=1;
    int head=1,tail=0;
    queue[++tail]=0;
    while(head<=tail){
        int x=queue[head++];
        for(int i=ls[x];i;i=e[i].next) {
            if(!vis[e[i].y]&&e[i].w>0) {
                vis[e[i].y]=1;
                queue[++tail]=e[i].y;
            }
        }
    }
}

int main(void) {
    freopen("data.in","r",stdin);
    freopen("myp.out","w",stdout);
    int n,m; scanf("%d%d",&n,&m);
    rep(i,1,m) scanf("%d%d",&a[i],&b[i]);
    double l=1.0/n,r=m;
    while (r-l>=EPS) {
        double mid=(l+r)*0.5;
        if (check(n,m,mid)>EPS) l=mid;
        else r=mid;
    }
    if (m==0) {
        puts("1\n1");
        return 0;
    }
    check(n,m,l); printf("%.5lf\n", l);
    bfsSol(); int ans=0;
    rep(i,1,n) if (vis[i]) ans++;
    printf("%d\n", ans);
    rep(i,1,n) if (vis[i]) printf("%d\n", i);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值