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

链接:

https://vjudge.net/problem/HDU-3081

题意:

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?

思路:

二分枚举答案, 每次对图用枚举的答案建图,男女根据并查集确定能否配对连一条权值为1 的边.
跑最大流即可.
#代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;

const int MAXN = 200+10;
const int INF = 1e9;

struct Edge
{
    int from, to, cap;
};
vector<Edge> edges;
vector<int> G[MAXN];
int Dis[MAXN], Vis[MAXN];
int Fa[MAXN];
int Map[MAXN][MAXN];
int n, m, s, t, d;

void AddEdge(int from, int to, int cap)
{
    edges.push_back(Edge{from, to, cap});
    edges.push_back(Edge{to, from, 0});
    G[from].push_back(edges.size()-2);
    G[to].push_back(edges.size()-1);
}

bool Bfs()
{
    //Bfs构造分层网络
    memset(Dis, -1, sizeof(Dis));
    queue<int> que;
    que.push(s);
    Dis[s] = 0;
    while (!que.empty())
    {
        int u = que.front();
        que.pop();
//        cout << u << endl;
        for (int i = 0;i < G[u].size();i++)
        {
            Edge & e = edges[G[u][i]];
            if (e.cap > 0 && Dis[e.to] == -1)
            {
                que.push(e.to);
                Dis[e.to] = Dis[u]+1;
            }
        }
    }
    return (Dis[t] != -1);
}

int Dfs(int u, int flow)
{
    //flow 表示当前流量上限
    if (u == t)
        return flow;
    int res = 0;
    for (int i = 0;i < G[u].size();i++)
    {
        Edge & e = edges[G[u][i]];
        if (e.cap > 0 && Dis[u]+1 == Dis[e.to])
        {
            int tmp = Dfs(e.to, min(flow, e.cap)); //  递归计算顶点 v
            flow -= tmp;
            e.cap -= tmp;
            res += tmp;
            edges[G[u][i]^1].cap += tmp;
            if (flow == 0)
                break;
        }
    }
    if (res == 0)
        Dis[u] = -1;
    return res;
}

int MaxFlow()
{
    int res = 0;
    while (Bfs())
    {
        res += Dfs(s, INF);
    }
    return res;
}

int GetF(int x)
{
    if (Fa[x] == x)
        return x;
    Fa[x] = GetF(Fa[x]);
    return Fa[x];
}

bool Check(int mid)
{
    for (int i = s;i <= t;i++)
        G[i].clear();
    edges.clear();
    for (int i = 1;i <= n;i++)
    {
        AddEdge(s, i, mid);
        AddEdge(n+i, t, mid);
    }
    for (int i = 1;i <= n;i++)
    {
        for (int j = 1;j <= n;j++)
        {
            if (Map[i][j])
                AddEdge(i, n+j, 1);
        }
    }
    int res = MaxFlow();
//    cout << mid << ' ' << res << endl;
    return res == mid*n;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    cin >> T;
    while (T--)
    {
        memset(Map, 0, sizeof(Map));
        cin >> n >> m >> d;
        for (int i = 1;i <= n;i++)
            Fa[i] = i;
        s = 0, t = n*2+1;
        int u, v;
        for (int i = 1;i <= m;i++)
        {
            cin >> u >> v;
            Map[u][v] = 1;
        }
        for (int i = 1;i <= d;i++)
        {
            cin >> u >> v;
            int tu = GetF(u);
            int tv = GetF(v);
            if (tu != tv)
                Fa[tv] = tu;
        }
        for (int i = 1;i <= n;i++)
        {
            for (int j = 1;j <= n;j++)
            {
                if (i == j || GetF(i) != GetF(j))
                    continue;
                for (int k = 1;k <= n;k++)
                {
                    if (Map[i][k])
                        Map[j][k] = 1;
                    if (Map[j][k])
                        Map[i][k] = 1;
                }
            }
        }
        int l = 0, r = n;
        int res = 0;
//        Check(2);
        while (l <= r)
        {
            int mid = (l+r)/2;
            if (Check(mid))
            {
                res = max(res, mid);
                l = mid+1;
            }
            else
                r = mid-1;
        }
        cout << res << endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/YDDDD/p/11334488.html

时间: 2024-10-16 20:27:49

HDU-3081-Marriage Match 2(最大流, 二分答案, 并查集)的相关文章

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

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

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 (二分+最大流+并查集)

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 3277 Marriage Match III(拆点+二分+最大流SAP)

这个题目是说,有n个女的和男的找伴侣.然后女的具有主动选择权,每个女的可以选自己喜欢的男的,也可以挑选k个不喜欢的男的,做法就是:把女的拆点,u1->u2建立一条容量为k的边.如果遇见喜欢的男生i->j+2*n建一条容量为1的边,否则i+n->j+2*n建一条容量为1的边.最后将源点和女生相连容量为mid,汇点与男生相连容量为mid.枚举mid,看是否会产生满流. 可能姿势不够优美dinic超时了啊,换成SAP快了很多啊... Marriage Match III Time Limit:

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

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

HDU 3081 Marriage Match II &lt;&lt;二分最大流 + 并查集

题意 n个女孩子跟n个男孩子过家家,女孩子选男孩子,告诉你每个女孩子可选的男孩子与女孩子之间的好友关系,好友关系是互相的而且是传递的,然后如果两个女孩子是好友,他们可选的男孩子也是可以合并的.然后每一轮进行匹配,匹配成功后开始下一轮,每个女孩子只能选同一个男孩子一次,问最多能玩几轮. 思路 首先,好友关系的建立显然就直接想到了用并查集处理. 然后在建图时,可以选择是二分图,然后跑完备匹配,每次匹配完后删除匹配边进行下一次匹配. 当然,不会二分图的我就选择直接跑网络流啦,但是建图时候发现需要知道流

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

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",