Hard Life UVA - 1389(最大密度子图 输出点集)
Posted wtsruvf
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hard Life UVA - 1389(最大密度子图 输出点集)相关的知识,希望对你有一定的参考价值。
题意:
rt
解析:
我用的第二种方法。。。
s向所有的边连权值为1的边
所有的点向t连权值为mid的边
如果存在u - > v 则边向u和v分别连一条权值为INF的边
二分mid
用dfs从s 顺着边走标记点
然后输出1 - n种被标记的点即可
#include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <cctype> #include <set> #include <vector> #include <stack> #include <queue> #include <algorithm> #include <cmath> #include <bitset> #define rap(i, a, n) for(int i=a; i<=n; i++) #define rep(i, a, n) for(int i=a; i<n; i++) #define lap(i, a, n) for(int i=n; i>=a; i--) #define lep(i, a, n) for(int i=n; i>a; i--) #define rd(a) scanf("%d", &a) #define rlld(a) scanf("%lld", &a) #define rc(a) scanf("%c", &a) #define rs(a) scanf("%s", a) #define rb(a) scanf("%lf", &a) #define rf(a) scanf("%f", &a) #define pd(a) printf("%d ", a) #define plld(a) printf("%lld ", a) #define pc(a) printf("%c ", a) #define ps(a) printf("%s ", a) #define MOD 2018 #define eps 1e-7 #define LL long long #define ULL unsigned long long #define Pair pair<int, int> #define mem(a, b) memset(a, b, sizeof(a)) #define _ ios_base::sync_with_stdio(0),cin.tie(0) //freopen("1.txt", "r", stdin); using namespace std; const int maxn = 10010, INF = 0x7fffffff; int n, m, s, t; vector<int> f, g; struct edge { int u, v; }Edge[maxn]; int head[maxn], cur[maxn], vis[maxn], d[maxn], cnt, nex[maxn << 1]; int ans; struct node { int u, v; double c; }Node[maxn << 1]; void add_(int u, int v, double c) { Node[cnt].u = u; Node[cnt].v = v; Node[cnt].c = c; nex[cnt] = head[u]; head[u] = cnt++; } void add(int u, int v, double c) { add_(u, v, c); add_(v, u, 0); } bool bfs() { queue<int> Q; mem(d, 0); Q.push(s); d[s] = 1; while(!Q.empty()) { int u = Q.front(); Q.pop(); for(int i = head[u]; i != -1; i = nex[i]) { int v = Node[i].v; if(!d[v] && Node[i].c > 0) { d[v] = d[u] + 1; Q.push(v); if(v == t) return 1; } } } return d[t] != 0; } double dfs(int u, double cap) { double ret = 0; if(u == t || abs(cap) < eps) return cap; for(int &i = cur[u]; i != -1; i = nex[i]) { int v = Node[i].v; if(d[v] == d[u] + 1 && Node[i].c > 0) { double V = dfs(v, min(cap, Node[i].c)); Node[i].c -= V; Node[i ^ 1].c += V; ret += V; cap -= V; if(cap == 0) break; } } if(cap > 0) d[u] = -1; return ret; } double Dinic() { double ans = 0; while(bfs()) { memcpy(cur, head, sizeof head); ans += dfs(s, INF); } return ans; } void build(double mid) { mem(head, -1), cnt = 0; rap(i, 1, m) { add(s, i, 1); add(i, m + Edge[i].u, INF); add(i, m + Edge[i].v, INF); } rap(i, 1, n) { f.push_back(cnt); // cout << mid << endl; add(m + i, t, mid); } } void f_dfs(int u) { for(int i = head[u]; i != -1; i = nex[i]) { int v = Node[i].v; if(!vis[v] && Node[i].c > eps) { vis[v] = 1, f_dfs(v); if(v - m >= 1 && v - m <= n) ans++; } } } int main() { while(scanf("%d%d", &n, &m) != EOF) { ans = 0; f.clear(); g.clear(); if(m == 0) { cout << 1 << endl; cout << 1 << endl; continue; } mem(head, -1); cnt = 0; int u, v, sum = m; f.clear(); s = 0, t = n + m + 1; rap(i, 1, m) { rd(Edge[i].u), rd(Edge[i].v); } double l = 1 / (double) n, r = m; while(r - l > (1.0 / n / n)) { // mem(head, -1), cnt = 0; // f.clear(); double mid = (r + l) / (double) 2; build(mid); if(sum - Dinic() > eps) l = mid; else r = mid; } f.clear(); build(l); Dinic(); mem(vis, 0); f_dfs(s); cout << ans << endl; for(int i = 1; i <= n; i++) if(vis[i + m]) cout << i << endl; } return 0; }
以上是关于Hard Life UVA - 1389(最大密度子图 输出点集)的主要内容,如果未能解决你的问题,请参考以下文章