zoukankan      html  css  js  c++  java
  • Luogu P2756 [网络流24题]飞行员配对方案问题_二分图匹配题解

    二分图模板题

    我用的是匈牙利

    其实最大流也可以做

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #define MAX 200
    #define MAXL 200*200
    using namespace std;
    struct Line{
          int v,next;
    }e[MAXL];
    int h[MAX],cnt=0,sum=0,match[MAX],n,m,u,v;
    bool vis[MAX];
    void addedge(int u,int v){
        e[cnt]=(Line){v,h[u]};
        h[u]=cnt++;
    }
    bool dfs(int u){
        for(int i=h[u];i!=-1;i=e[i].next){
            int v=e[i].v;
            if(!vis[v]){
            	vis[v]=true;
            	if(!match[v] or dfs(match[v])){
               		match[v]=u;
               		return 1;
            	}
            }
        }
        return 0;
    }
    int main(){
        memset(h,-1,sizeof(h));
        scanf("%d%d",&n,&m);
        while(1){
            scanf("%d%d",&u,&v);
            if(u==-1)break;
            addedge(u,v);
        }
        for(int i=1;i<=n;i++){
            memset(vis,0,sizeof(vis));
            if(dfs(i))sum++;
        }
        cout<<sum<<endl;
        for(int i=n+1;i<=m;i++){
        	if(match[i]){
        		printf("%d %d
    ",match[i],i);
            }
        }
        return 0;
    }
    
  • 相关阅读:
    列表
    break和continue
    第三天下午
    第二天
    简历,面试
    周三&nbsp;景安
    应届生求职优势(我的求职心…

    2013年01月02日
    Click&nbsp;and&nbsp;Drag
  • 原文地址:https://www.cnblogs.com/ezoihy/p/8893815.html
Copyright © 2011-2022 走看看