UVA1389 Hard Life
UVa
Luogu
分析
这个东西应该叫做最大密度子图。我们要让 $\frac{\sum E}{\sum V}$ 最大,显然是分数规划。
二分答案 $mid$,那么
$$ \frac{\sum E}{\sum V}>mid\Longrightarrow\sum E-mid\sum V>0 $$
问题变为,选一个点的代价是 $mid$,选一条边的收益是 $1$,选一条边必须选它的两个顶点,求最大收益。
显然是一个最大权闭合子图问题,那么最大收益即为边数减最小割。
这样子就能求出最大值了,然而题目要方案,直接找到所有与源点相连的点输出就好了。
代码
// ===================================
// author: M_sea
// website: http://m-sea-blog.com/
// ===================================
#include <bits/stdc++.h>
#define re register
using namespace std;
inline int read() {
int X=0,w=1; char c=getchar();
while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); }
while (c>='0'&&c<='9') X=X*10+c-'0',c=getchar();
return X*w;
}
const int N=2000+10,M=10000+10;
const double eps=1e-9;
const int inf=0x3f3f3f3f;
int n,m,s,t,x[M],y[M];
vector<int> ans;
struct edge { int v; double w; int nxt; } e[M<<1];
int head[N],cnt;
inline void addEdge(int u,int v,double w) {
e[++cnt]=(edge){v,w,head[u]},head[u]=cnt;
e[++cnt]=(edge){u,0,head[v]},head[v]=cnt;
}
int lv[N];
inline int bfs() {
memset(lv,0,sizeof(lv)),lv[s]=1;
queue<int> Q; Q.push(s);
while (!Q.empty()) { int u=Q.front(); Q.pop();
for (re int i=head[u];i;i=e[i].nxt) {
int v=e[i].v; double w=e[i].w;
if (w>eps&&!lv[v]) lv[v]=lv[u]+1,Q.push(v);
}
}
return lv[t]!=0;
}
inline double dfs(int u,double cpflow) {
if (u==t||cpflow<=eps) return cpflow;
double addflow=0;
for (re int i=head[u];i;i=e[i].nxt) {
int v=e[i].v; double w=e[i].w;
if (w>eps&&lv[v]==lv[u]+1) {
double tmpadd=dfs(v,min(cpflow,w));
e[i].w-=tmpadd,e[i^1].w+=tmpadd;
addflow+=tmpadd,cpflow-=tmpadd;
if (cpflow<=eps) break;
}
}
if (addflow<=eps) lv[u]=0;
return addflow;
}
inline double dinic() { double res=0;
for (;bfs();res+=dfs(s,inf));
return res;
}
inline int check(double mid) {
cnt=1,memset(head,0,sizeof(head));
s=0,t=n+m+1;
for (re int i=1;i<=m;++i) addEdge(s,i,1);
for (re int i=1;i<=n;++i) addEdge(i+m,t,mid);
for (re int i=1;i<=m;++i) addEdge(i,x[i]+m,inf);
for (re int i=1;i<=m;++i) addEdge(i,y[i]+m,inf);
return m-dinic()>eps;
}
int main() { int flag=0;
while (scanf("%d%d",&n,&m)==2) {
if (flag) puts(""); flag=1;
if (m==0) { puts("1\n1"); continue; }
for (re int i=1;i<=m;++i) x[i]=read(),y[i]=read();
double L=0,R=m;
while (R-L>1e-6) {
double mid=(L+R)/2;
if (check(mid)) L=mid;
else R=mid;
}
ans.clear(); check(L);
for (re int i=1;i<=m;++i)
if (lv[i]) ans.push_back(x[i]),ans.push_back(y[i]);
sort(ans.begin(),ans.end());
ans.erase(unique(ans.begin(),ans.end()),ans.end());
printf("%lu\n",ans.size());
for (re int i:ans) printf("%d\n",i);
}
return 0;
}
评论
发表评论