• POJ 3155 Hard Life(最大密度子图)


    POJ 3155 Hard Life

    题目链接

    最大密度子图模板题

    代码:

    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <algorithm>
    using namespace std;
    
    const int MAXNODE = 1105;
    const int MAXEDGE = 100005;
    
    typedef double Type;
    const Type INF = 1e15;
    
    struct Edge {
    	int u, v;
    	Type cap, flow;
    	Edge() {}
    	Edge(int u, int v, Type cap, Type flow) {
    		this->u = u;
    		this->v = v;
    		this->cap = cap;
    		this->flow = flow;
    	}
    };
    
    struct Dinic {
    	int n, m, s, t;
    	Edge edges[MAXEDGE];
    	int first[MAXNODE];
    	int next[MAXEDGE];
    	bool vis[MAXNODE];
    	Type d[MAXNODE];
    	int cur[MAXNODE];
    	vector<int> cut;
    
    	void init(int n) {
    		this->n = n;
    		memset(first, -1, sizeof(first));
    		m = 0;
    	}
    	void add_Edge(int u, int v, Type cap) {
    		edges[m] = Edge(u, v, cap, 0);
    		next[m] = first[u];
    		first[u] = m++;
    		edges[m] = Edge(v, u, 0, 0);
    		next[m] = first[v];
    		first[v] = m++;
    	}
    
    	bool bfs() {
    		memset(vis, false, sizeof(vis));
    		queue<int> Q;
    		Q.push(s);
    		d[s] = 0;
    		vis[s] = true;
    		while (!Q.empty()) {
    			int u = Q.front(); Q.pop();
    			for (int i = first[u]; i != -1; i = next[i]) {
    				Edge& e = edges[i];
    				if (!vis[e.v] && e.cap > e.flow) {
    					vis[e.v] = true;
    					d[e.v] = d[u] + 1;
    					Q.push(e.v);
    				}
    			}
    		}
    		return vis[t];
    	}
    
    	Type dfs(int u, Type a) {
    		if (u == t || a == 0) return a;
    		Type flow = 0, f;
    		for (int &i = cur[u]; i != -1; i = next[i]) {
    			Edge& e = edges[i];
    			if (d[u] + 1 == d[e.v] && (f = dfs(e.v, min(a, e.cap - e.flow))) > 0) {
    				e.flow += f;
    				edges[i^1].flow -= f;
    				flow += f;
    				a -= f;
    				if (a == 0) break;
    			}
    		}
    		return flow;
    	}
    
    	Type Maxflow(int s, int t) {
    		this->s = s; this->t = t;
    		Type flow = 0;
    		while (bfs()) {
    			for (int i = 0; i < n; i++)
    				cur[i] = first[i];
    			flow += dfs(s, INF);
    		}
    		return flow;
    	}
    
    	void MinCut() {
    		cut.clear();
    		for (int i = 0; i < m; i += 2) {
    			if (vis[edges[i].u] && !vis[edges[i].v])
    				cut.push_back(i);
    		}
    	}
    } gao;
    
    const int N = 1005;
    const double eps = 1e-8;
    
    int n, m, u[N], v[N], s, t, du[N], sum;
    
    double build(double g) {
    	gao.init(n + 2);
    	for (int i = 1; i <= n; i++) {
    		gao.add_Edge(s, i, 1.0 * m);
    		gao.add_Edge(i, t, m + 2 * g - du[i]);
    	}
    	for (int i = 0; i < m; i++) {
    		gao.add_Edge(u[i], v[i], 1.0);
    		gao.add_Edge(v[i], u[i], 1.0);
    	}
    	return n * m - gao.Maxflow(s, t);
    }
    
    void dfs(int u) {
    	gao.vis[u] = true;
    	if (u >= 1 && u <= n) sum++;
    	for (int i = gao.first[u]; i + 1; i = gao.next[i]) {
    		int v = gao.edges[i].v;
    		if (gao.edges[i].flow < gao.edges[i].cap && !gao.vis[v]) dfs(v);
    	}
    }
    
    int main() {
    	while (~scanf("%d%d", &n, &m)) {
    		if (m == 0) {
    			printf("1
    1
    ");
    			continue;
    		}
    		s = 0, t = n + 1;
    		memset(du, 0, sizeof(du));
    		for (int i = 0; i < m; i++) {
    			scanf("%d%d", &u[i], &v[i]);
    			du[u[i]]++; du[v[i]]++;
    		}
    		double l = 0, r = m;
    		while (r - l >= 1.0 / n / n) {
    			double mid = (l + r) / 2;
    			if (build(mid) < eps) r = mid;
    			else l = mid;
    		}
    		build(l);
    		gao.Maxflow(s, t);
    		memset(gao.vis, false, sizeof(gao.vis));
    		sum = 0;
    		dfs(s);
    		printf("%d
    ", sum);
    		for (int i = 1; i <= n; i++)
    			if (gao.vis[i]) printf("%d
    ", i);
    	}
    	return 0;
    }


  • 相关阅读:
    抓包工具之Charles
    docker之服务搭建
    Linux之查看开放端口
    Centos7下的开机自启动
    phpredis和predis
    Redis迁移工具之Redis-shake
    服务器间文件实时双向同步(rsync+inotify)
    Linux之轨迹记录(script)
    Redis集群之常用操作
    Redis集群搭建-多服务器
  • 原文地址:https://www.cnblogs.com/wzjhoutai/p/6766983.html
Copyright © 2020-2023  润新知