首页 > 代码库 > HDU3081 Marriage Match II 【最大匹配】

HDU3081 Marriage Match II 【最大匹配】

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?
 

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
 

话说题意真难读懂,说简单点就是有一些女孩喜欢一些男孩,女孩中有一些是朋友,朋友关系具有传递性,这些男孩女孩间玩结婚游戏,每一轮所有女孩必须都结婚,下一轮游戏时已经相互结过婚的男女孩不能再结婚,它们必须得换对象,求这个游戏最多能玩多少轮。

#include <stdio.h>
#include <string.h>

#define inf 0x3f3f3f3f
#define maxn 102
#define maxm 10002

int 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;
}


HDU3081 Marriage Match II 【最大匹配】