forked from yogykwan/acm-challenge-workbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoj1466.cpp
64 lines (57 loc) · 1.48 KB
/
poj1466.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
63
64
/*
* POJ 1466: Girls and Boys
* 题意:学生包括男女,某些男女间存在恋爱关系,求任意两人间无恋爱关系的最大的集合。
* 类型:二分匹配
* 算法:二分图中,最大匹配=最小顶点覆盖数。一般图中,最小顶点覆盖数+最大独立集=顶点总数。本题因为没有给出男女,可以让每个学生和自己的副本存在二分图左右两侧,最大匹配/2即为原二分图中的最大匹配。顶点数减去原最大匹配即为所求最大独立子集。
*/
#include <cstdio>
#include <cstring>
using namespace std;
bool e[510][510];
bool vis[510];
int rec[510];
int n1, n2;
bool Dfs(int u) {
for (int i = 0; i < n2; ++i) {
if (e[u][i] && !vis[i]) {
vis[i] = 1;
if (rec[i] == -1 || Dfs(rec[i])) {
rec[i] = u;
return true;
}
}
}
return false;
}
int Hungary() {
memset(rec, -1, sizeof(rec));
int ans = 0;
for (int i = 0; i < n1; ++i) {
memset(vis, 0, sizeof(vis));
if (Dfs(i)) {
++ans;
}
}
return ans;
}
struct Slide {
int x1, x2, y1, y2;
} slide[30];
int main() {
// freopen("/Users/yogy/acm-challenge-workbook/db.in", "r", stdin);
int n;
while (scanf("%d", &n) != EOF && n) {
n1 = n2 = n;
memset(e, 0, sizeof(e));
int i, j, t;
for (int k = 0; k < n; ++k) {
scanf("%d: (%d)", &i, &t);
while (t--) {
scanf("%d", &j);
e[i][j] = 1;
}
}
printf("%d\n", n - Hungary() / 2);
}
return 0;
}