USACO The Perfect Stall

Code

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
/*
ID: wcr19961
PROG: stall4
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;

const int maxn = 1010;
bool vis[maxn];
vector<int> g[maxn];
int n, from[maxn], tot;

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

int Hungarian() {
tot = 0;
memset(from, -1, sizeof from);
for(int i = 0; i < n; ++i) {
memset(vis,0,sizeof vis);
if(match(i)) {
++tot;
}
}
return tot;
}

int main() {
freopen("stall4.in", "r", stdin);
freopen("stall4.out", "w", stdout);
int m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
int x, u;
scanf("%d", &x);
while (x--) {
scanf("%d", &u);
g[i].push_back(n + u);
}
}
n += m;
printf("%d\n", Hungarian());
return 0;
}