Luo Gu P1894 [USACO4.2] the perfect solution to a problem bullpen The Perfect Stall

topic

Bipartite graph maximum matching problem

Now an array of standard cow in cattle byre is the date of cattle

Each looking to be emptied vis array

If there are two possible cases

1. The cowshed no cattle

2. The cattle byre where you can go to another bullpen

According to this recursive

Code:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 250;
int cow[N], m, ans, n, p, q; 
bool mp[N][N], vis[N];
int read() {
    int s = 0, w = 1;
    char ch = getchar();
    while(!isdigit(ch)) {if(ch == '-') w = -1; ch = getchar();}
    while(isdigit(ch)) {s = s * 10 + ch - '0'; ch = getchar();}
    return s * w;
}
bool find(int x) {
    for(int i = 1; i <= m; i++) {
        if(mp[x][i] && !vis[i]) {
            vis[i] = 1; 
            if(!cow[i] || find(cow[i])) {cow[i] = x; return 1;}
        }
    }
    return 0;
}
int main() {
    n = read(), m = read();
    for(int i = 1; i <= n; i++) {
        p = read();
        for(int j = 1; j <= p; j++)
            q = read(), mp[i][q] = 1;
    }
    for(int i = 1; i <= n; i++) {
        memset(vis, 0, sizeof(vis));
        if(find(i)) ans++;
    }
    printf("%d\n", ans);
    return 0;
}

Thank you for watching, I wish good health!

Guess you like

Origin www.cnblogs.com/yanxiujie/p/11708095.html