bzoj1711
网络流
如果只有两个的话就是裸的二分图匹配
然而这里有三个元素
由于食物和饮料互相没有关系,只和牛有关系
那么我们把牛拆点x->y cap = 1
然后饮料和食物分别限制x y就行了
#include<bits/stdc++.h> using namespace std; const int N = 410, inf = 1e9; int rd() { int x = 0, f = 1; char c = getchar(); while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); } return x * f; } int n, m, k, source, sink, cnt = 1, tot; int a[N], d[N], head[N], iter[N], Map[N][N]; struct edge { int nxt, to, f; } e[N * N]; bool bfs() { queue<int> q; memset(d, -1, sizeof(d)); d[source] = 0; q.push(source); while(!q.empty()) { int u = q.front(); q.pop(); for(int i = head[u]; i; i = e[i].nxt) if(d[e[i].to] == -1 && e[i].f) { d[e[i].to] = d[u] + 1; q.push(e[i].to); } } return d[sink] != -1; } int dfs(int u, int delta) { if(u == sink) return delta; int ret = 0; for(int &i = iter[u]; i && delta; i = e[i].nxt) if(d[e[i].to] == d[u] + 1 && e[i].f) { int x = dfs(e[i].to, min(delta, e[i].f)); ret += x; delta -= x; e[i].f -= x; e[i ^ 1].f += x; } return ret; } int dinic() { int ret = 0; while(bfs()) { for(int i = source; i <= sink; ++i) iter[i] = head[i]; ret += dfs(source, inf); } return ret; } void link(int u, int v, int f) { e[++cnt].nxt = head[u]; head[u] = cnt; e[cnt].to = v; e[cnt].f = f; } void insert(int u, int v, int f) { link(u, v, f); link(v, u, 0); } int main() { scanf("%d%d%d", &n, &m, &k); sink = 2 * n + m + k + 1; for(int i = 1; i <= n; ++i) { insert(i, i + n, 1); int n1, n2; scanf("%d%d", &n1, &n2); while(n1--) { int x; scanf("%d", &x); insert(x + 2 * n, i, 1); } while(n2--) { int x; scanf("%d", &x); insert(i + n, x + 2 * n + m, 1); } } for(int i = 1; i <= m; ++i) insert(source, i + 2 * n, 1); for(int i = 1; i <= k; ++i) insert(i + 2 * n + m, sink, 1); printf("%d\n", dinic()); return 0; }