1 条题解

  • 0
    @ 2025-8-24 21:41:14

    自动搬运

    查看原文

    来自洛谷,原作者为

    avatar tcswuzb
    **

    搬运于2025-08-24 21:41:14,当前版本为作者最后更新于2018-08-14 10:08:49,作者可能在搬运后再次修改,您可在原文处查看最新版

    自动搬运只会搬运当前题目点赞数最高的题解,您可前往洛谷题解查看更多

    以下是正文


    匈牙利算法自带匹配方案

    献给所有匈牙利算法党

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<cstdlib>
    #include<string>
    #include<queue>
    #include<map>
    #include<stack>
    #include<list>
    #include<set>
    #include<deque>
    #include<vector>
    #include<ctime>
    #define ll long long
    #define inf 0x7fffffff
    #define N 500008
    #define IL inline
    #define D double
    #define U unsigned
    #define R register
    using namespace std;
    template<typename T>void read(T &a)
    {
        T x=0,f=1;char ch=getchar();
        while(!isdigit(ch))
        {
            if(ch=='-')f=0;ch=getchar();
        }
        while(isdigit(ch))
        {
            x=(x<<1)+(x<<3)+ch-'0';ch=getchar();
        }
        a=f?x:-x;
    }
    /*-------------OI使我快乐---------------------*/
    ll n,m,ans;
    ll to[N];
    struct node{
        ll to,nex;
    }e[N];
    ll x,y,tot;
    ll head[N];
    bool vis[N];
    void add(ll a,ll b)
    {
        e[++tot].to=b;
        e[tot].nex=head[a];
        head[a]=tot;
    }
    bool find(ll x)
    {
        ll xx;
        for(ll i=head[x];i;i=e[i].nex)
        {
            xx=e[i].to;
            if(!vis[xx])
            {
                vis[xx]=1;
                if(!to[xx]||find(to[xx]))
                {
                    to[xx]=x;
                    return 1;
                }
            }
        }
        return 0;
    }
    int main()
    {
        read(n);read(m);
        read(x);read(y);
        while(x!=-1&&y!=-1)
        {
            if(x<=n&&y<=m) add(x,y);
            read(x);read(y);
        }
        for(ll i=1;i<=n;i++)
        {
            memset(vis,0,sizeof(vis));
            if(find(i)) ans++;
        }
        cout<<ans<<endl;
        for(ll i=n+1;i<=m;i++)
        {
            if(to[i]) cout<<to[i]<<" "<<i<<endl;
        }
        return 0;
    }
    

    最后对应输出即可

    • 1

    信息

    ID
    1785
    时间
    1000ms
    内存
    125MiB
    难度
    4
    标签
    递交数
    0
    已通过
    0
    上传者