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): 1721    Accepted Submission(s): 502



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
 
题意:跟HDU 3081差不多,就是多了一个条件,每个女孩可以另外选K个不喜欢的男生。

分析:既然题目就多了一个条件,那么建图的话自然和上一题差不多,【 点击这里查看HDU 3081构图法】,只要把每个女生拆成两个女生girl_1,girl_2,若girl_1对boy_1不排斥,则连边gilr_1 -> boy_1,容量为1;若girl_1不喜欢boy_1,则连边girl_2 -> boy_1,容量为1,girl_1 -> girl_2,容量为K。然后二分找答案,最大流判可行度即可。

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3277

代码清单:
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<cctype>
#include<string>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;

#define end() return 0

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;

const int maxN = 250 + 5;
const int maxn = 1000 + 5;
const int maxv = 100000 + 5;
const int INF = 0x7f7f7f7f;

struct Edge{
    int from,to,cap,flow;
    Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};

struct dinic{
    int n,m,s,t; //结点数,边数(包括反向弧),源点,汇点
    vector<Edge>edge;//边表。edge[e]和edge[e^1]互为反向弧
    vector<int>G[maxn];//邻接表。G[i][j]表示结点i的第j条边在e数组的序号
    bool vis[maxn]; //bfs用
    int d[maxn]; //从起点到i的距离
    int cur[maxn]; //当前弧下标

    void init(int n,int s,int t){
        this -> n = n;
        this -> s = s;
        this -> t = t;
        for(int i=0;i<=n;i++) G[i].clear();
        edge.clear();
    }

    void addEdge(int from,int to,int cap){
        edge.push_back(Edge(from,to,cap,0));
        edge.push_back(Edge(to,from,0,0));
        m=edge.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }

    bool bfs(){
        memset(vis,false,sizeof(vis));
        queue<int>q;
        q.push(s);
        d[s]=0;
        vis[s]=true;
        while(!q.empty()){
            int x=q.front();q.pop();
            for(int i=0;i<G[x].size();i++){
                Edge& e=edge[G[x][i]];
                if(!vis[e.to]&&e.cap>e.flow){ //只考虑残量网络中的弧
                    vis[e.to]=true;
                    d[e.to]=d[x]+1;
                    q.push(e.to);
                }
            }
        }
        return vis[t];
    }

    int dfs(int x,int a){
        if(x==t||a==0) return a;
        int flow=0,f;
        for(int& i=cur[x];i<G[x].size();i++){ // & -> 从上次考虑的弧
            Edge& e=edge[G[x][i]];
            if(d[x]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0){
                e.flow+=f;
                edge[G[x][i]^1].flow-=f;
                flow+=f;
                a-=f;
                if(a==0) break;
            }
        }
        return flow;
    }

    int maxflow(){
        int flow=0;
        while(bfs()){
            memset(cur,0,sizeof(cur));
            flow+=dfs(s,INF);
        }
        return flow;
    }
};

int T;
int N,M,K,F,X,Y;
int a[maxv],b[maxv];
bool vis[maxN][maxN];

int tail;
dinic dc;

int father[maxN];
int Find(int x){ return father[x]!=x ? father[x]=Find(father[x]) : father[x]; }

void init(){
    for(int i=0;i<maxN;i++){
        father[i]=i;
    }
    memset(vis,false,sizeof(vis));
}

void input(){
    scanf("%d%d%d%d",&N,&M,&K,&F);
    for(int i=1;i<=M;i++){
        scanf("%d%d",&a[i],&b[i]);
    }
    for(int i=0;i<F;i++){
        scanf("%d%d",&X,&Y);
        father[Find(Y)]=Find(X);
    }
}

int work(){

    for(int i=1;i<=M;i++){
        for(int j=1;j<=N;j++){
            if(Find(j)==Find(a[i])&&!vis[j][b[i]]){ //判重
                vis[j][b[i]]=true;
            }
        }
    }

    tail=3*N+1;
    int le=0,ri=N;
    int mid,ans=0;

    while(le<=ri){
        mid=(le+ri)>>1;
        dc.init(tail+1,0,tail);

        for(int i=1;i<=N;i++){

            dc.addEdge(0,i,mid);
            dc.addEdge(i,i+2*N,K);
            dc.addEdge(i+N,tail,mid);

            for(int j=1;j<=N;j++){
                if(!vis[i][j])
                    dc.addEdge(i+2*N,j+N,1);
                else
                    dc.addEdge(i,j+N,1);
            }
        }

        int flow=dc.maxflow();
        if(flow==mid*N){
            ans=mid;
            le=mid+1;
        }
        else ri=mid-1;
    }
    return ans;
}

void solve(){
    printf("%d\n",work());
}

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



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值