思路:
这个还是看的胡伯涛的论文《最小割在信息学竞赛中的应用》。是将最大密度子图问题转化为了01分数规划和最小割问题。
直接上代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#define Maxn 6010
#define Maxm 200000
#define LL double
#define inf 100000000
#define Abs(a) (a)>0?(a):(-a)
using namespace std;
struct Edge{
int from,to,next;
LL val;
}edge[Maxm];
const double eps=1e-;
LL value[Maxn];
int head[Maxn],work[Maxn],dis[Maxn],q[Maxn],e,vi[Maxn];
inline void addedge(int from,int to,LL val)//有向边
{
edge[e].from=from;
edge[e].to=to;
edge[e].val=val;
edge[e].next=head[from];
head[from]=e++;
edge[e].from=to;
edge[e].to=from;
edge[e].val=;
edge[e].next=head[to];
head[to]=e++;
}
inline double min(double a,double b)
{
return a>b?b:a;
}
void init()
{
e=;
memset(head,-,sizeof(head));
}
void add(int u,int v,LL c)
{
edge[e].to=v;edge[e].val=c;edge[e].next=head[u];head[u]=e++;
edge[e].to=u;edge[e].val=;edge[e].next=head[v];head[v]=e++;
}
int bfs(int S,int T)
{
int rear=;
memset(dis,-,sizeof(dis));
dis[S]=;q[rear++]=S;
for(int i=;i<rear;i++)
{
for(int j=head[q[i]];j!=-;j=edge[j].next)
{
if(edge[j].val>&&dis[edge[j].to]==-)
{
dis[edge[j].to]=dis[q[i]]+;
q[rear++]=edge[j].to;
if(edge[j].to==T) return ;
}
}
}
return ;
}
LL dfs(int cur,LL a,int T)
{
if(cur==T) return a;
for(int i=work[cur];i!=-;i=edge[i].next)
{
if(edge[i].val>&&dis[edge[i].to]==dis[cur]+)
{
LL t=dfs(edge[i].to,min(a,edge[i].val),T);
if(t>)
{
edge[i].val-=t;
edge[i^].val+=t;
return t;
}
}
}
return ;
}
LL Dinic(int S,int T)
{
LL ans=;
while(bfs(S,T))
{
memcpy(work,head,sizeof(head));
LL t=dfs(S,inf,T);
while(t>)
{
ans+=t;
t=dfs(S,inf,T);
}
}
return ans;
} int main()
{
int n,m,i,j,a[Maxn],b[Maxn];
int degree[Maxn];
memset(degree,,sizeof(degree));
while(scanf("%d%d",&n,&m)!=EOF)
{
if(m==)
{
printf("1\n1\n");
return ;
}
init();
for(i=;i<=m;i++)
{
scanf("%d%d",a+i,b+i);
degree[a[i]]++;
degree[b[i]]++;
}
double l=,r=m,mid;
double eps2=1.0/n/n;
while(r-l>eps2)
{
mid=(l+r)/;
init();
for(i=;i<=m;i++)
{
add(a[i],b[i],);
add(b[i],a[i],);
}
for(i=;i<=n;i++)
{
add(,i,m);
add(i,n+,m * 1.0 + * mid - degree[i] * 1.0); }
double tt=Dinic(,n+);
double temp=(m*n*1.0-tt)/2.0;
//cout<<tt<<endl;
if(temp>eps)
l=mid;
else
r=mid;
}
init();
for(i=;i<=m;i++)
{
add(a[i],b[i],);
add(b[i],a[i],);
}
for(i=;i<=n;i++)
{
add(,i,m);
add(i,n+,m*1.0+*l-degree[i]*1.0);
//cout<<m<<" "<<mid<<" "<<degree[i]<<" "<<m+2*mid-degree[i]<<endl;
}
//for(i=0;i<e;i++)
//cout<<edge[i].to<<" "<<edge[i].next<<" "<<edge[i].val<<endl;
Dinic(,n+);
vector<int> ans;
memset(vi,,sizeof(vi));
for(i=;i<=n;i++)
if(dis[i]>=)
ans.push_back(i);
int num=ans.size();
printf("%d\n",num);
for(i=;i<num;i++)
printf("%d\n",ans[i]);
}
return ;
}