题目大意:有n个人和m个组,一个人可能属于多个组,现在请你从某些组中去掉几个人,使得每个人都只属于一个组,且人数最多的组中人员数目达到最小
解题思路:最大值最小化,二分
建图就比较简单了,二分主要二分组别到超级汇点的容量
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define N 2010
#define INF 0x3f3f3f3f
struct Edge{
int from, to, cap, flow;
Edge() {}
Edge(int from, int to, int cap, int flow) : from(from), to(to), cap(cap), flow(flow) {}
};
struct Dinic{
int n, m, s, t;
vector<Edge> edges;
vector<int> G[N];
bool vis[N];
int d[N], cur[N];
void init(int n) {
this->n = n;
for (int i = 0; i <= n; i++) {
G[i].clear();
}
edges.clear();
}
void AddEdge(int from, int to, int cap) {
edges.push_back(Edge(from, to, cap, 0));
edges.push_back(Edge(to, from, 0, 0));
int m = edges.size();
G[from].push_back(m - 2);
G[to].push_back(m - 1);
}
bool BFS() {
memset(vis, 0, sizeof(vis));
queue<int> Q;
Q.push(s);
vis[s] = 1;
d[s] = 0;
while (!Q.empty()) {
int u = Q.front();
Q.pop();
for (int i = 0; i < G[u].size(); i++) {
Edge &e = edges[G[u][i]];
if (!vis[e.to] && e.cap > e.flow) {
vis[e.to] = true;
d[e.to] = d[u] + 1;
Q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x, int a) {
if (x == t || a == 0)
return a;
int flow = 0, f;
for (int i = cur[x]; i < G[x].size(); i++) {
Edge &e = edges[G[x][i]];
if (d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0) {
e.flow += f;
edges[G[x][i] ^ 1].flow -= f;
flow += f;
a -= f;
if (a == 0)
break;
}
}
return flow;
}
int Maxflow(int s, int t) {
this->s = s; this->t = t;
int flow = 0;
while (BFS()) {
memset(cur, 0, sizeof(cur));
flow += DFS(s, INF);
}
return flow;
}
bool MinCut(int mid, int m, int n, int source, int sink) {
for (int i = 0; i < edges.size(); i++)
edges[i].flow = 0;
for (int i = 0; i < m; i++)
for (int j = 0; j < G[i].size(); j++)
if (edges[G[i][j]].to == sink)
edges[G[i][j]].cap = mid;
int t = Maxflow(source, sink);
if (t == n) {
return true;
}
return false;
}
};
Dinic dinic;
int n, m, source, sink;
int num[N], Max;
char name[30];
void init() {
source = n + m; sink = source + 1;
memset(num, 0, sizeof(num));
dinic.init(sink);
Max = -INF;
int t;
char c;
for (int i = 0; i < n; i++) {
dinic.AddEdge(source, m + i, 1);
scanf("%s", name);
c = getchar();
while (c != '\n') {
scanf("%d", &t);
dinic.AddEdge(m + i, t, 1);
num[t]++;
c = getchar();
}
}
for (int i = 0; i < m; i++) {
Max = max(Max, num[i]);
dinic.AddEdge(i, sink, num[i]);
}
}
void solve() {
// printf("Max is %d\n", Max);
int l = 0, r = Max, mid, ans = 0;
while (l <= r) {
mid = (l + r) / 2;
if (dinic.MinCut(mid, m, n, source, sink)) {
r = mid - 1;
ans = mid;
}
else l = mid + 1;
}
printf("%d\n", ans);
}
int main() {
while (scanf("%d%d", &n, &m) != EOF && n + m) {
init();
solve();
}
return 0;
}