1394. 完美牛棚

二分图的最大匹配,匈牙利算法裸题。

const int N = 210;
vector<int> g[N];
bool vis[N];
int match[N];
int n, m;

int find(int x)
{
    for(int i = 0; i < g[x].size(); i++)
    {
        int j = g[x][i];
        if(!vis[j])
        {
            vis[j] = true;
            if(match[j] == 0 || find(match[j]))
            {
                match[j] = x;
                return x;
            }
        }
    }
    return false;
}

int main()
{
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
    {
        int k;
        cin >> k;
        while(k--)
        {
            int x;
            cin >> x;
            g[i].pb(x);
        }
    }

    int res = 0;
    for(int i = 1; i <= n; i++)
    {
        memset(vis, 0, sizeof vis);
        if(find(i)) res++;
    }
    
    cout << res << endl;
    //system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/fxh0707/p/15019723.html