HDU-3081-Marriage Match 2(最大流, 二分答案, 并查集)
链接:
https://vjudge.net/problem/HDU-3081
题意:
Presumably, you all have known the question of stable marriage match. A girl will choose a boy; it is similar as the game of playing house we used to play when we are kids. What a happy time as so many friends playing together. And it is normal that a fight or a quarrel breaks out, but we will still play together after that, because we are kids.
Now, there are 2n kids, n boys numbered from 1 to n, and n girls numbered from 1 to n. you know, ladies first. So, every girl can choose a boy first, with whom she has not quarreled, to make up a family. Besides, the girl X can also choose boy Z to be her boyfriend when her friend, girl Y has not quarreled with him. Furthermore, the friendship is mutual, which means a and c are friends provided that a and b are friends and b and c are friend.
Once every girl finds their boyfriends they will start a new round of this game—marriage match. At the end of each round, every girl will start to find a new boyfriend, who she has not chosen before. So the game goes on and on.
Now, here is the question for you, how many rounds can these 2n kids totally play this game?
思路:
二分枚举答案, 每次对图用枚举的答案建图,男女根据并查集确定能否配对连一条权值为1 的边.
跑最大流即可.
#代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 200+10;
const int INF = 1e9;
struct Edge
{
int from, to, cap;
};
vector<Edge> edges;
vector<int> G[MAXN];
int Dis[MAXN], Vis[MAXN];
int Fa[MAXN];
int Map[MAXN][MAXN];
int n, m, s, t, d;
void AddEdge(int from, int to, int cap)
{
edges.push_back(Edge{from, to, cap});
edges.push_back(Edge{to, from, 0});
G[from].push_back(edges.size()-2);
G[to].push_back(edges.size()-1);
}
bool Bfs()
{
//Bfs构造分层网络
memset(Dis, -1, sizeof(Dis));
queue<int> que;
que.push(s);
Dis[s] = 0;
while (!que.empty())
{
int u = que.front();
que.pop();
// cout << u << endl;
for (int i = 0;i < G[u].size();i++)
{
Edge & e = edges[G[u][i]];
if (e.cap > 0 && Dis[e.to] == -1)
{
que.push(e.to);
Dis[e.to] = Dis[u]+1;
}
}
}
return (Dis[t] != -1);
}
int Dfs(int u, int flow)
{
//flow 表示当前流量上限
if (u == t)
return flow;
int res = 0;
for (int i = 0;i < G[u].size();i++)
{
Edge & e = edges[G[u][i]];
if (e.cap > 0 && Dis[u]+1 == Dis[e.to])
{
int tmp = Dfs(e.to, min(flow, e.cap)); // 递归计算顶点 v
flow -= tmp;
e.cap -= tmp;
res += tmp;
edges[G[u][i]^1].cap += tmp;
if (flow == 0)
break;
}
}
if (res == 0)
Dis[u] = -1;
return res;
}
int MaxFlow()
{
int res = 0;
while (Bfs())
{
res += Dfs(s, INF);
}
return res;
}
int GetF(int x)
{
if (Fa[x] == x)
return x;
Fa[x] = GetF(Fa[x]);
return Fa[x];
}
bool Check(int mid)
{
for (int i = s;i <= t;i++)
G[i].clear();
edges.clear();
for (int i = 1;i <= n;i++)
{
AddEdge(s, i, mid);
AddEdge(n+i, t, mid);
}
for (int i = 1;i <= n;i++)
{
for (int j = 1;j <= n;j++)
{
if (Map[i][j])
AddEdge(i, n+j, 1);
}
}
int res = MaxFlow();
// cout << mid << ' ' << res << endl;
return res == mid*n;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while (T--)
{
memset(Map, 0, sizeof(Map));
cin >> n >> m >> d;
for (int i = 1;i <= n;i++)
Fa[i] = i;
s = 0, t = n*2+1;
int u, v;
for (int i = 1;i <= m;i++)
{
cin >> u >> v;
Map[u][v] = 1;
}
for (int i = 1;i <= d;i++)
{
cin >> u >> v;
int tu = GetF(u);
int tv = GetF(v);
if (tu != tv)
Fa[tv] = tu;
}
for (int i = 1;i <= n;i++)
{
for (int j = 1;j <= n;j++)
{
if (i == j || GetF(i) != GetF(j))
continue;
for (int k = 1;k <= n;k++)
{
if (Map[i][k])
Map[j][k] = 1;
if (Map[j][k])
Map[i][k] = 1;
}
}
}
int l = 0, r = n;
int res = 0;
// Check(2);
while (l <= r)
{
int mid = (l+r)/2;
if (Check(mid))
{
res = max(res, mid);
l = mid+1;
}
else
r = mid-1;
}
cout << res << endl;
}
return 0;
}
HDU-3081-Marriage Match 2(最大流, 二分答案, 并查集)的更多相关文章
- HDU 3081 Marriage Match II 最大流OR二分匹配
Marriage Match IIHDU - 3081 题目大意:每个女孩子可以和没有与她或者是她的朋友有过争吵的男孩子交男朋友,现在玩一个游戏,每一轮每个女孩子都要交一个新的男朋友,问最多可以玩多少 ...
- HDU 3081 Marriage Match II (网络流,最大流,二分,并查集)
HDU 3081 Marriage Match II (网络流,最大流,二分,并查集) Description Presumably, you all have known the question ...
- HDU 3081 Marriage Match II(二分法+最大流量)
HDU 3081 Marriage Match II pid=3081" target="_blank" style="">题目链接 题意:n个 ...
- HDU 3081 Marriage Match II (二分图,并查集)
HDU 3081 Marriage Match II (二分图,并查集) Description Presumably, you all have known the question of stab ...
- [HNOI2006]公路修建问题 (二分答案,并查集)
题目链接 Solution 二分答案+并查集. 由于考虑到是要求花费的最小值,直接考虑到二分. 然后对于每一个二分出来的答案,模拟 \(Kruskal\) 的过程再做一遍连边. 同时用并查集维护联通块 ...
- HDU 3081 Marriage Match II 二分 + 网络流
Marriage Match II 题意:有n个男生,n个女生,现在有 f 条男生女生是朋友的关系, 现在有 m 条女生女生是朋友的关系, 朋友的朋友是朋友,现在进行 k 轮游戏,每轮游戏都要男生和女 ...
- HDU 3081 Marriage Match II (二分+网络流+并查集)
注意 这题需要注意的有几点. 首先板子要快,尽量使用带当前弧优化的dinic,这样跑起来不会超时. 使用弧优化的时候,如果源点设置成0,记得将cur数组从0开始更新,因为有的板子并不是. 其次这题是多 ...
- hdu 5652 India and China Origins(二分+bfs || 并查集)BestCoder Round #77 (div.2)
题意: 给一个n*m的矩阵作为地图,0为通路,1为阻碍.只能向上下左右四个方向走.每一年会在一个通路上长出一个阻碍,求第几年最上面一行与最下面一行会被隔开. 输入: 首行一个整数t,表示共有t组数据. ...
- 洛谷P1991无线通讯网[kruskal | 二分答案 并查集]
题目描述 国防部计划用无线网络连接若干个边防哨所.2 种不同的通讯技术用来搭建无线网络: 每个边防哨所都要配备无线电收发器:有一些哨所还可以增配卫星电话. 任意两个配备了一条卫星电话线路的哨所(两边都 ...
随机推荐
- MySQL备份工具之mysqlhotcopy
mysqlhotcopy使用lock tables.flush tables和cp或scp来快速备份数据库.它是备份数据库或单个表最快的途径,完全属于物理备份,但只能用于备份MyISAM存储引擎和运行 ...
- Java学习之==>int和Integer的区别和联系
一.区别 1.类型 int是java中原始八种基本数据类型之一: Integer是一个类,包装整型提供了很多日常的操作: 2.存储位置和大小 int是由jvm底层提供,由Java虚拟机规范,int型数 ...
- java:多线程(代理模式,Thread中的方法,Timer,生产者和消费者)
*进程:一个正在运行的程序,进程是操作系统分配资源的基本单位,每个进行有独立的内存空间,进程之间切换开销较大. *线程:一个轻量级的进程,线程是任务调度的基本单位,一个进程可以有多个线程, * 系统没 ...
- win10更新导致chrome打开网页速度太慢
升级win10之后如果出现chrome内核的浏览器网页总是打不开 打开很慢 而ie和edge是可以正常访问的 用这个方法可以 我弄了几天终于 搞好了 我直接转载过来了 近期,工程师收到大量反馈360浏 ...
- 【电子电路技术】短波红外InGaAs探测器简析
核心提示: 红外线是波长介于微波与可见光之间的电磁波,波长在0.75-1000μm之间,其在军事.通讯.探测.医疗等方面有广泛的应用.目前对红外线的分类还没有统一的标准,各个专业根据应用的需要,有着自 ...
- Linux ulimit 命令 限制系统用户对 shell 资源的访问
ulimit命令用来限制系统用户对 shell 资源的访问,常见用法如下: [root@MongoDB ~]# ulimit -a // 查看当前所有的资源限制 [root@MongoDB ~]# u ...
- excel常用公式--数据清洗类
trim:去除单元格两端的空格. concat/&:连接单元格内的内容. mid: 提取字符串中间的字符串. left: 提取字符串左边的字符串. right: 提取字符串右边的字符串. ...
- 【五一qbxt】test2
又犯了一些迷之错误??要不然yy鼠标就是我的了 1.Superman: 小姐姐的题解:直接用set模拟即可emmmm 里面有很多指针啊,乱七八糟的,不会qwq,先看看我的大模拟吧: #include& ...
- BZOJ 1257 余数之和 题解
题面 这道题是一道整除分块的模板题: 首先,知道分块的人应该知道,n/i最多有2*sqrt(n)种数,但这和余数有什么关系呢? 注意,只要n/i的值和n/(i+d)的值一样,那么n%i到n%(i+d) ...
- 关于 Spring AOP (AspectJ) 你该知晓的一切 (转)
出处:关于 Spring AOP (AspectJ) 你该知晓的一切