洛谷P2756 飞行员配对方案问题

二分图裸题,找他的最大匹配即可

#include<bits/stdc++.h>
using namespace std;
int n,m,ans;
const int N=1e6+7;
int to[N];
struct node
{
    int to,nex;
}e[N];
int x,y,tot;
int head[N];
bool vis[N];
void add(int a,int b)
{
    e[++tot].to=b;
    e[tot].nex=head[a];
    head[a]=tot;
}
bool dfs(int x)
{
    for(int i=head[x];i;i=e[i].nex)
    {
        int xx=e[i].to;
        if(!vis[xx])
        {
            vis[xx]=1;
            if(!to[xx]||dfs(to[xx]))
            {
                to[xx]=x;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    cin>>n>>m;
    cin>>x>>y;
    while(x!=-1&&y!=-1)
    {
        if(x<=n&&y<=m) add(x,y);
        cin>>x;cin>>y;
    }
    for(int i=1;i<=n;i++)
    {
        memset(vis,0,sizeof(vis));
        if(dfs(i)) ans++;
    }
    cout<<ans<<endl;
    for(int i=n+1;i<=m;i++)
    {
        if(to[i]) cout<<to[i]<<" "<<i<<endl;
    }
    return 0;
}

 

posted @ 2019-04-19 16:10  JBLee  阅读(228)  评论(0编辑  收藏  举报