您的位置:首页 > 其它

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

2015-08-12 18:54 274 查看

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。

[code]#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
, first
, que
, lev
;
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: