题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3081

推荐博客:https://www.cnblogs.com/liuxin13/p/4728131.html

这个题目没什么思路,完全不知道怎么写,后来看了题解,说是网络流+二分+并查集,然后更加懵了。

然后就z继续看了详细解答,才知道怎么去写。

emmm...这个题目他是让你求不同男孩女孩全部都成功配对最多会有多少轮,每一轮的女孩配对的男生都不一样。

还有一个让题目复杂一点的就是女孩子有朋友,在一个女孩圈里,她朋友没吵过架的男生在下一轮里可以成为她的男朋友。

先不考虑女孩子有女孩圈里,也不求最多有多少轮,就是男生女生要全部成功配对,那就是一个网络流的裸题。

但是呢,现在要求最多有多少轮(不考虑女孩圈),那其实也很难想到二分。。。

不过有人提示你用二分了,这个就觉得二分可以做,而且感觉也特别对,那就这样写吧,至于解释就是一般求最值都要想到用二分,

而且这个还是可以找到单调性,这个单调性就是如果最大流==n*x(x是被二分枚举的答案)那就说明这个可以是答案,当然也可以更大一点,所以就更新这个l=mid+1

不然就是这个mid大了,所以这个r就更新为mid-1。

最后就是考虑有女孩圈了,既然有女孩圈,是一个圈,那就容易想到并查集,所以这个题目的第三个算法就是并查集,

但是这个并查集怎么用,我开始也不知道,后来看了一点点别人写的代码才理解的,这个就是把一个把一个圈子的女孩的所有男朋友都连到根节点上去。

这个样子,以后每一个女孩找男朋友都可以通过根节点去连线(建图)

大致思路就是这个样子的,接下来敲代码了。

非常郁闷,又找了一个晚上的bug,唉,好伤心,好想哭。。。。

最后还是刷牙的时候感觉自己的问题,我这么写很可能会同一边输入很多次,这个需要判断一下,哭了。。。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5+; struct edge
{
int u, v, c, f;
edge(int u, int v, int c, int f) :u(u), v(v), c(c), f(f) {}
};
vector<edge>e;
vector<int>G[maxn];
int level[maxn];//BFS分层,表示每个点的层数
int iter[maxn];//当前弧优化
int m;
void init()
{
for (int i = ; i <= maxn; i++)G[i].clear();
e.clear();
}
void addedge(int u, int v, int c)
{
e.push_back(edge(u, v, c, ));
e.push_back(edge(v, u, , ));
m = e.size();
G[u].push_back(m - );
G[v].push_back(m - );
}
void BFS(int s)//预处理出level数组
//直接BFS到每个点
{
memset(level, -, sizeof(level));
queue<int>q;
level[s] = ;
q.push(s);
while (!q.empty())
{
int u = q.front();
q.pop();
for (int v = ; v < G[u].size(); v++)
{
edge& now = e[G[u][v]];
if (now.c > now.f && level[now.v] < )
{
level[now.v] = level[u] + ;
q.push(now.v);
}
}
}
}
int dfs(int u, int t, int f)//DFS寻找增广路
{
if (u == t)return f;//已经到达源点,返回流量f
for (int &v = iter[u]; v < G[u].size(); v++)
//这里用iter数组表示每个点目前的弧,这是为了防止在一次寻找增广路的时候,对一些边多次遍历
//在每次找增广路的时候,数组要清空
{
edge &now = e[G[u][v]];
if (now.c - now.f > && level[u] < level[now.v])
//now.c - now.f > 0表示这条路还未满
//level[u] < level[now.v]表示这条路是最短路,一定到达下一层,这就是Dinic算法的思想
{
int d = dfs(now.v, t, min(f, now.c - now.f));
if (d > )
{
now.f += d;//正向边流量加d
e[G[u][v] ^ ].f -= d;
//反向边减d,此处在存储边的时候两条反向边可以通过^操作直接找到
return d;
}
}
}
return ;
}
int Maxflow(int s, int t)
{
int flow = ;
for (;;)
{
BFS(s);
if (level[t] < )return flow;//残余网络中到达不了t,增广路不存在
memset(iter, , sizeof(iter));//清空当前弧数组
int f;//记录增广路的可增加的流量
while ((f = dfs(s, t, INF)) > )
{
flow += f;
}
}
return flow;
} int f[maxn], boy[maxn], girl[maxn];
vector<int>love[maxn]; int findx(int x)
{
return f[x] == x ? x : f[x] = findx(f[x]);
} void unite(int x,int y)
{
x = findx(x);
y = findx(y);
if (x == y) return;
f[x] = y;
}
int n;
bool vis[][];
void buildgrath(int flow,int s,int t)
{
init();
memset(vis, , sizeof(vis));
for(int i=;i<=n;i++)
{
if(!vis[s][i]) addedge(s, i, flow);
if(!vis[i+n][t]) addedge(i + n, t, flow); int u = findx(i);
int len = love[u].size(); for(int j=;j<len;j++)
{
if (vis[i][love[u][j]]) continue;
addedge(i, love[u][j], );
vis[i][love[u][j]] = ;
}
}
} int main()
{
int w;
scanf("%d", &w);
while(w--)
{
int m, fx;
scanf("%d%d%d", &n, &m, &fx);
for (int i = ; i <= *n+; i++)
{
f[i] = i;
love[i].clear();
}
for(int i=;i<=m;i++)
{
scanf("%d%d", &girl[i], &boy[i]);
}
for(int i=;i<=fx;i++)
{
int x, y;
scanf("%d%d", &x, &y);
unite(x, y);
}
int s = , t = n + n + ;
int l = , r = n;
for(int i=;i<=m;i++)
{
int u = findx(girl[i]);
love[u].push_back(boy[i] + n);
}
int ans = ;
while(l<=r)
{
int mid = (l + r) >> ;
buildgrath(mid, s, t);
int ex = Maxflow(s, t);
if(ex==n*mid)
{
l = mid + ;
ans = mid;
}
else r = mid - ;
}
printf("%d\n", ans);
}
return ;
}

