HDU3081 Marriage Match II 【最大匹配】
来源:互联网 发布:刷会员软件 编辑:程序博客网 时间:2024/05/08 08:46
Marriage Match II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2296 Accepted Submission(s): 786
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?
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.
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
14 5 21 12 33 24 24 41 42 3
Sample Output
2
话说题意真难读懂,说简单点就是有一些女孩喜欢一些男孩,女孩中有一些是朋友,朋友关系具有传递性,这些男孩女孩间玩结婚游戏,每一轮所有女孩必须都结婚,下一轮游戏时已经相互结过婚的男女孩不能再结婚,它们必须得换对象,求这个游戏最多能玩多少轮。
#include <stdio.h>#include <string.h>#define inf 0x3f3f3f3f#define maxn 102#define maxm 10002int pre[maxn], id;int B[maxn], T, N, M, F;struct Node { int u, v;} E[maxm];bool vis[maxn];bool G[maxn][maxn];int ufind(int k) { int a = k, b; while(pre[k] != -1) k = pre[k]; while(a != k) { b = pre[a]; pre[a] = k; a = b; } return k;}void unite(int u, int v) { u = ufind(u); v = ufind(v); if(u != v) pre[v] = u;}void addEdge(int u, int v) { E[id].u = u; E[id++].v = v;}void getMap() { memset(G, 0, sizeof(G)); memset(B, 0, sizeof(B)); memset(pre, -1, sizeof(pre)); int i, j, u, v; id = 0; for(i = 0; i < M; ++i) { scanf("%d%d", &u, &v); addEdge(u, v); } for(i = 0; i < F; ++i) { scanf("%d%d", &u, &v); unite(u, v); } for(i = 0; i < id; ++i) { E[i].u = ufind(E[i].u); G[E[i].u][E[i].v] = 1; } for(i = 1; i <= N; ++i) { u = ufind(i); if(i != u) memcpy(G[i], G[u], sizeof(bool) * (N + 1)); }}int mfind(int x) { int i, v; for(i = 1; i <= N; ++i) { if(!vis[i] && G[x][i]) { vis[i] = 1; if(!B[i] || mfind(B[i])) { B[i] = x; return 1; } } } return 0;}void solve() { int ans = 0, tmp, i, j; do{ tmp = 0; for(i = 1; i <= N; ++i) if(B[i]) G[B[i]][i] = 0, B[i] = 0; for(i = 1; i <= N; ++i) { memset(vis, 0, sizeof(vis)); tmp += mfind(i); } if(tmp == N) ++ans; else break; } while(true); printf("%d\n", ans);}int main() { // freopen("stdin.txt", "r", stdin); scanf("%d", &T); while(T--) { scanf("%d%d%d", &N, &M, &F); getMap(); solve(); } return 0;}
0 0
- HDU3081 Marriage Match II 【最大匹配】
- hdu3081 Marriage Match II 二分+最大流
- HDU3081 Marriage Match II(最大流)
- hdu3081 Marriage Match II
- hdu3081 Marriage Match II
- hdu3081 Marriage Match II
- hdu3081—Marriage Match II(并查集+最大匹配)
- 【hdu3081】【二分法】【最大流】【并查集】Marriage Match II
- hdu3081 Marriage Match II 最大流+二分+并查集
- 【网络流】hdu3081 Marriage Match II
- 【网络流】【HDU3081】Marriage Match II
- HDU3081 Marriage Match II【网络流】
- HDU3081 3081 Marriage Match II(中等,好题)([二分最大流]+并查集)
- HDU--3081--Marriage Match II--最大匹配,匈牙利算法
- HDU - 3081 Marriage Match II(二分图最大匹配 + 并查集)
- Marriage Match II HDU
- 【hdoj 3081】Marriage Match II 【并查集orfloyd +完美匹配】or【最大流+二分+并查集】
- HDU 3081 Marriage Match II 二分+最大流
- hive show current roles问题
- OneNote自动同步到360网盘
- Linux grep命令用法以及正则表达式
- [ZedBoard移植嵌入式Linux教程(8)]编译Linux内核
- Java---09---异常
- HDU3081 Marriage Match II 【最大匹配】
- 深入理解netfilter
- MySQL 添加列,修改列,删除列
- HDU 5139 Formula
- ThreadLocal
- iOS8中使用CoreLocation定位
- 2.4 1的数目
- 【学习ios之路:C语言】循环方面试题
- javascript日期、时间比较函数