forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpianolessons.cpp
62 lines (54 loc) · 1.3 KB
/
pianolessons.cpp
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
61
62
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct BipartiteMatcher {
vector<vector<int>> G;
vector<int> L, R, Viz;
BipartiteMatcher(int n, int m) :
G(n), L(n, -1), R(m, -1), Viz(n) {}
void AddEdge(int a, int b) {
G[a].push_back(b);
}
bool Match(int node) {
if(Viz[node])
return false;
Viz[node] = true;
for(auto vec : G[node]) {
if(R[vec] == -1 || Match(R[vec])) {
L[node] = vec;
R[vec] = node;
return true;
}
}
return false;
}
int Solve() {
bool ok = true;
while(ok) {
ok = false;
fill(Viz.begin(), Viz.end(), 0);
for(int i = 0; i < L.size(); ++i)
if(L[i] == -1)
ok |= Match(i);
}
int ret = 0;
for(int i = 0; i < L.size(); ++i)
ret += (L[i] != -1);
return ret;
}
};
int main() {
int n, m;
cin >> n >> m;
BipartiteMatcher bm(n, m);
for(int i = 0; i < n; ++i) {
int size;
cin >> size;
for(int j = 0; j < size; ++j) {
int slot;
cin >> slot;
bm.AddEdge(i, slot-1);
}
}
cout << bm.Solve() << '\n';
}