最大密度子图:

这篇博客讨论了一个名为'Hard Life POJ - 3155'的问题,其中CEO John试图通过找出公司中最大硬度子图来使新任经理Scott的工作变得困难。硬度因子定义为团队中不配合工作的人数对团队总人数的比例,目标是找到最难管理的团队。输入包含公司人数和不配合工作的人员对,输出是最难团队的成员。博主分享了解决此类最大密度子图问题的模板和精度调整的经验。
摘要由CSDN通过智能技术生成

一、无边权点权:
Hard Life POJ - 3155 :
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.

Input
The first line of the input file contains two integer numbers n and m (1 ≤ n ≤ 100, 0 ≤ m ≤ 1000). Here n is a total number of people in the company (people are numbered from 1 to n), and m is the number of pairs of people who work poorly in the same team. Next m lines describe those pairs with two integer numbers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi) on a line. The order of people in a pair is arbitrary and no pair is listed twice.

Output
Write to the output file an integer number k (1 ≤ k ≤ n) — the number of people in the hardest team, followed by k lines listing people from this team in ascending order. If there are multiple teams with the same hardness factor then write any one.

Sample Input
sample input #1
5 6
1 5
5 4
4 2
2 5
1 2
3 1

sample input #2
4 0
Sample Output
sample output #1
4
1
2
4
5

sample output #2
1
1
Hint
Note, that in the last example any team has hardness factor of zero, and any non-empty list of people is a valid answer.

求最大密度子图模板题:
这题卡精度就不好了。。。。
调了一晚上。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#define ll long long
using namespace std;
const int maxx=210;
const int maxn=10100;
const int inf=0x3f3f3f3f;
const double dnf=1e9;
const double eps=1e-12;
int head[maxx],ver[maxn],nt[maxn];
double edge[maxn];
int d[maxx],du[maxx],n,m,s,t,tot;
bool ha[maxx];
int a[maxn],b[maxn],top=0;

void add(int x,int y,double z)
{
    ver[++tot]=y,edge[tot]=z,nt[tot]=head[x],head[x]=tot;
    ver[++tot]=x,edge[tot]=0,nt[tot]=head[y],head[y]=tot;
}

bool bfs(void)
{
    memset(d,0,sizeof(d));
    queue<int>q;

    q.push(s);
    d[s]=1;

    while(q.size())
    {
        int x=q.front();
        q.pop();

        for(int i=head[x];i;i=nt[i])
        {
            int y=ver[i];
            if(edge[i]>=eps&&!d[y])
            {
                q.push(y);
                d[y]=d[x]+1;
                if(y==t) return true;
            }
        }
    }
    return false;
}

double dinic(int x,double flow)
{
    if(x==t) return flow;
    double rest=flow,k;

    for(int i=head[x];i&&(rest>=eps);i=nt[i])
    {
        int y=ver[i];
        if(edge[i]>=eps&&d[y]==d[x]+1)
        {
            k=dinic(y,min(rest,edge[i]));
            if(k<eps) d[y]=0;
            edge[i]-=k;
            edge[i^1]+=k;
            rest-=k;
        }
    }
    return flow-rest;
}

bool check(double mid)
{

    tot=1;
    memset(head,0,sizeof(head));
    for(int i=1;i<=n;i++)
    {
        add(s,i,m);
        add(i,t,m+mid*2-du[i]);
    }
    for(int i=1;i<=m;i++)
    {
        add(a[i],b[i],1.0);
        add(b[i],a[i],1.0);
    }
    double flow=0,maxflow=0;
    while(bfs())
        while((flow=dinic(s,dnf))>=eps)
            maxflow+=flow;
    return (1.0*m*n-maxflow)/2>=eps;
}

void dfs(int x)
{

    for(int i=head[x];i;i=nt[i])
    {
        if(edge[i]>=eps&&!ha[ver[i]])
        {
            ha[ver[i]]=true;
            top++;
            dfs(ver[i]);
        }
    }
}

int main(void)
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(ha,0,sizeof(ha));
        memset(du,0,sizeof(du));
        top=0;
        s=n+1,t=n+2;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&a[i],&b[i]);
            du[a[i]]++,du[b[i]]++;
        }
        if(m==0)
        {
            printf("1\n1\n");
            continue;
        }
        double ee=1.0/n/n;
        //其实讲道理,这里 l 下限应该取 1.0/n即可
        //可是过不了。
        double l=0,r=m/1.0,mid;
        while(r-l>=ee)
        {
            mid=(l+r)/2.0;
            if(check(mid)) l=mid;
            else r=mid;
        }

        check(l);
        ha[s]=true;
        dfs(s);


        printf("%d\n",top);
        for(int i=1;i<=n;i++)
        {
            if(ha[i])
            printf("%d\n",i);
        }
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值