HDU3081二分匹配

来源:互联网 发布:台湾天心软件 编辑:程序博客网 时间:2024/05/18 02:25

题目:

Marriage Match II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2249    Accepted Submission(s): 773


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
14 5 21 12 33 24 24 41 42 3
 

Sample Output
2
 

Author
starvae
 

Source
HDU 2nd “Vegetable-Birds Cup” Programming Open Contest
 

题意比较简单:

        就是给你n个男孩和n个女孩,进行两两(男女)配对玩过家家的游戏。配对的条件是女的和男的是朋友关系或者女A和女B是朋友关系,女A跟男A是朋友关系,女B和男A也可以进行配对。玩过一轮过家家游戏之后,则这两个人不能再一起配对。求最多玩几轮过家家游戏。

思路:

        刚开始做这道题的时候,解决不了女女之间是朋友关系的情况该怎么处理,后来看了下别人的思路要用并查集,所以这才做出来。每次配对完之后看下能不能达到n对,如果能的话就次数加1,并且删除已经配对的边,再进行下一次的配对,直到进行配对的人数小于n时,跳出配对。


代码以及解释如下:

#include<cstdio>#include<cstring>#include<iostream>#include<sstream>#include<algorithm>#include<vector>#include<bitset>#include<set>#include<queue>#include<stack>#include<map>#include<cstdlib>#include<cmath>#define PI 2*asin(1.0)const int  MOD = 1e9 + 7;const int N = 1e2 + 15;const int INF = (1 << 30) - 1;using namespace std;int n, m, f;int a, b;int g[N][N];int father[N];///利用并查集进行合并int Find(int x) {    if(x == father[x]) return x;    return x = Find(father[x]);}void Union(int a, int b) {    int aa = Find(a);    int bb = Find(b);    if(aa != bb) father[bb] = aa;}void init() {    for(int i = 1; i <= n; i++) father[i] = i;    memset(g, 0, sizeof(g));}///匈牙利算法进行二分匹配int cx[N], cy[N]; ///匹配int sx[N], sy[N];int path(int u) {    sx[u] = 1;    for(int i = 1; i <= n; i++) {        if(g[u][i] && !sy[i]) {            sy[i] = 1;            ///如果i没有匹配,或者i匹配了,但从cy[i]能找到增广路            if(cy[i] == -1 || path(cy[i])) { ///查找                cx[u] = i, cy[i] = u;                return 1;///找到增广路            }        }    }    return 0;}///删除已经匹配的边void Del() {    for(int i = 1; i <= n; i++)        g[i][cx[i]] = 0;}int maxflow() {    int ans = 0;    while(1) {        memset(cx, -1, sizeof(cx));        memset(cy, -1, sizeof(cy));        int flow = 0;        for(int i = 1; i <= n; i++) {            memset(sx, 0, sizeof(sx));            memset(sy, 0, sizeof(sy));            if(cx[i] == -1) flow += path(i);///没有匹配的进行匹配        }        ///是否达到条件        if(flow == n) {            ans++;            Del();        } else break;    }    return ans;}int main() {    int tc;    scanf("%d", &tc);    while(tc--) {        scanf("%d%d%d", &n, &m, &f);        init();        for(int i = 0; i < m; i++) {            scanf("%d%d", &a, &b);            g[a][b] = 1;        }        ///朋友关系进行合并,为了之后更好的查找        for(int i = 0; i < f; i++) {            scanf("%d%d", &a, &b);            Union(a, b);        }        for(int i = 1; i <= n; i++)            for(int j = 1; j <= n; j++) {                if(i == j) continue;                if(Find(i) == Find(j)) {///朋友关系的话,朋友之间可以共享                    for(int k = 1; k <= n; k++) {                        if(g[i][k]) g[j][k] = 1;                    }                }            }        printf("%d\n", maxflow());    }    return 0;}

挺好的一道题,涉及到并查集于二分匹配。这道题也可以用网络流来做,网络流每次找最小割的问题。

0 0
原创粉丝点击