hdoj 3277 Marriage Match III 【最大流经典建图】【二分 + 最大流 + 并查集】

62 篇文章 0 订阅
35 篇文章 0 订阅

Marriage Match III

Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1622    Accepted Submission(s): 480


Problem Description
Presumably, you all have known the question of stable marriage match. A girl will choose a boy; it is similar as the ever game of play-house . What a happy time as so many friends play 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. As 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. On the other hand, in order to play more times of marriage match, every girl can accept any K boys. If a girl chooses a boy, the boy must accept her unconditionally whether they had quarreled before or not.

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 an integer T, means the number of test cases.
Each test case starts with three integer n, m, K and f in a line (3<=n<=250, 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 1 2 1 1 2 3 3 2 4 2 4 4 1 4 2 3
 

Sample Output
  
  
3
 

建议先做下hdoj3081,用最大流写匹配。

题意:同hdoj3081, 只不过加了个条件:一个女孩可以额外选择K个男孩,这K个男孩是任意的且无条件被选中。问你最多多少次游戏后不会存在完美匹配。

这里我还是把没有吵过架当作喜欢关系来说。。。


算法:二分 + 最大流 + 并查集

分析:在查找mid时,对每个女孩 有如下两个情况可能发生

一:当前情况满足完美匹配,那么做法同hdoj3081,也就是说题目给出的额外条件在当前情况我们用不到;

二:若不能,说明什么?我们可以有这个猜测:这个女孩她喜欢的以及她朋友所喜欢的男孩已经被选完了,那怎么办?她只有选那些她不喜欢的男孩。


这时,我们可以把一个女孩 拆分成两个点,一个点A代表情况一,另一个点B代表情况二。为了保证对于每一个女孩在点A无人可选时有且只有K此机会选人,我们可以让A连接

B且权值为K,再让B连接该女孩不喜欢的所有男孩即可。显然我们有如下方案建边

1,源点连接A,权值为当前查找值mid;

2,A连接 该女孩喜欢的男孩,权值为1;

3,A连接B,权值为K;

4,B连接 该女孩不喜欢的男孩,权值为1;

5,所有男孩连接汇点,权值为mid。


AC代码:

#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <algorithm>
#define MAXN 1000+10
#define MAXM 2000000+100
#define INF 1000000
using namespace std;
struct Edge
{
    int from, to, cap, flow, next;
};
Edge edge[MAXM];
int head[MAXN], edgenum;
int cur[MAXN];
int dist[MAXN];
bool vis[MAXN];
int Map[MAXN][MAXN];
int set[MAXN];
int N, M, K, F;
int find(int p)
{
    int t;
    int child = p;
    while(p != set[p])
        p = set[p];
    while(child != p)
    {
        t = set[child];
        set[child] = p;
        child = t;
    }
    return p;
}
void merge(int x, int y)
{
    int fx = find(x);
    int fy = find(y);
    if(fx != fy)
        set[fx] =fy;
}
bool same(int x, int y)
{
    return find(x) == find(y);
}
void input()
{
    int a, b;
    memset(Map, 0, sizeof(Map));
    for(int i = 1; i <= N; i++) set[i] = i;
    for(int i = 1; i <= M; i++)
    {
        scanf("%d%d", &a, &b);
        Map[a][b] = 1;
    }
    for(int i = 1; i <= F; i++)
        scanf("%d%d", &a, &b), merge(a, b);
}
void solve()
{
    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j <= N; j++)
        {
            if(i != j && same(i, j))
            {
                for(int k = 1; k <= N; k++)
                {
                    if(Map[j][k])
                        Map[i][k] = 1;
                }
            }
        }
    }
}
void init()
{
    edgenum = 0;
    memset(head, -1, sizeof(head));
}
void addEdge(int u, int v, int w)
{
    Edge E1 = {u, v, w, 0, head[u]};
    edge[edgenum] = E1;
    head[u] = edgenum++;
    Edge E2 = {v, u, 0, 0, head[v]};
    edge[edgenum] = E2;
    head[v] = edgenum++;
}
void getMap(int mid)
{
    for(int i = 1; i <= N; i++)//每个女孩
    {
        addEdge(i, i + 2*N, K);//拆点 建立权值为K的边
        addEdge(0, i, mid);//从源点引一条 权值为mid的边
        addEdge(i + N, 3*N + 1, mid);//每个男孩引一条到汇点的 权值为mid的边
        for(int j = 1; j <= N; j++)
        {
            if(Map[i][j])//选择她所喜欢的男孩
                addEdge(i, j + N, 1);
            else//她不喜欢的男孩 她也可以选
                addEdge(i + 2*N, j + N, 1);
        }
    }
}
bool BFS(int start, int end)
{
    queue<int> Q;
    memset(dist, -1, sizeof(dist));
    memset(vis, false, sizeof(vis));
    dist[start] = 0;
    vis[start] = true;
    Q.push(start);
    while(!Q.empty())
    {
        int u = Q.front();
        Q.pop();
        for(int i = head[u]; i != -1; i = edge[i].next)
        {
            Edge E = edge[i];
            if(!vis[E.to] && E.cap > E.flow)
            {
                vis[E.to] = true;
                dist[E.to] = dist[u] + 1;
                if(E.to == end) return true;
                Q.push(E.to);
            }
        }
    }
    return false;
}
int DFS(int x, int a, int end)
{
    if(x == end || a == 0) return a;
    int flow = 0, f;
    for(int &i = cur[x]; i != -1; i = edge[i].next)
    {
        Edge &E = edge[i];
        if(dist[E.to] == dist[x] + 1 && (f = DFS(E.to, min(E.cap - E.flow, a), end)) > 0)
        {
            E.flow += f;
            edge[i^1].flow -= f;
            flow += f;
            a -= f;
            if(a == 0) break;
        }
    }
    return flow;
}
int Maxflow(int start, int end)
{
    int flow = 0;
    while(BFS(start, end))
    {
        memcpy(cur, head, sizeof(head));
        flow += DFS(start, INF, end);
    }
    return flow;
}
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d%d%d%d", &N, &M, &K, &F);
        input();//输入
        solve();//处理朋友关系
        int left = 0, right = N, mid, ans;
        while(right >= left)
        {
            mid = (left + right) / 2;
            init();
            getMap(mid);
            if(Maxflow(0, 3*N+1) == mid * N)
            {
                ans = mid;
                left = mid + 1;
            }
            else
                right = 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、付费专栏及课程。

余额充值