HDU - 3081 Marriage Match II (网络流 +二分 )

HDU - 3081 Marriage Match II (网络流 +二分 )

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<nn,0<=f<n). n means there are 2n 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个女孩,每个女孩都有中意的男孩可以配对,另外女孩还有的是朋友。
如果女孩a和女孩b是朋友,并且女孩a可以和男孩c配对,那么b也可以和男孩c配对。
问:最多可把n个男女配对多少次。

输入 n m f n个点 m对男女配对,f对女孩朋友
m行 男女配对
f行 女互为朋友

解题思路:

用网络流做。
二分答案,然后把原点和女孩间连边,流量为二分的值,男孩和汇点连边,流量为2分的值。
女孩相互为朋友的用一个并查集维护,然后把一个并查集中的所有女生都连上他们能连的所有男生,流量为1.、
二分每次都得重新建图,重新跑最大流,如果是满流,也就是说流量为 n*mid 那么就说明这个值可以。一直二分得到答案。

注意:maxn不能太小,不然会T或者WA。(不知为何)

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 1100;
int n,m,sp,tp,f;
int jump;
int head[maxn],cnt = 0;
int deep[maxn];
int len ;
int cur[maxn];
int fa[maxn];
int vis[maxn][maxn];//看有没有边。
struct node{
    int to;
    int w;
    int next;
}side[maxn*10];
void init(){
    memset(head,-1,sizeof(head));
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++){
        fa[i] = i;
    }
    cnt = 0;
}
void add(int x,int y,int w){
    side[cnt].to = y;
    side[cnt].w = w;
    side[cnt].next = head[x];
    head[x] = cnt++;
}
bool bfs(){
    memset(deep,-1,sizeof(deep));
    queue<int> q;
    q.push(sp);
    deep[sp] = 0;
    while(q.size()){
        int now = q.front();
        q.pop();
        for(int i=head[now];i!=-1;i=side[i].next){
            int to = side[i].to;
            if(deep[to]==-1&&side[i].w>0){
                deep[to] = deep[now]+1;
                q.push(to);
                if(to == tp) break;
            }
        }
    }
    return deep[tp]!=-1;
}
int dfs(int u,int cap){
    if(u==tp||cap==0) return cap;
    int res = 0;
    int f;
    for(int &i=cur[u];i!=-1;i=side[i].next){
        int to = side[i].to;
        if(deep[to]==deep[u]+1&&(f=dfs(to,min(side[i].w,cap-res)))>0){
            side[i].w-=f;
            side[i^1].w+=f;
            res+=f;
            if(res==cap) return cap;
        }
    }
    if(!res) deep[u] = -1;
    return res;
}
int Dinic(){
    int ans = 0;
    while(bfs()){
        for(int i = 0;i<=tp;i++) cur[i] = head[i];
        ans +=dfs(sp,inf);
    }
    return ans ;    
}
int find(int x){
    if(x == fa[x]) return x;
    else{
        return fa[x] = find(fa[x]);
    }
}
void merge(int x,int y){
    int fx = find(x);
    int fy = find(y);
    if(fx!=fy){
        fa[fx] = fy;
    }
}
bool can(int k){
    //初始化
    memset(head,-1,sizeof(head));
    cnt = 0;
    sp = 0,tp = 2*n+1;
	//原点连女孩  男孩连汇点 
    for(int i=1;i<=n;i++){
        add(0,i,k);
        add(i,0,0);
        add(i+n,2*n+1,k);
        add(2*n+1,i+n,0);
    }
	//男女 
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(vis[i][j]){
                add(i,j+n,1);
                add(j+n,i,0);
               // cout<<"add "<<i<<"  "<<j+n<<endl;
            }
        }
    }
    int res = Dinic();
    if(res == k*n) return true;
    else return false;
}
int main(){
    int t;
    cin>>t;
    while(t--){
        scanf("%d%d%d",&n,&m,&f);
        init();
        int u,v;
        while(m--){
            scanf("%d%d",&u,&v);
            vis[u][v] =1;
        }
        while(f--){
            scanf("%d%d",&u,&v);
            merge(u,v);
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                if(find(i)==find(j)){
                    for(int k=1;k<=n;k++){
                   		if(vis[i][k]) vis[j][k] =1; 
                    }
                }
            }
        }
        int left =0;
        int right = n;
        int ans=0 ;
        while(left<=right){
            int mid = (left+right)/2;
            if(can(mid)){
                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、付费专栏及课程。

余额充值