思路:

这个还是看的胡伯涛的论文《最小割在信息学竞赛中的应用》。是将最大密度子图问题转化为了01分数规划和最小割问题。

直接上代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#define Maxn 6010
#define Maxm 200000
#define LL double
#define inf 100000000
#define Abs(a) (a)>0?(a):(-a)
using namespace std;
struct Edge{
int from,to,next;
LL val;
}edge[Maxm];
const double eps=1e-;
LL value[Maxn];
int head[Maxn],work[Maxn],dis[Maxn],q[Maxn],e,vi[Maxn];
inline void addedge(int from,int to,LL val)//有向边
{
edge[e].from=from;
edge[e].to=to;
edge[e].val=val;
edge[e].next=head[from];
head[from]=e++;
edge[e].from=to;
edge[e].to=from;
edge[e].val=;
edge[e].next=head[to];
head[to]=e++;
}
inline double min(double a,double b)
{
return a>b?b:a;
}
void init()
{
e=;
memset(head,-,sizeof(head));
}
void add(int u,int v,LL c)
{
edge[e].to=v;edge[e].val=c;edge[e].next=head[u];head[u]=e++;
edge[e].to=u;edge[e].val=;edge[e].next=head[v];head[v]=e++;
}
int bfs(int S,int T)
{
int rear=;
memset(dis,-,sizeof(dis));
dis[S]=;q[rear++]=S;
for(int i=;i<rear;i++)
{
for(int j=head[q[i]];j!=-;j=edge[j].next)
{
if(edge[j].val>&&dis[edge[j].to]==-)
{
dis[edge[j].to]=dis[q[i]]+;
q[rear++]=edge[j].to;
if(edge[j].to==T) return ;
}
}
}
return ;
}
LL dfs(int cur,LL a,int T)
{
if(cur==T) return a;
for(int i=work[cur];i!=-;i=edge[i].next)
{
if(edge[i].val>&&dis[edge[i].to]==dis[cur]+)
{
LL t=dfs(edge[i].to,min(a,edge[i].val),T);
if(t>)
{
edge[i].val-=t;
edge[i^].val+=t;
return t;
}
}
}
return ;
}
LL Dinic(int S,int T)
{
LL ans=;
while(bfs(S,T))
{
memcpy(work,head,sizeof(head));
LL t=dfs(S,inf,T);
while(t>)
{
ans+=t;
t=dfs(S,inf,T);
}
}
return ans;
} int main()
{
int n,m,i,j,a[Maxn],b[Maxn];
int degree[Maxn];
memset(degree,,sizeof(degree));
while(scanf("%d%d",&n,&m)!=EOF)
{
if(m==)
{
printf("1\n1\n");
return ;
}
init();
for(i=;i<=m;i++)
{
scanf("%d%d",a+i,b+i);
degree[a[i]]++;
degree[b[i]]++;
}
double l=,r=m,mid;
double eps2=1.0/n/n;
while(r-l>eps2)
{
mid=(l+r)/;
init();
for(i=;i<=m;i++)
{
add(a[i],b[i],);
add(b[i],a[i],);
}
for(i=;i<=n;i++)
{
add(,i,m);
add(i,n+,m * 1.0 + * mid - degree[i] * 1.0); }
double tt=Dinic(,n+);
double temp=(m*n*1.0-tt)/2.0;
//cout<<tt<<endl;
if(temp>eps)
l=mid;
else
r=mid;
}
init();
for(i=;i<=m;i++)
{
add(a[i],b[i],);
add(b[i],a[i],);
}
for(i=;i<=n;i++)
{
add(,i,m);
add(i,n+,m*1.0+*l-degree[i]*1.0);
//cout<<m<<" "<<mid<<" "<<degree[i]<<" "<<m+2*mid-degree[i]<<endl;
}
//for(i=0;i<e;i++)
//cout<<edge[i].to<<" "<<edge[i].next<<" "<<edge[i].val<<endl;
Dinic(,n+);
vector<int> ans;
memset(vi,,sizeof(vi));
for(i=;i<=n;i++)
if(dis[i]>=)
ans.push_back(i);
int num=ans.size();
printf("%d\n",num);
for(i=;i<num;i++)
printf("%d\n",ans[i]);
}
return ;
}

