题目链接: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

Problem Description
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?
 
Input
There are several test cases. First is a integer T, means the number of test cases. 
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.
 
Output
For each case, output a number in one line. The maximal number of Marriage Match the children can play.
 
Sample Input
1
4 5 2
1 1
2 3
3 2
4 2
4 4
1 4
2 3
 
Sample Output
2
 
Author
starvae
 
Source
 
Recommend
lcy

题意:

有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 传递闭包 + 二分 + 最大流的更多相关文章

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

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

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

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

  3. hdu3081 Marriage Match II(最大流)

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

  4. hdu3081 Marriage Match II(二分+并查集+最大流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3081 题意: n个女生与n个男生配对,每个女生只能配对某些男生,有些女生相互是朋友,每个女生也可以跟她 ...

  5. HDU 3081:Marriage Match II(二分图匹配+并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=3081 题意:有n个男生n个女生,他们只有没有争吵或者女生a与男生A没有争吵,且女生b与女生a是朋友,因此女生b也 ...

  6. hdu3081 Marriage Match II

    新年第一篇,又花了一早上,真是蠢啊! 二分+网络流 之前对于讨论哪些人是朋友的时候复杂度过高 直接n3的暴力虽然看起来复杂度高,其实并不是每次都成立 #include<bits/stdc++.h ...

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

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

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

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

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

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

随机推荐

  1. Office 中的各种小tips(更新中)

    1.Word 中打字输入会擦掉之后原有字符,出现“吃字”的情况? 要将“改写”切换为“插入”,最简单的方法就是点击键盘上小键盘旁边的“insert”键. 其实仔细观察的话,在word文档下方,会看到如 ...

  2. Spoj-NETADMIN Smart Network Administrator

    The citizens of a small village are tired of being the only inhabitants around without a connection ...

  3. cf468C Hack it!

    Little X has met the following problem recently. Let's define f(x) as the sum of digits in decimal r ...

  4. asp.net mvc 页面内容呈现Html.Raw HtmlString

    asp.net mvc 页面内容呈现Html.Raw Html.Raw内容经过页面呈现,不呈现Html标签 @Html.Raw( File.ReadAllText(Server.MapPath(&qu ...

  5. UVa10491 Cows and Cars

    #include<iostream> #include<cstdio> #include<algorithm> int main(){ double a,b,c; ...

  6. iOS 设置RGB色的宏

    转自:http://lizhuang.iteye.com/blog/1931768 
//RGB Color macro #define UIColorFromRGB(rgbValue) [UICol ...

  7. max file descriptors [4096] for elasticsearch proess is too low, increase to at least [65536]

    修改文件 /etc/security/limits.conf 加入以下两行: sonar hard nofile 65536 sonar soft nofile  65536 #备注:sonar这里是 ...

  8. AC日记——接苹果 洛谷 P2690

    题目背景 USACO 题目描述 很少有人知道奶牛爱吃苹果.农夫约翰的农场上有两棵苹果树(编号为1和2), 每一棵树上都长满了苹果.奶牛贝茜无法摘下树上的苹果,所以她只能等待苹果 从树上落下.但是,由于 ...

  9. go语言学习之路五:Go语言内存分配机制make&new

    Go有两种分配内存的机制,规则很简单,下面来简单介绍一下.1.new函数New()函数可以给一个值类型的数据分配内存(不知道什么是值类型请前往切片那一部分),调用成功后返回一个初始化的内存块指针,同时 ...

  10. iOS 内存管理策略

    内存管理策略(memory Management Policy) NSObject protocol中定义的的方法和标准命名惯例一起提供了一个引用计数环境,内存管理的基本模式处于这个环境中.NSObj ...