N - Marriage Match II 网络流的更多相关文章

  1. HDU 3081 Marriage Match II (网络流,最大流,二分,并查集)

    HDU 3081 Marriage Match II (网络流,最大流,二分,并查集) Description Presumably, you all have known the question ...

  2. HDU 3081 Marriage Match II 二分 + 网络流

    Marriage Match II 题意:有n个男生,n个女生,现在有 f 条男生女生是朋友的关系, 现在有 m 条女生女生是朋友的关系, 朋友的朋友是朋友,现在进行 k 轮游戏,每轮游戏都要男生和女 ...

  3. HDU 3081 Marriage Match II (二分图,并查集)

    HDU 3081 Marriage Match II (二分图,并查集) Description Presumably, you all have known the question of stab ...

  4. HDU3081:Marriage Match II (Floyd/并查集+二分图匹配/最大流(+二分))

    Marriage Match II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  5. hdu3081 Marriage Match II(最大流)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Marriage Match II Time Limit: 2000/1000 M ...

  6. HDU 3081 Marriage Match II(二分法+最大流量)

    HDU 3081 Marriage Match II pid=3081" target="_blank" style="">题目链接 题意:n个 ...

  7. Marriage Match II(二分+并查集+最大流,好题)

    Marriage Match II http://acm.hdu.edu.cn/showproblem.php?pid=3081 Time Limit: 2000/1000 MS (Java/Othe ...

  8. HDU3081 Marriage Match II —— 传递闭包 + 二分图最大匹配 or 传递闭包 + 二分 + 最大流

    题目链接:https://vjudge.net/problem/HDU-3081 Marriage Match II Time Limit: 2000/1000 MS (Java/Others)    ...

  9. 【HDU3081】Marriage Match II (二分+最大流)

    Description Presumably, you all have known the question of stable marriage match. A girl will choose ...

随机推荐

  1. 微服务框架-Spring Cloud

    Spring Cloud入门 微服务与微服务架构 微服务架构是一种新型的系统架构.其设计思路是,将单体架构系统拆分为多个可以相互调用.配合的独立运行的小程序.这每个小程序对整体系统所提供的功能就称为微 ...

  2. Html5移动端弹幕动画实现示例代码

    已知20条内容要有弹幕效果,分成三层,速度随机. 先来看看效果: 所以这里不考虑填写生成的.只是一个展现的效果. 如果要看填写生成的,请不要浪费Time 思路 把单个内容编辑好,计算自身宽度,确定初始 ...

  3. Some Modern Softwares' drawbacks: User experience 12/29/2015

    In the nowadays, there are many APP in the PC or smart Phone. Some of them can't meet the customers' ...

  4. [apue] getopt 可能重排参数

    看第21章时,介绍到了解析命令行的神器 getopt,了解了 linux 下处理通用命令行的方法. 命令行可分为参数与选项,其中不带 - 或 -- 前缀的为参数,对一个命令而言数量是固定的,多个参数之 ...

  5. 申请elasticsearch中x-pack插件许可证及授权

    前提:         ES主机中elasticsearch x-pack插件许可证申请使用期限为1年,到期后x-pack插件将不再可用,重启elasticsearch服务后日志会提示一下警告,如图所 ...

  6. 百度智能云虚拟主机 Typecho 分类功能失效 | 开启伪静态地址

    出现的问题 $this->is() 方法失效,无法正确判断 archive.category.tags 页面类型. 点击分类页面.归档页面时,虽然 URL 是正确的,但网页内容却是 index. ...

  7. 反向icmp_shell

    前言 很老的一个技术了,学习下. ICMP协议工作方式简介 Internet控制报文协议(ICMP)是Internet协议族中一个.它被用于包括路由器在内的网络设备中,用来发送错误报文和操作信息,表示 ...

  8. Springboot:thymeleaf模板(八)

    存放位置:resources\templates 访问方式:通过Controller请求访问,不可直接访问(相当于web项目的WEB-INF目录) 环境依赖: <!--thymeleaf模板支持 ...

  9. 图解AVL树

    1:AVL树简介 二叉搜索树在一般情况下其搜索的时间复杂度为O(logn),但某些特殊情况下会退化为链表,导致树的高度变大且搜索的时间复杂度变为O(n),发挥不出树这种数据结构的优势,因此平衡二叉树便 ...

  10. 五分钟学会Python装饰器,看完面试不再慌

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是Python专题的第12篇文章,我们来看看Python装饰器. 一段囧事 差不多五年前面试的时候,我就领教过它的重要性.那时候我Pyt ...