HDU3081 Marriage Match II —— 传递闭包 + 二分图最大匹配 or 传递闭包 + 二分 + 最大流
题目链接:https://vjudge.net/problem/HDU-3081
Marriage Match II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4493 Accepted Submission(s): 1488
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?
Each test case starts with three integer n, m and f in a line (3<=n<=100,0<m<n*n,0<=f<n). n means there are 2*n children, n girls(number from 1 to n) and n boys(number from 1 to n).
Then m lines follow. Each line contains two numbers a and b, means girl a and boy b had never quarreled with each other.
Then f lines follow. Each line contains two numbers c and d, means girl c and girl d are good friends.
4 5 2
1 1
2 3
3 2
4 2
4 4
1 4
2 3
题意:
有n个女生和n个男生,有两种关系:1)女生a与男生b没有吵过架, 2)女生a与女生b是朋友。并且朋友关系是互相的且可以传递的(注意:只有女生才拥有朋友关系,男生没有这一个概念)。接着女生挑选男生作为男朋友,可以挑选和她没有吵过架的或者没有跟她朋友吵过架的男生。当所有女生都选完男朋友之后,一轮结束,再进行下一轮匹配挑选,规定选择过的伴侣不能再选。问:最多能进行多少轮匹配?
传递闭包 + 最大匹配:
1.根据题目意思,可以先用Flyod算法求出传递闭包。
2.由于在每一轮的匹配中,每个女生都要求能够找到男朋友。所以我们可以建立二分图,求出最大匹配是否完全匹配。
3.如果第2步中求出的是完全匹配,那么表示这一轮的匹配是有效的。由于题目要求新一轮的匹配中,不能选择“前任”,那么我们就把这一轮所有匹配对断开关系,即去掉两者之间的边。然后重复第2步。直到求出的不是完全匹配。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int MAXM = 1e5+;
const int MAXN = 2e2+; int uN, vN;
int maze[MAXN][MAXN], id[MAXN][MAXN], link[MAXN];
bool vis[MAXN]; bool dfs(int u)
{
for(int i = uN+; i<=uN+vN; i++)
if(maze[u][i] && !vis[i])
{
vis[i] = true;
if(link[i]==- || dfs(link[i]))
{
link[i] = u;
return true;
}
}
return false;
} bool hungary()
{
int ret = ;
memset(link, -, sizeof(link));
for(int i = ; i<=uN; i++)
{
memset(vis, , sizeof(vis));
if(!dfs(i)) return false;
}
return true;
} void flyod(int N)
{
for(int k = ; k<=N; k++)
for(int i = ; i<=N; i++)
for(int j = ; j<=N; j++)
maze[i][j] = maze[i][j]||(maze[i][k]&&maze[k][j]);
} int main()
{
int T, n, m, f;
scanf("%d", &T);
while(T--)
{
scanf("%d%d%d", &n,&m,&f);
uN = vN = n;
memset(maze, , sizeof(maze));
for(int i = ; i<=m; i++)
{
int u, v;
scanf("%d%d", &u, &v);
maze[u][uN+v] = ;
}
for(int i = ; i<=f; i++)
{
int u, v;
scanf("%d%d", &u, &v);
maze[u][v] = maze[v][u] = ;
} flyod(uN+vN);
int round = ;
while(hungary())
{
round++;
for(int i = uN+; i<=uN+vN; i++)
maze[link[i]][i] = ;
}
printf("%d\n", round);
}
}
传递闭包 + 二分 + 最大流:
1.同样,先用Flyod算法求出传递闭包。
2.二分答案,即轮数,然后检测是否合法,以此缩小范围,最终得出最优解。
3.怎么检测是否合法呢?答:利用最大流算法进行检测:
1) 建立超级源点,超级源点连向每个女生,边权为二分的轮数mid,表明这个女生要有过mid个男朋友;
2) 建立超级汇点,每个男生连向超级汇点,且边权也为mid,表明这个男生要有mid个女朋友;
3) 然后如果女生girl能挑选男生boy,那么就连一条边u-->v,边权为1,表明,该男女只能匹配一次。
4) 跑最大流算法,如果最大流maxflow==n*mid,则表明当前轮数mid是合法的,否则不合法。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int MAXM = 1e5+;
const int MAXN = 2e2+; int uN, vN;
int maze[MAXN][MAXN];
int gap[MAXN], dis[MAXN], pre[MAXN], cur[MAXN];
int flow[MAXN][MAXN]; int sap(int start, int end, int nodenum)
{
memset(cur, , sizeof(cur));
memset(dis, , sizeof(dis));
memset(gap, , sizeof(gap));
memset(flow, , sizeof(flow));
int u = pre[start] = start, maxflow = , aug = INF;
gap[] = nodenum; while(dis[start]<nodenum)
{
loop:
for(int v = cur[u]; v<nodenum; v++)
if(maze[u][v]-flow[u][v]> && dis[u] == dis[v]+)
{
aug = min(aug, maze[u][v]-flow[u][v]);
pre[v] = u;
u = cur[u] = v;
if(v==end)
{
maxflow += aug;
for(u = pre[u]; v!=start; v = u, u = pre[u])
{
flow[u][v] += aug;
flow[v][u] -= aug;
}
aug = INF;
}
goto loop;
} int mindis = nodenum-;
for(int v = ; v<nodenum; v++)
if(maze[u][v]-flow[u][v]> && mindis>dis[v])
{
cur[u] = v;
mindis = dis[v];
}
if((--gap[dis[u]])==) break;
gap[dis[u]=mindis+]++;
u = pre[u];
}
return maxflow;
} bool test(int mid)
{
int start = , end = uN+vN+;
for(int i = ; i<=uN; i++) maze[start][i] = mid;
for(int i = ; i<=vN; i++) maze[uN+i][end] = mid;
return sap(start, end, uN+vN+)==uN*mid;
} void flyod(int N)
{
for(int k = ; k<=N; k++)
for(int i = ; i<=N; i++)
for(int j = ; j<=N; j++)
maze[i][j] = maze[i][j]||(maze[i][k]&&maze[k][j]);
} int main()
{
int T, n, m, f;
scanf("%d", &T);
while(T--)
{
scanf("%d%d%d", &n,&m,&f);
uN = vN = n;
memset(maze, , sizeof(maze));
for(int i = ; i<=m; i++)
{
int u, v;
scanf("%d%d", &u, &v);
maze[u][uN+v] = ;
}
for(int i = ; i<=f; i++)
{
int u, v;
scanf("%d%d", &u, &v);
maze[u][v] = maze[v][u] = ;
} flyod(uN+vN);
int l = , r = uN;
while(l<=r)
{
int mid = (l+r)/;
if(test(mid))
l = mid + ;
else
r = mid - ;
}
printf("%d\n", r);
}
}
HDU3081 Marriage Match II —— 传递闭包 + 二分图最大匹配 or 传递闭包 + 二分 + 最大流的更多相关文章
- HDU 3081 Marriage Match II (二分图,并查集)
HDU 3081 Marriage Match II (二分图,并查集) Description Presumably, you all have known the question of stab ...
- HDU3081:Marriage Match II (Floyd/并查集+二分图匹配/最大流(+二分))
Marriage Match II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- hdu3081 Marriage Match II(最大流)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Marriage Match II Time Limit: 2000/1000 M ...
- hdu3081 Marriage Match II(二分+并查集+最大流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3081 题意: n个女生与n个男生配对,每个女生只能配对某些男生,有些女生相互是朋友,每个女生也可以跟她 ...
- HDU 3081:Marriage Match II(二分图匹配+并查集)
http://acm.hdu.edu.cn/showproblem.php?pid=3081 题意:有n个男生n个女生,他们只有没有争吵或者女生a与男生A没有争吵,且女生b与女生a是朋友,因此女生b也 ...
- hdu3081 Marriage Match II
新年第一篇,又花了一早上,真是蠢啊! 二分+网络流 之前对于讨论哪些人是朋友的时候复杂度过高 直接n3的暴力虽然看起来复杂度高,其实并不是每次都成立 #include<bits/stdc++.h ...
- 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个 ...
- Marriage Match II(二分+并查集+最大流,好题)
Marriage Match II http://acm.hdu.edu.cn/showproblem.php?pid=3081 Time Limit: 2000/1000 MS (Java/Othe ...
随机推荐
- Adobe Premiere Pro导入插件开发遇到的一个问题
最近在更新公司一款Premiere Pro CC导入插件的时候,遇到了一个神奇的现象.具体的现象是这样的:我们的插件需要将一些私有的文件数据放到插件中,比如说当前活动的文件名.当插件中收到不同的sel ...
- webstorm(三):webstorm的一些waring提示
一.Attribute key is not allowed here 二.Comparison this.loginType != 'username' may cause unexpected t ...
- 初学Java经典例子
我自己看的书的理解学习Java就是学习对象,就像谈恋爱,你对她多付出,收货就多(跑题了对象是啥??对象就是实体,通过类可以生成具有特定状态(或者叫属性)和行为或动作的实例,问题来了怎么创建? new一 ...
- Nginx官方配置文档收集
官方入口: https://www.nginx.com/resources/wiki/start/#pre-canned-configurations http://nginx.org/en/docs ...
- HttpServletRequest接口是怎么实现的
request只是规范中的一个名称而已.不是SUN提供的,这是由各个不同的Servlet提供商编写的,SUN只是规定这个类要实现HttpServletRequest接口,并且规定了各个方法的用途,但具 ...
- android EditText禁止复制粘贴完整代码
<!-- 定义基础布局LinearLayout --> <LinearLayout xmlns:android="http://schemas.android.com/ap ...
- gdb源码安装过程中的问题:no termcap library found
gdb使用源码安装的时候遇到错误:no termcap library found ./configure --> make --> make install 解决办法,下载termca ...
- flask如何设置模板语言Jinjia?如何查看路由视图函数映射?
首先flask的模板和静态文件命名必须是确定的templates和static pycharm的模板语言设置
- BUPT复试专题—中位数(2014-2)
题目描述 给定一个长度为N的非降数列,求数列的中位数.中位数:当数列的项数N为奇数吋,处于中间位置的变最值即为中位数:当N 为偶数时,中位数则为处于中间位置的两个数的平均数. 输入 输入数据第一行 ...
- 哈理工2015 暑假训练赛 zoj 2976 Light Bulbs
MS Memory Limit:65536KB 64bit IO Format:%lld & %llu SubmitStatusid=14946">Practice ...