zoukankan      html  css  js  c++  java
  • HDU 3277 最大流+二分

    Marriage Match III

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


    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
     
    Author
    starvae
     
    Source
     题意:
    有n个女生n个男生,每个女生可以和她喜欢的男生配对也可以和她的朋友喜欢的男生配对还可以和不超过k个不喜欢的男生配对,当所有的人都配对了时游戏结束,要求每一轮游戏中互相已经配对的两个人以后就不能再配对了问可以进行多少轮游戏。
    输入t组数据
    输入n,m,k,f,表示n对人,m个喜欢关系,f个朋友关系
    输入m行a b 表示女生a喜欢男生b
    输入f行a b 表示女生a和女生b是朋友
    代码:
    //和上一道题类似,只不过是加了每个女生可以选k个不喜欢的男生这一条件,类似上一题建图,然后把女生拆点容量为k
    //拆点后的点连向她不喜欢的男生容量为1,然后就一样了。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<vector>
    #include<queue>
    using namespace std;
    const int maxn=1000;
    const int inf=0x7fffffff;
    int mp[maxn][maxn],fat[maxn];
    int find(int x){
        return fat[x]==x?x:fat[x]=find(fat[x]);
    }
    void connect(int x,int y){
        int xx=find(x),yy=find(y);
        if(xx!=yy) fat[yy]=xx;
    }
    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>edges;
        vector<int>g[maxn];
        bool vis[maxn];
        int d[maxn];
        int cur[maxn];
        void init(int n){
            this->n=n;
            for(int i=0;i<n;i++) g[i].clear();
            edges.clear();
        }
        void Addedge(int from,int to,int cap){
            edges.push_back(Edge(from,to,cap,0));
            edges.push_back(Edge(to,from,0,0));//反向弧
            m=edges.size();
            g[from].push_back(m-2);
            g[to].push_back(m-1);
        }
        bool Bfs(){
            memset(vis,0,sizeof(vis));
            queue<int>q;
            q.push(s);
            d[s]=0;
            vis[s]=1;
            while(!q.empty()){
                int x=q.front();q.pop();
                for(int i=0;i<(int)g[x].size();i++){
                    Edge &e=edges[g[x][i]];
                    if(!vis[e.to]&&e.cap>e.flow){
                        vis[e.to]=1;
                        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<(int)g[x].size();i++){
                Edge &e=edges[g[x][i]];
                if(d[x]+1==d[e.to]&&(f=Dfs(e.to,min(a,e.cap-e.flow)))>0){
                    e.flow+=f;
                    edges[g[x][i]^1].flow-=f;
                    flow+=f;
                    a-=f;
                    if(a==0) break;
                }
            }
            return flow;
        }
        int Maxflow(int s,int t){
            this->s=s;this->t=t;
            int flow=0;
            while(Bfs()){
                memset(cur,0,sizeof(cur));
                flow+=Dfs(s,inf);
            }
            return flow;
        }
    }dc;
    bool solve(int k,int n,int mid){
        dc.init(3*n+2);
        for(int i=1;i<=n;i++){
            dc.Addedge(0,i,mid);
            dc.Addedge(i,i+n,k);
            for(int j=2*n+1;j<=3*n;j++){
                if(mp[i][j])
                    dc.Addedge(i,j,1);
                else dc.Addedge(i+n,j,1);
            }
            dc.Addedge(i+2*n,3*n+1,mid);
        }
        return n*mid==dc.Maxflow(0,3*n+1);
    }
    int main()
    {
        int t,n,m,k,f;
        scanf("%d",&t);
        while(t--){
            scanf("%d%d%d%d",&n,&m,&k,&f);
            int a,b;
            memset(mp,0,sizeof(mp));
            for(int i=1;i<=n;i++) fat[i]=i;
            for(int i=1;i<=m;i++){
                scanf("%d%d",&a,&b);
                mp[a][b+2*n]=1;
            }
            for(int i=1;i<=f;i++){
                scanf("%d%d",&a,&b);
                connect(a,b);
            }
            for(int i=1;i<=n;i++){
                for(int j=i+1;j<=n;j++){
                    if(find(i)==find(j))
                        for(int k=2*n+1;k<=3*n;k++)
                            mp[i][k]=mp[j][k]=(mp[i][k]||mp[j][k]);
                }
            }
            int l=0,r=n,mid,ans=0;
            while(l<=r){
                mid=(l+r)/2;
                if(solve(k,n,mid)){
                    ans=mid;
                    l=mid+1;
                }
                else r=mid-1;
            }
            printf("%d
    ",ans); 
        }
        return 0;
    }
  • 相关阅读:
    Mac OS terminal 查看内存使用情况的命令
    【教程】5分钟在以太坊上发行自己的代币
    Mac下eclipse的快捷键
    Mac上编辑文本的一些实用快捷键:跳转、选中等操作
    Mysql数据库用source命令导入SQL文件,利用navicat修改MySQL用户root密码,忘记mysql密码如何修改
    比特币1个确认和6个确认的区别在哪里?什么是双花问题?
    以 1、3、bc1 开头的比特币地址都有啥区别?转账手续费那种便宜,隔离见证地址为什么手续费便宜?
    以太坊 2.0 中的 DeFi:城市、郊区、农村
    Mac版navicat生成ER图把表结构导出pdf
    下载文件(API接口,Angularjs前端)
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/6933913.html
Copyright © 2011-2022 走看看