网络流(最大密集度子图,分数规划):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.

网络流(最大密集度子图,分数规划):UvaLive 3709 Hard Life

  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 网络流(最大密集度子图,分数规划):UvaLive 3709 Hard Life . If we add person number 3 to the team then hardness factor decreases to 网络流(最大密集度子图,分数规划):UvaLive 3709 Hard Life .

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

(1网络流(最大密集度子图,分数规划):UvaLive 3709 Hard Lifen网络流(最大密集度子图,分数规划):UvaLive 3709 Hard Life100, 0网络流(最大密集度子图,分数规划):UvaLive 3709 Hard Lifem网络流(最大密集度子图,分数规划):UvaLive 3709 Hard Life1000)
. 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网络流(最大密集度子图,分数规划):UvaLive 3709 Hard Lifeai, bi网络流(最大密集度子图,分数规划):UvaLive 3709 Hard Lifen, ai网络流(最大密集度子图,分数规划):UvaLive 3709 Hard Lifebi)
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

(1网络流(最大密集度子图,分数规划):UvaLive 3709 Hard Lifek网络流(最大密集度子图,分数规划):UvaLive 3709 Hard Lifen)
-- 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 ;
}
上一篇:哪些JavaScript IDE最好用?


下一篇:MYSQL 简单的循环存储过程