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;
}
时间: 2024-08-05 10:57:18

HDU3081 Marriage Match II 【最大匹配】的相关文章

HDU-3081 Marriage Match II (最大流,二分答案,并查集)

题目链接:HDU-3081 Marriage Match II 题意 有$n$个男孩和$n$个女孩玩配对游戏,每个女孩有一个可选男孩集合(即每轮游戏的搭档可从集合中选择),已知有些女孩之间是朋友(这里的朋友关系是相互的,即a和b是朋友,a和c是朋友,那么b和c也是朋友),那么她们可以共享男孩集合,即这些男孩集合的并集成为她们各自的可选男孩集合,如果某一轮女孩选择了一个男孩作为搭档,则这个男孩后面不能再作为这个女孩的搭档,问游戏最多进行几轮. 思路 女孩朋友之间共享男孩集合问题可以用并查集解决,将

hdu3081 Marriage Match II(最大流)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Marriage Match II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2410    Accepted Submission(s): 820 Problem Description Presumably, y

hdu3081 Marriage Match II 二分+最大流

题意: n个男孩n个女孩,女孩选男孩,每个女孩都要选到不同的人 k对女孩有相同选择标准, 女孩每轮都选择没选过的男孩, 问总共能选几轮. 思路: 女孩1..n,男孩n+1..2*n编号 由女孩到男孩建容量为1的边 起点st=2*n+1,到1..n建边: n+1..2*n到终点ed=2*n+2建边 二分搜索最大容量即为答案.详见代码: /********************************************************* file name: hdu3081.cpp

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 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 hdu 3277 hdu 3416 Marriage Match II III IV //最大流的灵活运用

3081 题意: n个女孩选择没有与自己吵过架的男孩有连边(自己的朋友也算,并查集处理),2分图,有些边,求有几种完美匹配(每次匹配每个点都不重复匹配) 我是建二分图后,每次增广一单位,(一次完美匹配),再修改起点还有终点的边流量,继续增广,直到达不到完美匹配为止.网上很多是用二分做的,我觉得没必要...(网上传播跟风真严重...很多人都不是真正懂最大流算法的...) 3277 : 再附加一条件,每个女孩可以最多与k个自己不喜欢的男孩.求有几种完美匹配(同上). 我觉得:求出上题答案,直接ans

HDU 3081 Marriage Match II(二分法+最大流量)

HDU 3081 Marriage Match II pid=3081" target="_blank" style="">题目链接 题意:n个女孩n个男孩,每一个女孩能够和一些男孩配对.然后有些女孩是朋友.满足这个朋友圈里面的人,假设有一个能和某个男孩配对,其它就都能够,然后每轮要求每一个女孩匹配到一个男孩.且每轮匹配到的都不同,问最多能匹配几轮 思路:二分轮数k,然后建图为,源点连向女孩,男孩连向汇点容量都为k,然后女孩和男孩之间连边为.有关系的

Marriage Match II (hdu 3081 二分图+并查集)

Marriage Match II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2455    Accepted Submission(s): 837 Problem Description Presumably, you all have known the question of stable marriage match. A

HDU 3081 Marriage Match II(二分+最大流)

HDU 3081 Marriage Match II 题目链接 题意:n个女孩n个男孩,每个女孩可以和一些男孩配对,然后有些女孩是朋友,满足这个朋友圈里面的人,如果有一个能和某个男孩配对,其他就都可以,然后每轮要求每个女孩匹配到一个男孩,且每轮匹配到的都不同,问最多能匹配几轮 思路:二分轮数k,然后建图为,源点连向女孩,男孩连向汇点容量都为k,然后女孩和男孩之间连边为,有关系的连边容量1,这样一个匹配对应一条边,且不会重复,每次判断最大流是否等于n * k即可 代码: #include <cst