最大密度子图

Hard Life
Time Limit: 8000MS   Memory Limit: 65536K
Total Submissions: 7347   Accepted: 2114
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 Outpusample output #14

1
2
4
5

sample output #2
1
1

题意:给出一个无向连通图,求出一个子图,使得(边的条数)/(点的个数)比值最大,所有的边都在子图中,所有的点都在子图中;
分析:建边方式:原来图中的边(u,v)直接连接双向边,流量为1,源点到每个顶点建边,流量为m,每个点到汇点建边,流量为m+2*g-dv;
最小割证明结果如下:
  cut[s,t]=m*n-2(|E'|-g|V'|);
其中:
  g=args(e)/args(v),dv是每个点的度H(g)=(m*n-cut[s,t])/2,H(g)=|E'|-g|V'|;
二分枚举g值,当H(g)取得0的时候g值最大

该函数H(g)的函数曲线是随着g值的增大先递减然后保持0不变,而正确的答案是H刚突变到0的g值

#include"stdio.h"
#include"string.h"
#include"queue"
#define inf 0x3f3f3f3f
#define LL double
#define M 3000
#define eps 1e-8
using namespace std;
LL min(LL a,LL b)
{
    return a<b?a:b;
}
struct node
{
    int u,v,next;
    LL w;
}edge[M*4],e[M*4];
int t,head[M],dis[M],work[M],du[M],use[M],cnt;
void init()
{
    t=0;
    memset(head,-1,sizeof(head));
}
void add(int u,int v,LL w)
{
    edge[t].u=u;
    edge[t].v=v;
    edge[t].w=w;
    edge[t].next=head[u];
    head[u]=t++;
}
int bfs(int S,int T)
{
    queue<int>q;
    memset(dis,-1,sizeof(dis));
    dis[S]=0;
    q.push(S);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            if(edge[i].w>eps&&dis[v]==-1)
            {
                dis[v]=dis[u]+1;
                if(v==T)
                    return 1;
                q.push(v);
            }
        }
    }
    return 0;
}
LL dfs(int cur,LL a,int T)
{
    if(cur==T)return a;
    for(int i=head[cur];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(edge[i].w>eps&&dis[v]==dis[cur]+1)
        {
            LL tt=dfs(v,min(a,edge[i].w),T);
            if(tt)
            {
                edge[i].w-=tt;
                edge[i^1].w+=tt;
                return tt;
            }
        }
    }
    return 0;
}
LL Dinic(int S,int T)
{
    LL ans=0;
    while(bfs(S,T))
    {
        while(LL tt=dfs(S,inf,T))
            ans+=tt;
    }
    return ans;
}
LL creat(int n,int m,LL g)
{
    init();
    for(int i=1;i<=n;i++)
    {
        add(0,i,m);
        add(i,0,0);
        add(i,n+1,m+2*g-du[i]);
        add(n+1,i,0);
    }
    for(int i=1;i<=m;i++)
    {
        add(e[i].u,e[i].v,1);
        add(e[i].v,e[i].u,1);
    }
    return Dinic(0,n+1);
}
void DFS(int u)
{
    cnt++;
    use[u]=1;
    for(int i=head[u];~i;i=edge[i].next)
    {
        int v=edge[i].v;
        if(!use[v]&&edge[i].w>eps)
        {
            DFS(v);
        }
    }
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=-1)
    {
        memset(du,0,sizeof(du));
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&e[i].u,&e[i].v);
            du[e[i].u]++;
            du[e[i].v]++;
        }
        if(m==0)
        {
            printf("1\n1\n");
            continue;
        }
        LL l=1.0/n,r=m,mid;
        while(r-l>1.0/n/n)//此题卡精度,精度太大太小都错
        {
            mid=(l+r)/2;
            LL cut=creat(n,m,mid);
            LL hg=(m*n-cut);
            if(hg>eps)//不能直接大于0,否则g值不一定刚好是突变的g点
            {
                l=mid;
            }
            else
            {
                r=mid;
            }
        }
        //printf("%lf\n",r);
        creat(n,m,l);
        memset(use,0,sizeof(use));
        cnt=0;
        DFS(0);
        printf("%d\n",cnt-1);
        for(int i=1;i<=n;i++)
        {
            if(use[i])
                printf("%d\n",i);
        }
    }
    return 0;
}

  




posted @ 2015-04-14 17:45  一样菜  阅读(829)  评论(0编辑  收藏  举报