HDU - 3081 Marriage Match II(二分图最大匹配 + 并查集)

题目大意:有n个男生n个女生,现在要求将所有的男女配对,所有的男女都配对的话,算该轮游戏结束了。接着另一轮游戏开始,还是男女配对,但是配对的男女不能是之前配对过的。

问这游戏能玩多少轮

男女能配对的条件如下

1.男女未曾吵架过。

2.如果两个女的是朋友,那么另一个女的男朋友可以和该女的配对

解题思路:并查集解决朋友之间的关系,找到能配对的所有情况

接着二分图匹配,找到一个完美匹配算玩了一轮游戏,接着将这完美匹配的边删掉,继续进行匹配

如果无法完美匹配了,表示游戏结束了

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 1010
int g[N][N], link[N], vis[N], father[N];
int n, m, f;

int find(int x) {
    return x == father[x] ? x : father[x] = find(father[x]);
}

void init() {
    scanf("%d%d%d", &n, &m, &f);
    memset(g, 0, sizeof(g));
    for (int i = 1; i <= n; i++)
        father[i] = i;

    int x, y;
    for (int i = 0; i < m; i++) {
        scanf("%d%d", &x, &y);
        g[x][y] = 1;
    }

    for (int i = 0; i < f; i++) {
        scanf("%d%d", &x, &y);
        father[find(x)] = find(y);
    }

    for (int i = 1; i <= n; i++) {
        int t = find(i);

        for (int j = 1; j <= n; j++) {
            if (i != j && t == find(j)) {
                for (int k = 1; k <= n; k++)
                    if (g[j][k])
                        g[i][k] = 1;
            }
        }
    }
}

bool dfs(int u) {
    for (int v = 1; v <= n; v++) {
        if (!vis[v] && g[u][v]) {
            vis[v] = 1;
            if (!link[v] || dfs(link[v])) {
                link[v] = u;
                return true;
            }
        }
    }
    return false;
}

void solve() {
    int ans = 0;
    while (1) {
        memset(link, 0, sizeof(link));
        int cnt = 0;
        for (int i = 1; i <= n; i++) {
            memset(vis, 0, sizeof(vis));
            if (dfs(i))
                cnt++;
        }
        if (cnt == n) {
            ans++;
            for (int i = 1; i <= n; i++)
                g[link[i]][i] = 0;
        }
        else
            break;
    }
    printf("%d\n", ans);
}

int main() {
    int test;
    scanf("%d", &test);
    while (test--) {
        init();
        solve();
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-24 02:54:57

HDU - 3081 Marriage Match II(二分图最大匹配 + 并查集)的相关文章

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(网络流+并查集+二分答案)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3081 题目: 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. W

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

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

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

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

hdu 3081 Marriage Match II (二分+最大流+并查集)

hdu 3081 Marriage Match II 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 p

hdu 3081 Marriage Match II(最大流 + 二分 + 并查集)

Marriage Match II                                                                           Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Presumably, you all have known the question of stable

HDU 3081 Marriage Match II 二分+最大流

题目来源:HDU 3081 Marriage Match II 题意: 思路: 错误代码 纠结不知道哪错了 先放一放 #include <cstdio> #include <queue> #include <vector> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1010; const int INF = 999999999; st

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 Marriage Match II 二分 + 网络流

Marriage Match II 题意:有n个男生,n个女生,现在有 f 条男生女生是朋友的关系, 现在有 m 条女生女生是朋友的关系, 朋友的朋友是朋友,现在进行 k 轮游戏,每轮游戏都要男生和女生配对,每轮配对过的人在接下来中都不能配对,求这个k最大是多少. 题解:二分 + 网络流check . 代码: 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define Fopen freopen("_in.txt",