poj 2239/caioj 1123(二分图最大匹配)

poj 2239
裸二分图,第一眼还没看出来
左边点为课程右边点为时间即可
注意二分图的初始化,$lk, vis$数组不是左边的个数,$G$才是左边的个数

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
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define ms(i, j) memset(i, j, sizeof i)
#define LL long long
using namespace std;
const int MAXN = 300 + 5;
vector<int> G[MAXN];
int cnt, n, lk[MAXN], vis[MAXN];
int getDayByDate(int p, int q) {
return (p - 1) * 12 + q;
}
bool hungary(int u) {
for (int i = 0; i < (int)G[u].size(); i++) {
int v = G[u][i];
if (vis[v] != cnt) {
vis[v] = cnt;
if (!lk[v] || hungary(lk[v])) {
lk[v] = u;
return true;
}
}
}
return false;
}
void clean() {
for (int i = 1; i <= max(n, 7 * 12); i++) lk[i] = 0, vis[i] = 0, G[i].clear();
}
void solve() {
clean();
for (int i = 1; i <= n; i++) {
int t;
scanf("%d", &t);
for (int j = 1; j <= t; j++) {
int p, q;
scanf("%d%d", &p, &q);
G[i].push_back(getDayByDate(p, q));
}
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (hungary(cnt = i)) ans++;
}
printf("%d\n", ans);
}
int main() {
while (scanf("%d", &n) == 1) solve();
return 0;
}
------ 本文结束 ------