POJ 3155 Hard Life 最大密度子图

h(g) = |E'| - g|V'|.

E'和V'为生成子图的边集和点集。

二分g

原图无向边u - v 改成两条有向边u -> v 和 v -> u, 流量为1

源点s到每个点连一条流量为m的边。

每个点到汇点t连一条流量为 m + 2 * g - d[v]的边,d[v] 为度数

h(g) = (n * m - C) / 2

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <stack>
#include <cmath>
#include <map>
#include <vector>
#include <queue>
using namespace std;

#define mxn 120
#define mxe 10020
#define LL long long
#define inf 0x3f3f3f3f
#define vi vector<int>
#define PB push_back
#define MP make_pair
#define G(i, u) for(int i = fst[u]; ~i; i = nxt[i])
#define F(i, n) for(int i = 1; i <= n; ++i)

int fst[mxn], nxt[mxe], to[mxe];
double cap[mxe], flow[mxe];
int e;
int cur[mxn], d[mxn];


int n, m, A[mxe], B[mxe];
int ind[mxn];
int s, t;

int dcmp(double x) {
	if(fabs(x) < 1e-8) return 0;
	return x < 0? -1: 1;
}

void init() {
	memset(fst, -1, sizeof fst);
	e = 0;
}
void add(int u, int v, double c) {
	to[e] = v, nxt[e] = fst[u], cap[e] = c, flow[e] = 0, fst[u] = e++;
	to[e] = u, nxt[e] = fst[v], cap[e] = 0, flow[e] = 0, fst[v] = e++;
}

bool bfs() {
	memset(d, -1, sizeof d);
	queue<int> q;
	q.push(s), d[s] = 0;
	while(!q.empty()) {
		int u = q.front(); q.pop();
		G(i, u) {
			int v = to[i];
			if(d[v] == -1 && cap[i] > flow[i]) {
				d[v] = d[u] + 1;
				q.push(v);
			}
		}
	}
	return d[t] != -1;
}
double dfs(int u, double a) {
	if(u == t || dcmp(a) == 0) return a;
	double ret = 0, f;
	for(int &i = cur[u]; ~i; i = nxt[i]) {
		int v = to[i];
		if(d[v] == d[u] + 1 && dcmp(f = dfs(v, min(a, cap[i] - flow[i]))) > 0) {
			a -= f;
			ret += f;
			flow[i] += f;
			flow[i ^ 1] -= f;
			if(dcmp(a) == 0) break;
		}
	}
	return ret;
}

double gao() {
	double ret = 0;
	while(bfs()) {
		memcpy(cur, fst, sizeof fst);
		ret += dfs(s, 1e10);
	}
	return ret;
}
bool check(double g) {
	init();
	s = 0, t = n + 1;
	F(i, m) add(A[i], B[i], 1), add(B[i], A[i], 1);
	F(i, n) {
		add(s, i, m);
		add(i, t, m + 2 * g - ind[i]);
	}
	double C = gao();
	double ret = m * n - C;
	return dcmp(ret) > 0;
}
int main() {
	while(scanf("%d%d", &n, &m) != EOF) {
		memset(ind, 0, sizeof ind);
		F(i, m) {
			scanf("%d%d", &A[i], &B[i]);
			ind[A[i]]++, ++ind[B[i]];
		}
		if(m == 0) {
			puts("1\n1");
			continue;
		}
		double l = 0, r = m;
		while(r - l > 1e-6) {
			double mid = (l + r) / 2;
			if(check(mid))
				l = mid;
			else
				r = mid;
		}
		check(l);
		vector<int> ans;
		memset(d, -1, sizeof d);
		d[s] = 0;
		queue<int> q;
		q.push(s);
		while(!q.empty()) {
			int u = q.front(); q.pop();
			G(i, u) {
				int v = to[i];
			//	cout << cap[i] << ' ' << flow[i] << endl;
				if(d[v] == -1 && cap[i] > flow[i]) {
					d[v] = d[u] + 1;
					q.push(v);
					ans.push_back(v);
				}
			}
		}
		sort(ans.begin(), ans.end());
		printf("%d\n", ans.size());
		for(int i = 0; i < ans.size(); ++i)
			printf("%d\n", ans[i]);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值