类似二分图匹配,建立网络图——将S和食物连,T和饮料连,将每只牛拆成X和X‘,两个点之间连,然后X和对应的食物连,X‘和对应的饮料连,然后构图完毕,上sap,然后还要submit一下才能AC哦QAQ
1 type
2 point=^node;
3 node=record
4 g,w:longint;
5 next,anti:point;
6 end;
7 var
8 i,j,k,l,m,n,t,a1,a2,a3,a4,s,ans:longint;
9 a:array[0..10000] of point;
10 d,dv:array[0..10000] of longint;
11 function min(x,y:longint):longint;inline;
12 begin
13 if x<y then min:=x else min:=y;
14 end;
15 procedure add(x,y,z:longint);inline;
16 var p:point;
17 begin
18 new(p);p^.g:=y;p^.w:=z;p^.next:=a[x];a[x]:=p;
19 new(p);p^.g:=x;p^.w:=0;p^.next:=a[y];a[y]:=p;
20 a[x]^.anti:=a[y];a[y]^.anti:=a[x];
21 end;
22 function dfs(x,flow:longint):longint;inline;
23 var p:point;k:longint;
24 begin
25 if x=t then exit(flow);
26 p:=a[x];dfs:=0;
27 while p<>nil do
28 begin
29 if (d[x]=(d[p^.g]+1)) and (p^.w>0) then
30 begin
31 k:=dfs(p^.g,min(flow-dfs,p^.w));
32 dec(p^.w,k);
33 inc(p^.anti^.w,k);
34 inc(dfs,k);
35 if dfs=flow then exit;
36 end;
37 p:=p^.next;
38 end;
39 if d[s]=n then exit;
40 dec(dv[d[x]]);
41 if dv[d[x]]=0 then d[s]:=n;
42 inc(d[x]);inc(dv[d[x]]);
43 end;
44 begin
45 readln(n,m,t);
46 for i:=1 to m+n+t+2 do a[i]:=nil;
47 for i:=1 to n do add(1+m+i,1+n+m+i,1);
48 for i:=1 to m do add(1,1+i,1);
49 for i:=1 to t do add(1+n+n+m+i,n+n+m+t+2,1);
50 for i:=1 to n do
51 begin
52 read(k,l);
53 for j:=1 to k do
54 begin
55 read(a1);
56 add(1+a1,1+m+i,1);
57 end;
58 for j:=1 to l do
59 begin
60 read(a1);
61 add(1+m+n+i,1+m+n+n+a1,1);
62 end;
63 readln;
64 end;
65 s:=1;t:=n+n+m+t+2;n:=n+n+m+t+2;
66 ans:=0;
67 fillchar(d,sizeof(d),0);
68 fillchar(dv,sizeof(dv),0);
69 dv[0]:=n;
70 while d[s]<n do inc(ans,dfs(s,maxlongint));
71 writeln(ans);
72 readln;
73 end.