网络流(最大密集度子图,分数规划):UvaLive 3709 Hard Life
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 harderst 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 . If we add person number 3 to the team then hardness factor decreases to
.
Input
The input will contain several test cases, each of them as described below. Consecutive test cases are separated by a single blank line.
The first line of the input contains two integer numbers n
and m
(1n
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
(1ai, bi
n, ai
bi)
on a line. The order of people in a pair is arbitrary and no
pair is listed twice.
Output
For each test case, the output must follow the description below.
The outputs of two consecutive cases will be separated by a blank line.
Write to the output an integer number k
(1k
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.
Note, that in the last example any team has hardness factor of zero,
and any non-empty list of people is a valid answer.
Sample Input
5 6
1 5
5 4
4 2
2 5
1 2
3 1 4 0
Sample Output
4
1
2
4
5 1
1
胡博涛论文有提到。
WA67发,都不敢刷Uva了。
原因是最后的答案不能用lam获得,因为lam不一定是最优解,而且还会得到错误答案。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int maxn = ;
const int maxm = ;
const double INF = 0x3fffffff;
const double eps = 1e-;
int n,m; struct Max_Flow{
int cnt,fir[maxn],fron[maxn];
int tot,to[maxm],nxt[maxm];
double cap[maxm];queue<int>q;
int dis[maxn],gap[maxn],path[maxn];
void Init(int tot_=){
memset(fir,,sizeof(fir));
memset(dis,,sizeof(dis));
memset(gap,,sizeof(gap));
cnt=;tot=tot_;
}
void add(int a,int b,double c){
nxt[++cnt]=fir[a];
fir[a]=cnt;
cap[cnt]=c;
to[cnt]=b;
} void addedge(int a,int b,double c){
add(a,b,c);
add(b,a,);
} bool BFS(int s,int t){
dis[t]=;q.push(t);
while(!q.empty()){
int x=q.front();q.pop();
for(int i=fir[x];i;i=nxt[i])
if(!dis[to[i]]){
dis[to[i]]=dis[x]+;
q.push(to[i]);
}
}
return dis[s];
} double Aug(int s,int t){
int p=t;double f=INF;
while(p!=s){
f=min(f,cap[path[p]]);
p=to[path[p]^];
}
p=t;
while(p!=s) {
cap[path[p]]-=f;
cap[path[p]^]+=f;
p=to[path[p]^];
}
return f;
} double ISAP(int s,int t){
if(!BFS(s,t));
for(int i=s;i<=t;i++)gap[dis[i]]+=;
for(int i=s;i<=t;i++)fron[i]=fir[i];
int p=s;double ret=;
while(dis[s]<=tot){
if(p==t){
ret+=Aug(s,t);
p=s;
}
int &ii=fron[p];
for(;ii;ii=nxt[ii])if(cap[ii])
if(dis[p]==dis[to[ii]]+)
break;
if(ii)
path[p=to[ii]]=ii;
else{
if(--gap[dis[p]]==)break;
int minn=tot+;
for(int i=fir[p];i;i=nxt[i])
if(cap[i]>eps)minn=min(minn,dis[to[i]]);
gap[dis[p]=minn+]+=;fron[p]=fir[p];
if(p!=s)p=to[path[p]^];
}
}
return ret;
}
}isap; int vis[maxn],ans;
void DFS(int x){
vis[x]=true;
if(x>=&&x<=n)ans+=;
for(int i=isap.fir[x];i;i=isap.nxt[i])
if(!vis[isap.to[i]]&&isap.cap[i]>eps)
DFS(isap.to[i]);
} int x1[maxm],y1[maxm];
void Build(double lam){
isap.Init(n+m+);
for(int i=;i<=m;i++){
int u=x1[i],v=y1[i];
isap.addedge(,n+i,1.0);
isap.addedge(n+i,u,INF);
isap.addedge(n+i,v,INF);
}
for(int i=;i<=n;i++)
isap.addedge(i,n+m+,lam);
} void Solve(){ int s=,t=n+m+;
double l=,r=m,lam;
while (r-l>=1.0/n/n){
lam=(l+r)/;Build(lam);
double ret=isap.ISAP(s,t);
if(1.0*m-ret<eps)r=lam;
else l=lam;
}
Build(l);isap.ISAP(s,t);
memset(vis,,sizeof(vis));
ans=;DFS(s);printf("%d\n",ans);
for (int i=;i<=n;i++)
if(vis[i])printf("%d\n", i);
} int main(){
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=;i<=m;i++)
scanf("%d%d",&x1[i],&y1[i]);
if(!m){
printf("1\n1\n");
continue;
}
Solve();
}
return ;
}
网络流(最大密集度子图,分数规划):UvaLive 3709 Hard Life的更多相关文章
- 【BZOJ2285】[SDOI2011]保密(分数规划,网络流)
[BZOJ2285][SDOI2011]保密(分数规划,网络流) 题面 BZOJ 洛谷 题解 首先先读懂题目到底在干什么. 发现要求的是一个比值的最小值,二分这个最小值\(k\),把边权转换成\(t- ...
- 【BZOJ3232】圈地游戏(分数规划,网络流)
[BZOJ3232]圈地游戏(分数规划,网络流) 题面 BZOJ 题解 很神仙的一道题. 首先看到最大化的比值很容易想到分数规划.现在考虑分数规划之后怎么计算贡献. 首先每条边的贡献就变成了\(mid ...
- 【XSY2718】gift 分数规划 网络流
题目描述 有\(n\)个物品,买第\(i\)个物品要花费\(a_i\)元.还有\(m\)对关系:同时买\(p_i,q_i\)两个物品会获得\(b_i\)点收益. 设收益为\(B\),花费为\(A\), ...
- 【BZOJ4819】新生舞会(分数规划,网络流)
[BZOJ4819]新生舞会(分数规划,网络流) 题面 BZOJ Description 学校组织了一次新生舞会,Cathy作为经验丰富的老学姐,负责为同学们安排舞伴.有n个男生和n个女生参加舞会 买 ...
- 【BZOJ3597】方伯伯运椰子(分数规划,网络流)
[BZOJ3597]方伯伯运椰子(分数规划,网络流) 题解 给定了一个满流的费用流模型 如果要修改一条边,那么就必须满足流量平衡 也就是会修改一条某两点之间的路径上的所有边 同时还有另外一条路径会进行 ...
- [SCOI2018]游泳池(计算几何+分数规划+最大权闭合子图)
题目链接 https://www.luogu.org/problemnew/show/U56187 注:题面参考了网上的其他博客,并非原题题面,因此数据范围可能有误.数据为原创数据. 题解 其实就是许 ...
- bzoj 3232 圈地游戏——0/1分数规划(或网络流)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3232 当然是0/1分数规划.但加的东西和减的东西不在一起,怎么办? 考虑把它们合在一起.因为 ...
- BZOJ2285 [SDOI2011]保密 【01分数规划 + 网络流】
题目 现在,保密成为一个很重要也很困难的问题.如果没有做好,后果是严重的.比如,有个人没有自己去修电脑,又没有拆硬盘,后来的事大家都知道了. 当然,对保密最需求的当然是军方,其次才是像那个人.为了应付 ...
- bzoj 3232 圈地游戏 —— 01分数规划+最小割建图(最大权闭合子图)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3232 心烦意乱的时候调这道题真是...越调越气,就这样过了一晚上... 今天再认真看看,找出 ...
随机推荐
- 第四篇:python基础之dict、set及字符
python基础之dict.set及字符 python基础之dict.set及字符串处理 本节内容 字典介绍及内置方法 集合介绍 字符串处理 1.字典介绍及内置方法 字典是python中唯一的映射 ...
- JS(一)
额, 写了一堆, 待机win8崩溃, 重启之后只剩光秃秃的界面, 以后再填吧, 下次一定要一口气把博客写完,中间不能放下干其他事. 1.对象,以及对象与方法联合: var bob = new Obje ...
- 微信分享 分享icon和分享标题的简单设置
前几天做的一个活动,用到微信分享功能,分享的icon.分享的标题和内容是自定义的.我上网查了一下,好多是注册微信公众号,使用微信api来实现的,注册微信号比较麻烦,最简单的方法就是 页面的title改 ...
- C# 如何创建接口以及使用接口的简单Demo(转载!)
//No:1 首先,我们要封装一个接口,接口中不要实现具体的方法(说白了这就是一个架子而已!) using System;using System.Collections.Generic;using ...
- Solaris用户管理(一):用户与组管理
Solaris用户管理(一):用户与组管理 2008-07-01 09:19 用户管理是系统管理的基础.Solaris中不但支持传统Unix所支持的用户和组的概念,还从Solaris 8开始引入了基 ...
- 服务端配置scan ip
节点1确认当前监听状态 SQL> show parameter listener; NAME TYPE VALUE ...
- 『重构--改善既有代码的设计』读书笔记----Introduce Foreign Method
当你无法获得一个类的源代码或者没有权限去修改这个类的时候,你对于这种为你服务的类,你可能会出现需要别的需求的时候,比如一个Date类,你需要能够让他本身直接返回出他的后一天的对象,但他没有,这个时候你 ...
- Android 学习手札(二) 活动(Activity)组件
1.建立和配置Activity 建立Android工程时已经自动生成了一个默认的Activity,同时也生成了很多与Activity相关的文件,例如,res目录中的XML及图像文件.AndroidMa ...
- date日期比较和格式化方法
时间的比较 var now = new Date();//现在时间 var setDate = new Date(2017,0,5,17,55,55);//设定的时间 laert(now < s ...
- Bootstrap_表单_图像
在Bootstrap框架中对于图像的样式风格提供以下几种风格: 1.img-responsive:响应式图片,主要针对于响应式设计2.img-rounded:圆角图片3.img-circle:圆形图片 ...