网络流(最大密集度子图,分数规划):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
(1n100, 0m1000)
. 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, bin, 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
(1kn)
-- 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 心烦意乱的时候调这道题真是...越调越气,就这样过了一晚上... 今天再认真看看,找出 ...
随机推荐
- C#--Session用完如何清除
Session.Abandon();//清除全部Session//清除某个SessionSession["UserName"] = null;Session.Remove(&quo ...
- Win7设置承载网络 分类: 网络 2014-10-30 09:08 105人阅读 评论(0) 收藏
Win7设置承载网络 (1)最重要的第一步,要知道自己的网卡是否支持承载网络,如果不支持就悲剧地一票否决了,支持的话才能开始以后各步骤的设置. netsh wlan show drivers (2)设 ...
- C# Activator.CreateInstance()
C#在类工厂中动态创建类的实例,所使用的方法为: 1. Activator.CreateInstance (Type) 2. Activator.CreateInstance (Type, Objec ...
- iOS多线程的初步研究(八)-- dispatch队列
GCD编程的核心就是dispatch队列,dispatch block的执行最终都会放进某个队列中去进行,它类似NSOperationQueue但更复杂也更强大,并且可以嵌套使用.所以说,结合bloc ...
- SQL三大范式
第一范式:确保每列的原子性. 如果每列(或者每个属性)都是不可再分的最小数据单元(也称为最小的原子单元),则满足第一范式. 例如:顾客表(姓名.编号.地址.……)其中"地址"列还可 ...
- SGU 144.Meeting
题目: 两支地区ACM比赛的队伍决定为了国际决赛而在一起集训. 他们约定在某天的 X 时到 Y 时的某一时刻相会. 但由于他们很少按时到 (有的队伍比赛那天都会迟到), 他们没有设定一个确切的相遇时间 ...
- ios开发之ios9UIWebView不显示网页问题
错误描述: App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecu ...
- Entity Framework 级联删除
为一对主从表增加级联删除功能 protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.E ...
- EasyUI 1.3之前DataGrid中动态选中、获取Checkbox
这几天做项目,由于项目中用到的EasyUI版本过低,不能使用自带操作DataGrid中CheckBox的方法. 所以自己写了一个临时方案: 根据ID集合选中所属行的CheckBox: data={1, ...
- YII框架的部署 通过YII脚手架程序创建应用程序系统
1,把YII框架里面的framework复制粘贴到nginx目录下 2,创建一个商城系统: 1)修改环境变量 制定php.exe的目录 2)C:\Users\Administrator>cd C ...