poj3155 Hard Life

Hard Life
Time Limit: 8000MS Memory Limit: 65536K
Total Submissions: 8554 Accepted: 2481
Case Time Limit: 2000MS Special Judge

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 54. If we add person number 3 to the team then hardness factor decreases to 65.

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 ≤ aibi ≤ nai ≠ 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.


题意:求一个图的最大密度子图。

解题思路:

1、二分对最大密度进行枚举,建图时对原图的边建立无向边,容量为1,超级源点到各点建立有向边,容量为边的数量m,各点到超级汇点建立有向边,容量是m-2*最大密度-该点的度。

2、对于最大密度进行二分时,初始上界是m,下界是0,当上界减下界小于点的数量平方分之一时二分停止,这里运用的是01分数规划的知识(暂时还没看明白)

3、当二分出最大密度之后,需要在重建一边图来输出那些点。

解题思路及证明参照《最小割模型在信息学竞赛中的应用》(胡伯涛著)

PPT链接:http://wenku.baidu.com/view/6507a6fe2cc58bd63186bdaf.html

楼主表示证明过程灰常复杂难懂,所以暂时是记住了最大密度子图怎么求了,但是证明原理什么的不会。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int MAXN=105;
const int inf=1e9;
int n,m;
int du[MAXN];
double tu[MAXN][MAXN];
int s,e;
struct node
{
    int u,v;
}edge[1000+10];
void build(double g)
{
    memset(tu,0,sizeof(tu));
    int i;
    for(i=0; i<m; ++i)tu[edge[i].u][edge[i].v]=tu[edge[i].v][edge[i].u]=1.0;
    for(i=1; i<=n; ++i)
    {
        tu[s][i]=m*1.0;
        tu[i][e]=m+2*g-du[i];
    }
}
int pre[MAXN];
double flow[MAXN];
double EK()
{
    int i;
    for(i=0; i<=e; ++i)pre[i]=-1;
    queue<int>q;
    q.push(s);
    flow[s]=inf*1.0;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        if(u==e)break;
        for(i=1; i<=e; ++i)
        {
            if(pre[i]==-1&&tu[u][i]>0)
            {
                pre[i]=u;
                flow[i]=min(flow[u],tu[u][i]);
                q.push(i);
            }
        }
    }
    if(pre[e]==-1)return 0;
    else return flow[e];
}

double get_maxflow()
{
    double max_flow=0.0;
    double flow=EK();
    while(flow>0)
    {
        max_flow+=flow;
        int u=pre[e],v=e;
        while(u!=-1)
        {
            tu[u][v]-=flow;
            tu[v][u]+=flow;
            v=u;
            u=pre[v];
        }
        flow=EK();
    }
    return max_flow;
}

bool vis[MAXN];
int ans;
void dfs(int x)
{
    vis[x]=1;
    if(x<=n&&x>0)ans++;
    for(int i=1; i<=n; ++i)
    {
        if(!vis[i]&&tu[x][i]>0)dfs(i);
    }
}
int main()
{
    int i;
    scanf("%d%d",&n,&m);
    if(!m)puts("1\n1");
    else
    {
        memset(du,0,sizeof(du));
        for(i=0; i<m; ++i)
        {
            scanf("%d%d",&edge[i].u,&edge[i].v);
            du[edge[i].u]++;
            du[edge[i].v]++;
        }
        double high=m,low=0;
        s=0,e=n+1;

        while(high-low>1.0/n/n)//二分搜索最大密度(01分数规划)
        {
            double mid=(high+low)/2.0,t;
            build(mid);//建立残留网络
            t=get_maxflow();//得到最大流
            t=(m*n-t)/2;
            if(t>1e-6)
            {
                low=mid;
            }
            else high=mid;
        }
        //二分完毕之后要用答案重新构造一遍图,然后把每条流量大于0的边相连的顶点输出
        build(low);
        get_maxflow();
        ans=0;
        dfs(0);
        printf("%d\n",ans);
        for(i=1; i<=n; ++i)if(vis[i])printf("%d\n",i);
    }
    return 0;
}





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值