HDU 3277 Marriage Match III(二分+最大流)
HDU 3277 Marriage Match III
题意:n个女孩n个男孩,每一个女孩能够和一些男孩配对,此外还能够和k个随意的男孩配对。然后有些女孩是朋友,满足这个朋友圈里面的人。假设有一个能和某个男孩配对,其它就都能够。然后每轮要求每一个女孩匹配到一个男孩,且每轮匹配到的都不同。问最多能匹配几轮
思路,比HDU3081多了一个条件,此外能够和k个随意的男孩配对。转化为模型,就是多了一个结点,有两种两边的方式。一种连向能够配对的,一种连向不能配对的。此外还要保证流量算在一起,这要怎么搞呢。
事实上拆点就能够了,一个女孩拆成两个点。一个连能够配,一个连不能配,源点连向当中一点,容量为mid,然后两点之间,在连一条边。连接起来,这样就能保证总流量不会超过mid了,其它都和上一题差不不多。只是这题数据比上一题大,要注意一开是预处理关系的做法
代码:
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std; const int MAXNODE = 805;
const int MAXEDGE = 200005; typedef int Type;
const Type INF = 0x3f3f3f3f; 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 = 205; int t, n, m, k, f, g[N][N], parent[N];
int u[N * N], v[N * N]; int find(int x) {
return x == parent[x] ? x : parent[x] = find(parent[x]);
} bool judge(int mid) {
int s = 0, t = n * 3 + 1;
gao.init(3 * n + 2);
for (int i = 1; i <= n; i++) {
gao.add_Edge(s, i, mid);
gao.add_Edge(i, i + n, k);
gao.add_Edge(i + 2 * n, t, mid);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (g[i][j]) gao.add_Edge(i, j + 2 * n, 1);
else gao.add_Edge(i + n, j + 2 * n, 1);
}
}
return gao.Maxflow(s, t) == n * mid;
} int main() {
scanf("%d", &t);
while (t--) {
scanf("%d%d%d%d", &n, &m, &k, &f);
memset(g, 0, sizeof(g));
for (int i = 1; i <= n; i++) parent[i] = i;
for (int i = 0; i < m; i++)
scanf("%d%d", &u[i], &v[i]);
int a, b;
while (f--) {
scanf("%d%d", &a, &b);
int pa = find(a);
int pb = find(b);
if (pa != pb) parent[pa] = pb;
}
for (int i = 0; i < m; i++)
g[find(u[i])][v[i]] = 1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
g[i][j] |= g[find(i)][j];
int l = 1, r = n + 1;
while (l < r) {
int mid = (l + r) / 2;
if (judge(mid)) l = mid + 1;
else r = mid;
}
printf("%d\n", l - 1);
}
return 0;
}
HDU 3277 Marriage Match III(二分+最大流)的更多相关文章
- HDU 3277 Marriage Match III(并查集+二分答案+最大流SAP)拆点,经典
Marriage Match III Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDU 3277 Marriage Match III
Marriage Match III Time Limit: 4000ms Memory Limit: 32768KB This problem will be judged on HDU. Orig ...
- HDU 3081 Marriage Match II (网络流,最大流,二分,并查集)
HDU 3081 Marriage Match II (网络流,最大流,二分,并查集) Description Presumably, you all have known the question ...
- HDU 3081 Marriage Match II 二分 + 网络流
Marriage Match II 题意:有n个男生,n个女生,现在有 f 条男生女生是朋友的关系, 现在有 m 条女生女生是朋友的关系, 朋友的朋友是朋友,现在进行 k 轮游戏,每轮游戏都要男生和女 ...
- HDU 3081 Marriage Match II (二分+并查集+最大流)
题意:N个boy和N个girl,每个女孩可以和与自己交友集合中的男生配对子;如果两个女孩是朋友,则她们可以和对方交友集合中的男生配对子;如果女生a和女生b是朋友,b和c是朋友,则a和c也是朋友.每一轮 ...
- 【HDOJ】3277 Marriage Match III
Dinic不同实现的效率果然不同啊. /* 3277 */ #include <iostream> #include <string> #include <map> ...
- HDU 3416 Marriage Match IV (最短路径,网络流,最大流)
HDU 3416 Marriage Match IV (最短路径,网络流,最大流) Description Do not sincere non-interference. Like that sho ...
- hdu 3416 Marriage Match IV (最短路+最大流)
hdu 3416 Marriage Match IV Description Do not sincere non-interference. Like that show, now starvae ...
- HDU 3081 Marriage Match II(二分法+最大流量)
HDU 3081 Marriage Match II pid=3081" target="_blank" style="">题目链接 题意:n个 ...
随机推荐
- 日积月累:ScrollView嵌套ListView只显示一行
在开发的过程当中,由于手机屏幕的大小的限制,我们经常需要使用滑动的方式,来显示更多的内容.在最近的工作中,遇见一个需求,需要将ListView嵌套到ScrollView中显示.于是乎有了如下布局: & ...
- 算法-最长子序列和C/C++实现(三个复杂度)
最长子序列和的问题非常easy: 就是一个数组,求出当中当中连续的某一段和,而这一段和是全部的连续段和的最大的值.求出这个值. 先说复杂度最高的:O(n3) 直接上代码,非常easy的: // // ...
- ACM POJ 2192 Zipper
题目大意:输入字符串a,b,c 要求推断c是否有a,b中的个字符保持原有顺序组合而成. 算法思想: DP 用dp[i][j]表示a的前0~i-1共i个字符和b的前0~j-1共j个字符是否构成c[i+j ...
- 去掉Enter字符(\r)的几个方法
数据:test.txt: f1:f2:f3:# Shell: #!/bin/bash while read line do echo $line result1=$(echo $line|awk -F ...
- [LeetCode] Print All Combinations of a Number as a Sum of Candidate Numbers
题目连接:http://leetcode.com/2010/09/print-all-combinations-of-number-as-sum.html 题目分析: 由于这里说明了输入是升序的,当然 ...
- Linux IP代理筛选系统(shell+proxy)
代理的用途 其实,除了抓取国外网页需要用到IP代理外,还有很多场景会用到代理: 通过代理访问一些国外网站,绕过被某国防火墙过滤掉的网站 使用教育网的代理服务器,可以访问到大学或科研院所的内部网站资源 ...
- KMP poj
题目来自:http://www.cnblogs.com/wuyiqi/archive/2012/01/06/2315188.html KMP算法开始是判断字符串b是否是字符串a的子串,朴素的算法是枚举 ...
- boost计算随机数和计算crc32简单示例 - jwybobo2007的专栏 - 博客频道 - CSDN.NET
boost计算随机数和计算crc32简单示例 - jwybobo2007的专栏 - 博客频道 - CSDN.NET boost::crc_32_type crc32; crc32. ...
- 被忽视的META标签之特效
在web设计中使用js可以实现很多的页面特效,然而很多人却忽视了HTML标签中META标签的强大功效,其实meta标签也可以实现很多漂亮的页面过渡效果. META标签是HTML语言HEAD区的一个辅助 ...
- Android学习路线(二十四)ActionBar Fragment运用最佳实践
转载请注明出处:http://blog.csdn.net/sweetvvck/article/details/38645297 通过前面的几篇博客.大家看到了Google是怎样解释action bar ...