水题啊,牛放食物饮料中间建边即可,当然牛要拆点啊,我怎么总是忘拆点啊。。
1 //Achen
2 #include<algorithm>
3 #include<iostream>
4 #include<cstring>
5 #include<cstdlib>
6 #include<vector>
7 #include<cstdio>
8 #include<queue>
9 #include<cmath>
10 #include<set>
11 #include<map>
12 #define Formylove return 0
13 #define For(i,a,b) for(int i=(a);i<=(b);i++)
14 #define Rep(i,a,b) for(int i=(a);i>=(b);i--)
15 const int N=160007;
16 typedef long long LL;
17 typedef double db;
18 using namespace std;
19 int n,m;
20
21 template<typename T>void read(T &x) {
22 char ch=getchar(); x=0; T f=1;
23 while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
24 if(ch=='-') f=-1,ch=getchar();
25 for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
26 }
27
28 struct edge {
29 int u,v,cap,fl,nx;
30 edge(){}
31 edge(int u,int v,int cap,int fl,int nx):u(u),v(v),cap(cap),fl(fl),nx(nx){}
32 }e[N];
33
34 int ecnt=1,fir[N];
35 void add(int u,int v,int cap) {
36 e[++ecnt]=edge(u,v,cap,0,fir[u]); fir[u]=ecnt;
37 //printf("%d->%d:%d
",u,v,cap);
38 e[++ecnt]=edge(v,u,0,0,fir[v]); fir[v]=ecnt;
39 }
40
41 queue<int>que;
42 int d[N];
43 void bfs(int s,int t) {
44 que.push(t);
45 For(i,1,n) d[i]=n;
46 d[t]=0;
47 while(!que.empty()) {
48 int x=que.front();
49 que.pop();
50 for(int i=fir[x];i;i=e[i].nx) {
51 int y=e[i].v;
52 if(d[y]==n&&e[i].cap==0) {
53 d[y]=d[x]+1;
54 que.push(y);
55 }
56 }
57 }
58 }
59
60 #define inf 1e9
61 int p[N];
62 int calc(int s,int t) {
63 int fl=inf;
64 for(int i=t;i!=s;i=e[p[i]].u)
65 fl=min(fl,e[p[i]].cap-e[p[i]].fl);
66 for(int i=t;i!=s;i=e[p[i]].u)
67 e[p[i]].fl+=fl,e[p[i]^1].fl-=fl;
68 return fl;
69 }
70
71 int c[N],cur[N];
72 int isap(int s,int t) {
73 For(i,0,n) c[i]=0;
74 bfs(s,t);
75 For(i,1,n) cur[i]=fir[i],c[d[i]]++;
76 int rs=0;
77 for(int x=s;d[x]<n;) {
78 if(x==t) {
79 rs+=calc(s,t);
80 x=s;
81 }
82 int ok=0;
83 for(int &i=cur[x];i;i=e[i].nx) if(e[i].cap>e[i].fl&&d[e[i].v]+1==d[x]) {
84 ok=1; p[x=e[i].v]=i; break;
85 }
86 if(!ok) {
87 int D=n; cur[x]=fir[x];
88 for(int i=fir[x];i;i=e[i].nx) if(e[i].cap>e[i].fl)
89 D=min(D,d[e[i].v]+1);
90 if(!(--c[d[x]])) break;
91 c[d[x]=D]++;
92 if(x!=s) x=e[p[x]].u;
93 }
94 }
95 return rs;
96 }
97
98 int dd[N];
99 vector<int>vc;
100 void init() {
101 ecnt=1;
102 memset(fir,0,sizeof(fir));
103 memset(dd,0,sizeof(dd));
104 }
105
106 int main() {
107 #ifdef ANS
108 freopen(".in","r",stdin);
109 freopen(".out","w",stdout);
110 #endif
111 read(n); int F,D;
112 read(F); read(D);
113 int s=n*2+1,t=n*2+2;
114 For(i,1,F) add(s,n*2+2+i,1);
115 For(i,1,D) add(n*2+2+F+i,t,1);
116 For(i,1,n) {
117 int F_i,D_i,x;
118 read(F_i); read(D_i);
119 For(j,1,F_i) {
120 read(x);
121 add(n*2+2+x,i,1);
122 }
123 add(i,i+n,1);
124 For(j,1,D_i) {
125 read(x);
126 add(n+i,n*2+2+F+x,1);
127 }
128 }
129 n=n*2+2+F+D;
130 printf("%d
",isap(s,t));
131 Formylove;
132 }