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

46 篇文章 0 订阅

Marriage Match II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1322    Accepted Submission(s): 470


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
题意:有n个女生和n个男生,给出m个关系a b,表示a能与b配对。给出f个关系a b,表示a与b是朋友。若a与c能配对,而a与b是朋友,则b与c也能配对。游戏进行多轮,每一轮每个女生找之前都没有配对过的男生配对,问这个游戏最多能进行多少轮。
思路:源点向每个女生(i——n)连一条边,容量为轮数k,每个男生(n+1——2*n)向汇点连一条边,容量也为轮数k,然后女生向能配对的男生连一条流量为1的边。这样求出的最大流如果满流,即为k*n,那么就可以进行n轮游戏。所以我们可以二分枚举轮数k,然后看是否满流就可以了。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <cmath>
#include <cstdlib>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define ll __int64
#define eps 1e-6

using namespace std;

const int INF = 1000000007;
const int maxn = 300;
struct Edge{
    int u, v, cap, flow, next;
}et[maxn*maxn];
struct node{
    int boy,girl;
}match[maxn*maxn];
int cnt[maxn], pre[maxn], cur[maxn], eh[maxn], dis[maxn], low[maxn];
int fa[maxn];
bool G[maxn][maxn];
int n, m, f, s, t, num;
void init(){
    memset(eh, -1, sizeof(eh));
    num = 0;
}
void add(int u, int v ,int cap, int flow){
    Edge e = {u, v, cap, flow, eh[u]};
    et[num] = e;
    eh[u] = num++;
}
void addedge(int u, int v, int cap){
    add(u, v, cap, 0);
    add(v, u, 0, 0);
}
void make_set() {for(int i = 1; i <= n; i++) fa[i] = i;}
int find(int x) {return x == fa[x] ? x : fa[x] = find(fa[x]);}
void Union(int a, int b) {fa[find(b)] = find(a);}
void build_graph(int cap)
{
    init();
    memset(G, 0, sizeof(G));
    for(int i = 1; i <= n; i++)
    {
        addedge(s, i, cap);
        addedge(i+n, t, cap);
    }
    for(int i = 0; i < m; i++)
    {
        int u = match[i].girl, v = match[i].boy;
        for(int j = 1; j <= n; j++)
        if(find(u)==find(j)&&!G[j][v])
        {
            G[j][v] = 1;
            addedge(j, v + n, 1);
        }
    }
}
int isap(int s, int t, int nv)
{
    int u, v, now, flow = 0;
    memset(low, 0, sizeof(low));
    memset(dis, 0, sizeof(dis));
    memset(cnt, 0, sizeof(cnt));
    for(u = 0; u <= nv; u++) cur[u] = eh[u];
    low[s] = INF, cnt[0] = nv, u = s;
    while(dis[s] < nv)
    {
        for(now = cur[u]; now != -1; now = et[now].next)
        if(et[now].cap - et[now].flow && dis[u] == dis[v = et[now].v] + 1) break;
        if(now != -1)
        {
            cur[u] = pre[v] = now;
            low[v] = min(et[now].cap - et[now].flow, low[u]);
            u = v;
            if(u == t)
            {
                for(; u != s; u = et[pre[u]].u)
                {
                    et[pre[u]].flow += low[t];
                    et[pre[u]^1].flow -= low[t];
                }
                flow += low[t];
                low[s] = INF;
            }
        }
        else
        {
            if(--cnt[dis[u]] == 0) break;
            dis[u] = nv, cur[u] = eh[u];
            for(now = eh[u]; now != -1; now = et[now].next)
            if(et[now].cap - et[now].flow && dis[u] > dis[et[now].v] + 1)
            dis[u] = dis[et[now].v] + 1;
            cnt[dis[u]]++;
            if(u != s) u = et[pre[u]].u;
        }
    }
    return flow;
}
int main()
{
    int tt,a,b;
    scanf("%d", &tt);
    while(tt--)
    {
        scanf("%d%d%d", &n, &m, &f);
        s = 0;
        t = 2 * n + 1;
        for(int i = 0; i < m; i++)
        scanf("%d%d", &match[i].girl, &match[i].boy);
        make_set();
        while(f--)
        {
            scanf("%d%d", &a, &b);
            Union(a, b);
        }
        int low = 0, high = 101, mid, ans = 0;
        while(low <= high)
        {
            mid = (low + high) >> 1;
            build_graph(mid);
            if(isap(s, t, t+1) == mid * n)
            {
                ans = mid;
                low = mid + 1;
            }
            else high = mid - 1;
        }
        printf("%d\n", ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值