博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3155 Hard Life(最小割-最大密集子图)
阅读量:6071 次
发布时间:2019-06-20

本文共 3063 字,大约阅读时间需要 10 分钟。

题目链接:http://poj.org/problem?id=3155

题意:给定一个n个点m条边的无向图。输出最大密集子图中的顶点个数和各个顶点编号。

思路:二分答案g,原图中的边(u,v)连边<u,v,1>和<v,u,1>。s和每个点连边<s,i,m>,每个点和汇点连边<i,t,m+2*g-d[i]>。d[i]为每个点的度。

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define max(x,y) ((x)>(y)?(x):(y))#define min(x,y) ((x)<(y)?(x):(y))#define abs(x) ((x)>=0?(x):-(x))#define i64 long long#define u32 unsigned int#define u64 unsigned long long#define clr(x,y) memset(x,y,sizeof(x))#define CLR(x) x.clear()#define ph(x) push(x)#define pb(x) push_back(x)#define Len(x) x.length()#define SZ(x) x.size()#define PI acos(-1.0)#define sqr(x) ((x)*(x))#define MP(x,y) make_pair(x,y)#define EPS 1e-9#define FOR0(i,x) for(i=0;i
=0;i--)#define FORL1(i,a) for(i=a;i>=1;i--)#define FORL(i,a,b)for(i=a;i>=b;i--)using namespace std;void RD(int &x){scanf("%d",&x);}void RD(u32 &x){scanf("%u",&x);}void RD(double &x){scanf("%lf",&x);}void RD(int &x,int &y){scanf("%d%d",&x,&y);}void RD(u32 &x,u32 &y){scanf("%u%u",&x,&y);}void RD(double &x,double &y){scanf("%lf%lf",&x,&y);}void RD(int &x,int &y,int &z){scanf("%d%d%d",&x,&y,&z);}void RD(int &x,int &y,int &z,int &t){scanf("%d%d%d%d",&x,&y,&z,&t);}void RD(u32 &x,u32 &y,u32 &z){scanf("%u%u%u",&x,&y,&z);}void RD(double &x,double &y,double &z){scanf("%lf%lf%lf",&x,&y,&z);}void RD(char &x){x=getchar();}void RD(char *s){scanf("%s",s);}void RD(string &s){cin>>s;}void PR(int x) {printf("%d\n",x);}void PR(int x,int y) {printf("%d %d\n",x,y);}void PR(i64 x) {printf("%lld\n",x);}void PR(u32 x) {printf("%u\n",x);}void PR(double x) {printf("%.5lf\n",x);}void PR(char x) {printf("%c\n",x);}void PR(char *x) {printf("%s\n",x);}void PR(string x) {cout<
<
edges[curedge[i]].cap) k=i,d=edges[curedge[i]].cap; for(i=s;i!=t;i=edges[curedge[i]].v) { x=curedge[i]; edges[x].cap-=d; edges[x].flow+=d; edges[x^1].cap+=d; edges[x^1].flow-=d; } maxflow+=d;u=k; } for(i=curedge[u];i!=-1;i=edges[i].next)if(edges[i].cap>0&&h[u]==h[edges[i].v]+1) break; if(i!=-1) { curedge[u]=i; pre[edges[i].v]=u; u=edges[i].v; } else { if(0==--num[h[u]]) break; curedge[u]=head[u]; for(x=n,i=head[u];i!=-1;i=edges[i].next) if(edges[i].cap>0) x=min(x,h[edges[i].v]); h[u]=x+1;num[h[u]]++; if(u!=s) u=pre[u]; } } return maxflow;}void DFS(int u){ int i; visit[u]=1; if(u!=s) ans[++ans[0]]=u; for(i=head[u];i!=-1;i=edges[i].next) { if(edges[i].cap>1e-8&&!visit[edges[i].v]) { DFS(edges[i].v); } }}double calMaxDensity(int s,int t){ double low,high,mid,flow; low=0.0;high=1.0*m; while(high-low>=1.0/n/n) { mid=(low+high)/2; init(mid); flow=Maxflow(s,t); flow=(1.0*m*n-flow)/2; if(flow>1e-8) low=mid; else high=mid; } init(low); Maxflow(s,t);clr(visit,0); ans[0]=0;DFS(s); sort(ans+1,ans+ans[0]+1); return low;}int main(){ while(scanf("%d%d",&n,&m)!=EOF) { int i; clr(d,0); FOR1(i,m) { RD(a[i],b[i]); d[a[i]]++; d[b[i]]++; } if(!m) { puts("1"); puts("1"); continue; } s=0;t=n+1; calMaxDensity(s,t); PR(ans[0]); FOR1(i,ans[0]) PR(ans[i]); } return 0;}

  

转载地址:http://uwigx.baihongyu.com/

你可能感兴趣的文章
AngularJS PhoneCat代码分析
查看>>
maven错误解决:编码GBK的不可映射字符
查看>>
2016/4/19 反射
查看>>
SharePoint Wiki发布页面的“保存冲突”
查看>>
oracle 10g 数据库与客户端冲突导致实例创建无监听问题
查看>>
Delphi中读取文本文件的方法(实例一)
查看>>
Linux常用命令
查看>>
Android开源代码解读の使用TelephonyManager获取移动网络信息
查看>>
想说一点东西。。。。
查看>>
css知多少(8)——float上篇
查看>>
NLB网路负载均衡管理器详解
查看>>
水平添加滚动条
查看>>
PHP中”单例模式“实例讲解
查看>>
VS2008查看dll导出函数
查看>>
VM EBS R12迁移,启动APTier . AutoConfig错误
查看>>
atitit.细节决定成败的适合情形与缺点
查看>>
iOS - Library 库
查看>>
MATLAB 读取DICOM格式文件
查看>>
spring事务管理(Transaction)
查看>>
django.contrib.auth登陆注销学习
查看>>