题目
link
题解
由于每头奶牛只会和牛栏起关系,而让我们求最多能分配到多少牛栏,所以我们可以建立二分图
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| #include<bits/stdc++.h>
using namespace std;
const int N = 510; int head[N], nxt[50010], to[50010], cnt = 1; int n, m, e;
void add(int x, int y) { to[++cnt] = y; nxt[cnt] = head[x]; head[x] = cnt; }
int vistime[N], match[N];
bool dfs(int u, int vist) { if(vistime[u] == vist) return 0; vistime[u] = vist;
for(int i = head[u]; i; i = nxt[i]) { int v = to[i]; if(match[v] == 0 || dfs(match[v], vist)) { match[v] = u; return true; } } return 0; }
int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> m; for(int i = 1, v, s;i <= n;i++) { cin >> s; for(int j = 1;j <= s;j++) { cin >> v; add(i, v); } } int ans = 0; for(int i = 1;i <= n;i++) { if(dfs(i, i)) { ans ++; } } cout << ans; return 0; }
|