1 条题解

  • 0
    @ 2025-10-8 16:57:09
    #include<bits/stdc++.h> 
    using namespace std;
    const int N=210;
    int n1,n2,m,match[N],chw[N],tsp,f[N][N];
    vector<int>G[N];
    bool findmuniu(int x)
    {
        for(int y:G[x])
        {
            if(chw[y]!=tsp)
            {
                chw[y]=tsp;
                if((match[y]==0)||(findmuniu(match[y])==1))
                {
                    match[y]=x;
                    return 1;
                }
            }
        }
        return 0;
    }
    int main()
    {
        scanf("%d%d%d",&n1,&n2,&m);
        memset(f,0,sizeof(f));
        for(int i=1;i<=m;i++)
        {
            int x,y;scanf("%d%d",&x,&y);
            f[x][y]=1;//f二维数组的第x行第y列为1,表示第x只公牛和第y只母牛不能匹配
        }
        for(int i=1;i<=n1;i++)
            for(int j=1;j<=n2;j++)
                if(f[i][j]==0)
                    G[i].emplace_back(j);
        int ans=0;
        memset(match,0,sizeof(match));
        memset(chw,0,sizeof(chw));
        for(int i=1;i<=n1;i++)
        {
            tsp=i;//tsp记录当前是第i轮找母牛的活动
            if(findmuniu(i))ans++;
        }
        printf("%d",ans);
        return 0;
    }
    
    • 1

    *【二分图:最大匹配】車的放置

    信息

    ID
    1461
    时间
    1000ms
    内存
    64MiB
    难度
    6
    标签
    递交数
    257
    已通过
    70
    上传者