poj 3155 最大密度子图的更多相关文章

  1. POJ 3155 Hard Life 最大密度子图 最大权闭合图 网络流 二分

    http://poj.org/problem?id=3155 最大密度子图和最大权闭合图性质很相近(大概可以这么说吧),一个是取最多的边一个是取最多有正贡献的点,而且都是有选一种必须选另一种的限制,一 ...

  2. POJ 3155 Hard Life(最大密度子图+改进算法)

    Hard Life Time Limit: 8000MS   Memory Limit: 65536K Total Submissions: 9012   Accepted: 2614 Case Ti ...

  3. POJ 3155 Hard Life(最大密度子图)

    裸题.输入一个无向图,输出最大密度子图(输出子图结点数和升序编号). 看了<最小割模型在信息学竞赛中的应用——胡伯涛>的一部分,感觉01分数规划问题又是个大坑.暂时还看不懂. 参考http ...

  4. POJ 3155:Hard Life(最大密度子图)

    题目链接 题意 给出n个人,和m对有冲突的人.要裁掉一些人,使得冲突率最高,冲突率为存在的冲突数/人数. 思路 题意可以转化为,求出一些边,使得|E|/|V|最大,这种分数规划叫做最大密度子图. 学习 ...

  5. POJ 3155 Hard Life

    Hard Life Time Limit: 8000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID:  ...

  6. POJ3155 Hard Life [最大密度子图]

      题意:最大密度子图 #include<iostream> #include<cstdio> #include<cstring> #include<algo ...

  7. poj3155 最大密度子图

    求最大密度子图 记得在最后一次寻找的时候记得将进入的边放大那么一点点,这样有利于当每条边都满流的情况下会选择点 #include <iostream> #include <algor ...

  8. bzoj 1312 最大密度子图

    晕,m=0是要输出1(弄的我还找管理员要数据,但明显题意是叫我们输出0呀) 最大密度子图,把边转换成点,然后二分答案,跑最大权闭合子图判定是否可行. #include <cstdio> # ...

  9. 2017 计蒜之道 初赛 第三场 D. 腾讯狼人杀 (点边都带权的最大密度子图)

    点边都带权的最大密度子图,且会有必须选的点. 求\(\frac{\sum w_e}{k*(2n-k)}\)的最大值,其中k为子图点数 设\[h(g) = \sum w_e - g*(2nk-k^2)\ ...

随机推荐

  1. ocp 1Z0-051 141-175题解析

    141. View the Exhibitand examine the structure of CUSTOMERS and GRADES tables. You need to displayna ...

  2. codeforces 617BChocolate

    B. Chocolate time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  3. 利用hashtable和time函数加速Lisp程序

    程序功能是从一个英文文本中得到单词表,再得到押韵词表.即输出可能这样开始: a ameoeba alba samba marimba... 这样结束: ...megahertz gigahertz j ...

  4. HDU 5832 A water problem (水题,大数)

    题意:给定一个大数,问你取模73 和 137是不是都是0. 析:没什么可说的,先用char 存储下来,再一位一位的算就好了. 代码如下: #pragma comment(linker, "/ ...

  5. Unity3D之Legacy动画系统学习笔记

    Unity3D的Mecanim动画系统是非常强大的,而且作为Unity推荐的动画系统,其未来会完全代替老的一套动画系统,即Legacy动画系统.目前的情况是Mecanim与Legacy两套动画系统同时 ...

  6. How Much Work Does it Take to be a Successful Mathematician?

    http://mathoverflow.net/questions/9799/how-much-work-does-it-take-to-be-a-successful-mathematician# ...

  7. 标准C++ 字符串处理增强函数

    转自:http://dewei.iteye.com/blog/1566734 //标准C++ string 去除首尾空白字符 2012-8-12 By Dewei static inline void ...

  8. VMM服务模板(虚机、APP)部署排错

    I won't focus this blog on how to create a service template but more on how you can track the change ...

  9. 【转】larbin中的url去重算法

    1.bloom filter算法 传说中,larbin使用bloom filter算法来进行url去重.那我们就先来了解下bloom filter算法好了. [以下转自:http://hi.baidu ...

  10. C++ 预编译头文件

    1.解决什么问题? C++ 编译器是单独,分别编译的,每个cpp文件,进行预编译(也就是对#include,define 等进行文本替换),生成编译单元.编译单元是一个自包含文件,C++编译器对编译单 ...