记录编号 569430 评测结果 AAAAAAAAAA
题目名称 [USACO Open07]牛的进餐 最终得分 100
用户昵称 Gravataryrtiop 是否通过 通过
代码语言 C++ 运行时间 0.000 s
提交时间 2022-03-07 19:44:56 内存使用 0.00 MiB
显示代码纯文本
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 20005;
int ver[maxn << 1],nxt[maxn << 1],cap[maxn << 1],head[maxn];
int cnt = -1;
int cur[maxn],level[maxn];
queue <int> q; 
void add(int u,int v,int t) {
    nxt[++ cnt] = head[u];
    ver[cnt] = v;
    cap[cnt] = t;
    head[u] = cnt;
    return ;
}
void AddEdge(int u,int v) {
    add(u , v , 1);
    add(v , u , 0);
    return ;
}
int n;
bool bfs(int s,int t) {
    memset(level , 0 , sizeof(level));
    q.push(s);
    level[s] = 1;
    while(!q.empty()) {
        int u = q.front();
        q.pop();
        for(int i = head[u];~ i;i = nxt[i]) {
            int v = ver[i];
            if(!level[v]&&cap[i]) {
                level[v] = level[u] + 1;
                q.push(v);
            }
        }
    }
    return level[t] != 0;
}
int dfs(int x,int t,int maxflow) {
    if(x == t||!maxflow)return maxflow;
    int flow = 0,f;
    for(int& i = cur[x];~ i;i = nxt[i]) {
        int v = ver[i];
        if(level[v] == level[x] + 1&&(f = dfs(v , t , min(maxflow , cap[i])))) {
            if(!f) {
                level[v] = 0;
                break ;
            }
            flow += f;
            maxflow -= f;
            cap[i] -= f;
            cap[i ^ 1] += f;
            if(!maxflow)break;
        }
    }
    return flow;
}
int Dinic(int s,int t) {
    int flow = 0;
    while(bfs(s , t)) {
        memcpy(cur , head , sizeof(cur));
        flow += dfs(s , t , 0x3f3f3f3f);
    }
    return flow;
}
int S,T,tot,f[maxn],d[maxn],F,D,c1[maxn],c2[maxn];
int main() {
    freopen("usaco_open07_dining.in","r",stdin);
    freopen("usaco_open07_dining.out","w",stdout);
    scanf("%d%d%d",&n,&F,&D);
    memset(head , -1 , sizeof(head));
    S = ++ tot;
    T = ++ tot;
    for(int i = 1;i <= F;++ i)f[i] = ++ tot,AddEdge(S , f[i]);
    for(int i = 1;i <= D;++ i)d[i] = ++ tot,AddEdge(d[i] , T);
    for(int i = 1;i <= n;++ i)c1[i] = ++ tot,c2[i] = ++ tot,AddEdge(c1[i] , c2[i]);
    for(int i = 1;i <= n;++ i) {
        int x,y,z;
        scanf("%d%d",&x,&y);
        for(int j = 1;j <= x;++ j) {
            scanf("%d",&z);
            AddEdge(f[z] , c1[i]);
        }
        for(int j = 1;j <= y;++ j) {
            scanf("%d",&z);
            AddEdge(c2[i] , d[z]);
        }
    }
    printf("%d\n",Dinic(S , T));
    fclose(stdin);
    fclose(stdout);
    return 0;
}