UVa 1345 - Jamie's Contact Groups

链接

传送门

题意

\(n, (1 \leq n \leq 1000)\)个人,\(m, (1 \leq m \leq 500)\)个组,一个人可能属于多个组。现在从一些组中删除一部分人,使得每个人仅在一个组中,且包含人数最多的组的人数最少。

思路

源点向每个人连一条容量为1的边,人和组之间连容量为1的边,组与汇点之间的边的容量为最大人数。二分最大人数,判断最大流是否等于\(n\)

代码

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;

typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = 1510;

struct Edge {
int from, to, cap, flow;
Edge() {}
Edge(int u, int v, int c, int f): from(u), to(v), cap(c), flow(f) {}
};

struct ISAP {
int n, m, s, t;
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int p[maxn];
int d[maxn];
int num[maxn];
int cur[maxn];

void AddEdge(int from, int to, int cap) {
edges.push_back(Edge(from, to, cap, 0));
edges.push_back(Edge(to, from, 0, 0));
m = int(edges.size());
G[from].push_back(m - 2);
G[to].push_back(m - 1);
}

void ClearAll(int n) {
this -> n = n;
for (int i = 0; i < n; ++i) {
G[i].clear();
}
edges.clear();
}

bool BFS() {
memset(vis, 0, sizeof vis);
vis[t] = true;
d[t] = 0;
queue<int> Q;
Q.push(t);
while (!Q.empty()) {
int x = Q.front();
Q.pop();
for (int i = 0; i < G[x].size(); ++i) {
Edge& e = edges[G[x][i] ^ 1];
if (!vis[e.from] && e.cap > e.flow) {
vis[e.from] = true;
d[e.from] = d[x] + 1;
Q.push(e.from);
}
}
}
return vis[s];
}

int Augment() {
int x = t, a = inf;
while (x != s) {
Edge& e = edges[p[x]];
a = min(a, e.cap - e.flow);
x = e.from;
}
x = t;
while (x != s) {
edges[p[x]].flow += a;
edges[p[x] ^ 1].flow -= a;
x = edges[p[x]].from;
}
return a;
}

int Maxflow(int s, int t) {
this -> s = s, this -> t = t;
int flow = 0;
BFS();
memset(num, 0, sizeof num);
for (int i = 0; i < n; ++i) {
++num[d[i]];
}
memset(cur, 0, sizeof cur);
int x = s;
while (d[s] < n) {
if (x == t) {
flow += Augment();
x = s;
}
bool ok = false;
for (int i = cur[x]; i < G[x].size(); ++i) {
Edge& e = edges[G[x][i]];
if (e.cap > e.flow && d[x] == d[e.to] + 1) {
ok = true;
p[e.to] = G[x][i];
cur[x] = i;
x = e.to;
break;
}
}
if (!ok) {
int m = n - 1;
for (int i = 0; i < G[x].size(); ++i) {
Edge& e = edges[G[x][i]];
if (e.cap > e.flow) {
m = min(m, d[e.to]);
}
}
if (--num[d[x]] == 0) {
break;
}
++num[d[x] = m + 1];
cur[x] = 0;
if (x != s) {
x = edges[p[x]].from;
}
}
}
return flow;
}

void ClearFlow() {
for (int i = 0; i < m; ++i) {
edges[i].flow = 0;
}
}
} isap;

int main() {
int n, m;
while (~scanf("%d%d", &n, &m) && (n || m)) {
isap.ClearAll(n + m + 5);
for (int i = 0; i < n; ++i) {
scanf("%*s");
isap.AddEdge(n + m, i, 1);
while (getchar() != '\n') {
int x;
scanf("%d", &x);
isap.AddEdge(i, n + x, 1);
}
}
int L = 1, R = n, x = isap.m;
for (int i = 0; i < m; ++i) {
isap.AddEdge(n + i, n + m + 1, inf);
}
int ans = n;
while (L <= R) {
int M = L + (R - L) / 2;
isap.ClearFlow();
for (int i = x; i < isap.m; i += 2) {
isap.edges[i].cap = M;
}
if (isap.Maxflow(n + m, n + m + 1) == n) {
R = M - 1;
ans = min(ans, M);
} else {
L = M + 1;
}
}
printf("%d\n", ans);
}
return 0;
}