HDU 3081 Marriage Match II 二分图最大匹配

Marriage Match II

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

过家家游戏,有N个男孩和N个女孩,女孩优先。

女孩可以选择没有和她发生争吵的男孩组成一对,也可以和她的闺蜜中没有和她闺蜜发生争吵的男孩组成一对。

组成N对后就开始下一轮,但每一轮都不可以和前面已经组成的人组成一对,问最多可以玩多少轮。

二分最大匹配的模板题,不过边就要用并查集处理下。每次能匹配的人数等于n,ans就加1,然后把已经匹配的边删掉,小与n的话就说明不能匹配了。

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <algorithm>
 5 using namespace std;
 6 const int N = 110;
 7 int edge[N][N], match[N], vis[N], fa[N];
 8 int n, m, f, t;
 9 int find(int x) {
10     return fa[x] = (fa[x]==x ? x: find(fa[x]));
11 }
12 bool dfs(int u) {
13     for(int v = 1; v <= n; v ++) {
14         if(edge[u][v] && !vis[v]) {
15             vis[v] = 1;
16             if(match[v] == -1 || dfs(match[v])){
17                 match[v] = u;
18                 return true;
19             }
20         }
21     }
22     return false;
23 }
24 int main() {
25     ios::sync_with_stdio(false);
26     cin>>t;
27     while(t--) {
28         cin>>n>>m>>f;
29         for(int i = 1; i <= n; i ++) fa[i] = i;
30         memset(edge, 0, sizeof(edge));
31         int u, v;
32         for(int i = 1; i <= m; i ++) {
33             cin>>u>>v;
34             edge[u][v] = 1;
35         }
36         for(int i = 1; i <= f; i ++) {
37             cin>>u>>v;
38             fa[find(u)] = find(v);
39         }
40         for(int i = 1; i <= n; i ++) {
41             for(int j = 1; j <= n; j ++) {
42                 if(find(i)==find(j)) {
43                     for(int k = 1; k <= n; k ++) {
44                         if(edge[i][k])edge[j][k] = 1;
45                     }
46                 }
47             }
48         }
49         int ans = 0;
50         while(1) {
51             int res = 0;
52             memset(match, -1, sizeof(match));
53             for(int i = 1; i <= n; i ++) {
54                 memset(vis, 0, sizeof(vis));
55                 if(dfs(i)) res++;
56             }
57             if(res < n) break;
58             ans++;
59             for(int i = 1; i <= n; i ++) {
60                 if(match[i] != -1) {
61                     edge[match[i]][i] = 0;
62                 }
63             }
64             // for(int i = 1; i <= n; i ++) {
65             //     for(int j = 1; j <= n; j ++) printf("%d ", edge[i][j]);
66             //     printf("\n");
67             // }
68         }
69         printf("%d\n",ans);
70     }
71     return 0;
72 }
时间: 2024-10-11 01:48:33

HDU 3081 Marriage Match II 二分图最大匹配的相关文章

HDU - 3081 Marriage Match II(二分图最大匹配 + 并查集)

题目大意:有n个男生n个女生,现在要求将所有的男女配对,所有的男女都配对的话,算该轮游戏结束了.接着另一轮游戏开始,还是男女配对,但是配对的男女不能是之前配对过的. 问这游戏能玩多少轮 男女能配对的条件如下 1.男女未曾吵架过. 2.如果两个女的是朋友,那么另一个女的男朋友可以和该女的配对 解题思路:并查集解决朋友之间的关系,找到能配对的所有情况 接着二分图匹配,找到一个完美匹配算玩了一轮游戏,接着将这完美匹配的边删掉,继续进行匹配 如果无法完美匹配了,表示游戏结束了 #include <cst

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

HDU 3081 Marriage Match II pid=3081" target="_blank" style="">题目链接 题意:n个女孩n个男孩,每一个女孩能够和一些男孩配对.然后有些女孩是朋友.满足这个朋友圈里面的人,假设有一个能和某个男孩配对,其它就都能够,然后每轮要求每一个女孩匹配到一个男孩.且每轮匹配到的都不同,问最多能匹配几轮 思路:二分轮数k,然后建图为,源点连向女孩,男孩连向汇点容量都为k,然后女孩和男孩之间连边为.有关系的

HDU 3081 Marriage Match II(二分+最大流)

HDU 3081 Marriage Match II 题目链接 题意:n个女孩n个男孩,每个女孩可以和一些男孩配对,然后有些女孩是朋友,满足这个朋友圈里面的人,如果有一个能和某个男孩配对,其他就都可以,然后每轮要求每个女孩匹配到一个男孩,且每轮匹配到的都不同,问最多能匹配几轮 思路:二分轮数k,然后建图为,源点连向女孩,男孩连向汇点容量都为k,然后女孩和男孩之间连边为,有关系的连边容量1,这样一个匹配对应一条边,且不会重复,每次判断最大流是否等于n * k即可 代码: #include <cst

hdu 3081 Marriage Match II (二分+最大流+并查集)

hdu 3081 Marriage Match II 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 p

hdu 3081 Marriage Match II(最大流 + 二分 + 并查集)

Marriage Match II                                                                           Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Presumably, you all have known the question of stable

HDU 3081 Marriage Match II 二分+最大流

题目来源:HDU 3081 Marriage Match II 题意: 思路: 错误代码 纠结不知道哪错了 先放一放 #include <cstdio> #include <queue> #include <vector> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1010; const int INF = 999999999; st

HDU 3081 Marriage Match II

Marriage Match II Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 308164-bit integer IO format: %I64d      Java class name: Main Presumably, you all have known the question of stable marriage match. A girl wi

HDU 3081 Marriage Match II 二分 + 网络流

Marriage Match II 题意:有n个男生,n个女生,现在有 f 条男生女生是朋友的关系, 现在有 m 条女生女生是朋友的关系, 朋友的朋友是朋友,现在进行 k 轮游戏,每轮游戏都要男生和女生配对,每轮配对过的人在接下来中都不能配对,求这个k最大是多少. 题解:二分 + 网络流check . 代码: 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define Fopen freopen("_in.txt",

HDU 3081 Marriage Match II【并查集+二分图最大匹配】

大意:有n个男孩n个女孩,告诉你每个女孩喜欢哪些男孩,又告诉你女孩之间的存在一些朋友关系 一个女孩可以和她喜欢的男孩结婚也可以和她朋友喜欢的男孩结婚, 并且朋友关系可以传递 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 boyfr