hdu3081—Marriage Match II(并查集+最大匹配)

题目链接:传送门

Marriage Match II

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


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个女生是朋友,其中任意女生能和其他女生的匹配对象进行匹配,女生间的关系是可以进行传递的,若a是b的朋友,b是c的朋友,则a是c的朋友,问游戏最多能玩几轮。


解题思路:在女生与能够匹配的男生间建边,注意若n个女生是朋友,则她们的匹配对象是共享的(并查集),然后在图中找完美匹配,找到一个就删除匹配的边,看能找到多少个完美匹配。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <stack>
#include <queue>
#include <set>

using namespace std;

typedef long long ll;
const int N = 40900;
const int M = 109;
const int INF = 0x3fffffff;
const double eps = 1e-8;
const double PI = acos(-1.0);

int parent[M],Flag[M];
int match[M],graph[M][M];

bool find( int x , int n )
{
    for( int i = 1 ; i <= n ; ++i ){
        //x能与i匹配,且i在本轮未被访问
        if( graph[x][i] && !Flag[i] ){
            Flag[i] = true;
            //i没被匹配或i的对象能找到其他匹配对象
            if( match[i] == 0 || find(match[i],n) ){
                match[i] = x;
                return true;
            }
        }
    }
    return false;
}

int check( int a )
{
    while( a != parent[a] ){
        parent[a] = parent[parent[a]];
        a = parent[a];
    }
    return a;
}

void merge( int a , int b )
{
    int i = check(a);
    int j = check(b);
    if( i != j ) parent[i] = j;
}

//构建二分图
void Build( int n , int m , int f )
{
    memset( graph , 0 , sizeof(graph) );
    for( int i = 0 ; i < M ; ++i ) parent[i] = i;
    int a,b;
    for( int i = 0 ; i < m ; ++i ){
        scanf("%d%d",&a,&b);
        graph[a][b] = 1;
    }
    for( int i = 0 ; i < f; ++i ){
        scanf("%d%d",&a,&b);
        merge(a,b);
    }

    for( int i = 1 ; i <= n ; ++i ) parent[i] = check(i);

    for( int i = 1 ; i <= n ; ++i ){
        for( int j = 1 ; j <= n ; ++j ){
            if( i == j ) continue;
            if( parent[j] == parent[i] ){
                for( int k = 1 ; k <= n ; ++k ){
                    if( graph[j][k] == 1 ) graph[i][k] = 1;
                }
            }
        }
    }
}

bool solve( int n )
{
    fill( match , match+M , 0 );
    for( int i =  1 ; i <= n ; ++i ){
        fill( Flag , Flag+M , 0 );
        if( !find(i,n) ) return false;
    }
    //匹配一次后的边就不能再用了
    for( int i = 1 ; i <= n ; ++i ){
        graph[match[i]][i] = 0;
    }
    return true;
}

int main()
{
    int T;
    scanf("%d",&T);
    while( T-- ){
        int n,m,f,cnt = 0;
        scanf("%d%d%d",&n,&m,&f);
        Build(n,m,f);
        while( solve(n) ) cnt++;
        printf("%d\n",cnt);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值