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 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?

问最后能进行多少轮game

分析:

用并查集处理处朋友的关系集合   把每个人到喜欢的男孩和其朋友喜欢的男孩建边

当最大匹配 == n 的时候 rounds++ 然后删除匹配边

求出round数即可

代码:

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <vector>
  5 using namespace std;
  6
  7 const int maxn = 105;
  8 const int INF = 1000000000;
  9
 10 int fa[maxn];
 11 void init(int x) {
 12     for(int i = 0; i <= x; i++) {
 13         fa[i] = i;
 14     }
 15 }
 16
 17 int find(int u) {
 18     if(fa[u] == u) return u;
 19     return fa[u] = find(fa[u]);
 20 }
 21
 22 void unin(int u, int v) {
 23     int fu = find(u);
 24     int fv = find(v);
 25     if(fu != fv) {
 26         fa[fu] = fv;
 27     }
 28 }
 29
 30
 31 int Link[maxn];
 32 int W[maxn][maxn];
 33 int vis[maxn];
 34 int n;
 35
 36 int Find(int i) {
 37     for(int j = 1; j <= n; j++) {
 38         if(W[i][j] && !vis[j]) {
 39             vis[j] = 1;
 40             if(Link[j] == -1 || Find(Link[j]) ) {
 41                 Link[j] = i;
 42                 return true;
 43             }
 44         }
 45     }
 46     return false;
 47 }
 48
 49 bool Match() {
 50     memset(Link, -1, sizeof(Link));
 51     for(int i = 1; i <= n; i++) {
 52         memset(vis, 0, sizeof(vis));
 53         if(!Find(i)) return false;
 54     }
 55     return true;
 56 }
 57
 58 int Solve() {
 59     int cnt = 0;
 60     while(1) {
 61         if(Match() ) {
 62             cnt ++;
 63             for(int j = 1; j <= n; j++) {
 64                 W[Link[j]][j] = 0;
 65             }
 66         } else break;
 67     }
 68     return cnt;
 69 }
 70
 71 vector<int> v[maxn * maxn];
 72
 73 int main() {
 74     int m, f;
 75     int a, b;
 76     int c, d;
 77     int t;
 78     scanf("%d", &t);
 79     while(t--) {
 80         scanf("%d %d %d", &n, &m, &f);
 81         memset(W, 0, sizeof(W));
 82         init(n);
 83         for(int i = 0; i <= n; i++) v[i].clear();
 84         for(int i = 1; i <= m; i++) {
 85             scanf("%d %d", &a, &b);
 86             v[a].push_back(b);
 87         }
 88         for(int i = 1; i <= f; i++) {
 89             scanf("%d %d",&c, &d);
 90             unin(c, d);
 91         }
 92         for(int i = 1; i <= n; i++) {
 93             for(int j = 1; j <= n; j++) {
 94                 if(find(i) == find(j) ) {
 95                     for(int k = 0; k < v[j].size(); k++) {
 96                         W[i][v[j][k]] = 1;
 97                     }
 98                 }
 99             }
100         }
101         printf("%d\n", Solve() ) ;
102     }
103     return 0;
104 }

时间: 2024-07-30 10:18:40

HDU 3081 Marriage Match II【并查集+二分图最大匹配】的相关文章

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 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 题意: 思路: 错误代码 纠结不知道哪错了 先放一放 #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 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 pl

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(网络流+并查集+二分答案)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3081 题目: 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. W