网络流(最大密集度子图,分数规划):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 心烦意乱的时候调这道题真是...越调越气,就这样过了一晚上... 今天再认真看看,找出 ...
随机推荐
- SQL Server 事务、异常
转自: http://www.cnblogs.com/moss_tan_jun/archive/2011/11/26/2263988.html 事务 在数据库中有时候需要把多个步骤的指令当作一个整 ...
- 安卓数据存储(2):SharedPreferences
SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的就是一个key-value(键值对)在读取数据时可以通过这个键把相应的值取出 ...
- List指定字段赋特定值(非循环) asp.net
List<Cart> cartd=cartd.Where(p => (p.Id= "123").Length > -1).ToList(); 把Id的值都赋 ...
- Android studio混淆
看了一篇关于Android studio混淆的文章http://blog.csdn.net/qq_23547831/article/details/51581491,感觉有必要总结一个简单的混淆版本设 ...
- Mongodb 启动时 lock文件访问没有权限处理
mongodb 第二次启动时候异常信息: lock file: /data/db/mongod.lock errno:13 Permission denied Is a mongod instance ...
- SQL存储过程传入字段名查询.
根据字段名和对应的值查询. (正确代码):目前发现,需要"分组,排序"等才能解决,如"order by","group by"等. SQL代 ...
- base64加密解密文件
1 //字符串加密 -(void)demo1 { //普通的 8 bit二进制数据 NSString *str = @"hello world!"; //将字符串转换成二进制数据 ...
- 层模型--绝对定位(position:absolute)
如果想为元素设置层模型中的绝对定位,需要设置position:absolute(表示绝对定位),这条语句的作用将元素从文档流中拖出来,然后使用left.right.top.bottom属性相对于其最接 ...
- JavaScript中style.left与offsetLeft的区别
今天在制作焦点轮播图的时候,遇到一个问题,在使用style.left获取图片的位置时,怎么也获取不到.换用offsetLeft就能够成功获取到了.虽然实现了我想要的效果,但是还是不甘心啊,没有找到原因 ...
- vijos P1055奶牛浴场&& Winter Camp2002
这道题是我在寒假的模拟赛里碰到的,现在想起来仍觉得余味无穷.题目大意大致如下:给你一个矩形并在其中划出一个最大的子矩形,当然,在这个矩形里有些地方是取不到的,也就是说我们划的这个子矩形不能包含这些点( ...