luogu P2756 飞行员配对方案问题 网络流24

匈牙利:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<iomanip>
#include<cstring>
using namespace std;
typedef long long ll;
const int N=4004;
int read()
{
    int res=0,ch,flag=0;
    if((ch=getchar())=='-')             //判断正负
        flag=1;
    else if(ch>='0'&&ch<='9')           //得到完整的数
        res=ch-'0';
    while((ch=getchar())>='0'&&ch<='9')
        res=res*10+ch-'0';
    return flag?-res:res;
}
int n,m;
int result[N],use[N];
int e[N*16],ne[N*16],idx,h[N*16];
void add(int a,int b)
{
    e[idx]=b;
    ne[idx]=h[a];
    h[a]=idx++;
}

bool dfs(int now)
{

    for(int j=h[now]; ~j; j=ne[j])
    {
        int v=e[j];
        if(!use[v])
        {
            use[v]++;
            if(!result[v]||dfs(result[v]))
            {
                result[v]=now;
                return true;
            }
        }
    }
    return false;
}
int main()
{
    idx=0;
    memset(h,-1,sizeof h);
    m=read(),n=read();
    int a,b;
    a=read(),b=read();
    while(a!=-1&&b!=-1)
    {
        add(a,b);
        a=read(),b=read();
    }
    int ans=0;
    for(int i=1; i<=m; i++)
    {
        memset(use,0,sizeof(use));
        if(dfs(i))
            ans++;
    }
    cout<<ans<<endl;
    for(int i=m+1; i<=n; i++)
        if(result[i])
            cout<<result[i]<<" "<<i<<endl;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/QingyuYYYYY/p/13167717.html