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

题目大意:n个男孩n个女孩配对,每个女孩选的人不能相同,k对女孩有相同选择标准,女孩每轮都要选择之前几轮没选过的男孩,问总共能选几轮。

解题思路:超级源点向女生建边,容量为mid,男生向超级汇点建边,容量为mid。女生向其认同的男生建边,容量为1。相同选择标准的女生可以用并查集处理。然后二分mid,取最大的mid。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#pragma comment(linker, "/STACK:102400000,102400000") //手动扩栈防RE
using namespace std;

const int PO = 205;
const int N = 500;
const int M = 50000;
const int INF = 0x3f3f3f3f;
typedef long long ll;
int n, m, k, s, t;
int f[PO];
struct Node{
    int x, y;
}mg[M];
int vis[PO][PO];
int ec, head[N], first[N], que[N], lev[N];
int Next[M], to[M], v[M];

void init() {
    ec = 0;
    memset(first, -1, sizeof(first));
}

void addEdge(int a,int b,int c) {
    to[ec] = b;
    v[ec] = c;
    Next[ec] = first[a];
    first[a] = ec++;

    to[ec] = a;
    v[ec] = 0;
    Next[ec] = first[b];
    first[b] = ec++;
}

int BFS() {
    int kid, now, f = 0, r = 1, i;
    memset(lev, 0, sizeof(lev));
    que[0] = s, lev[s] = 1;
    while (f < r) {
        now = que[f++];
        for (i = first[now]; i != -1; i = Next[i]) {
            kid = to[i];
            if (!lev[kid] && v[i]) {
                lev[kid] = lev[now] + 1;
                if (kid == t) return 1;
                que[r++] = kid;
            }
        }
    }
    return 0;
}

int DFS(int now, int sum) {
    int kid, flow, rt = 0;
    if (now == t) return sum;
    for (int i = head[now]; i != -1 && rt < sum; i = Next[i]) {
        head[now] = i;
        kid = to[i];
        if (lev[kid] == lev[now] + 1 && v[i]) {
            flow = DFS(kid, min(sum - rt, v[i]));
            if (flow) {
                v[i] -= flow;
                v[i^1] += flow;
                rt += flow;
            } else lev[kid] = -1;
        }
    }
    return rt;
}

int dinic() {
    int ans = 0;
    while (BFS()) {
        for (int i = 0; i <= t; i++) {
            head[i] = first[i];
        }
        ans += DFS(s, INF);
    }
    return ans;
}   

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

void Union(int x, int y) {
    if (find(x) != find(y)) f[find(x)] = find(y);
}

void input() {
    scanf("%d %d %d", &n, &m, &k);
    for (int i = 0; i <= n; i++) f[i] = i;
    s = 0, t = n * 2 + 2;
    for (int i = 0; i < m; i++) {
        scanf("%d %d", &mg[i].x, &mg[i].y);
    }
    int a, b;
    for (int i = 0; i < k; i++) {
        scanf("%d %d", &a, &b);
        Union(a, b);
    }
    memset(vis, 0, sizeof(vis));
    for (int i = 1; i <= n; i++) {
        addEdge(s, i, 1);
        addEdge(i + n, t, 1);
    }

    for (int i = 0; i < m; i++) {
        int a = mg[i].x, b = mg[i].y;
        for (int j = 1; j <= n; j++) {
            if (find(a) == find(j) && !vis[j][b]) {
                vis[j][b] = 1;
                addEdge(j, b + n, 1);
            }
        }
    }
}

int build(int x) {
    for (int i = 0; i < ec; i += 2) { //更换容量,清空流量
        if (to[i^1] == s) {
            v[i] = x;
            v[i^1] = 0;
        } else if (to[i] == t) {
            v[i] = x;
            v[i^1] = 0;
        } else {
            v[i] = 1;
            v[i^1] = 0;
        }
    }
    return dinic();
}

void solve() {
    int l = 0, r = n + 2;
    while (l < r) {
        int mid = (l + r) / 2;
        if (build(mid) == mid * n) {
            l = mid + 1;
        } else r = mid;
    }
    printf("%d\n", r - 1);
}

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

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

时间: 2024-07-30 03:24:06

hdu 3081 Marriage Match II (二分+最大流+并查集)的相关文章

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

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

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

题目大意:有n个男生n个女生,现在要求将所有的男女配对,所有的男女都配对的话,算该轮游戏结束了.接着另一轮游戏开始,还是男女配对,但是配对的男女不能是之前配对过的. 问这游戏能玩多少轮 男女能配对的条件如下 1.男女未曾吵架过. 2.如果两个女的是朋友,那么另一个女的男朋友可以和该女的配对 解题思路:并查集解决朋友之间的关系,找到能配对的所有情况 接着二分图匹配,找到一个完美匹配算玩了一轮游戏,接着将这完美匹配的边删掉,继续进行匹配 如果无法完美匹配了,表示游戏结束了 #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 二分 + 网络流

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

hdu 3277 Marriage Match III【最大流+并查集+二分枚举】

Marriage Match III Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1491    Accepted Submission(s): 440 Problem Description Presumably, you all have known the question of stable marriage match.

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

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