题目
https://www.luogu.org/problem/show?pid=1231
题解
上面是练习册,中间是书(拆点限流),下面是答案。做最大流。
代码
//最大流
#include <cstdio>
#include <algorithm>
#include <cstring>
#define maxn 300000
#define inf 0x3f3f3f3f
using namespace std;=
int head[maxn], next[maxn], to[maxn], c[maxn], d[maxn], num[maxn], Exit, N1, N2, N3, M1,
M2, S, T, tot=1, last[maxn];
void adde(int a, int b, int cc){to[++tot]=b;c[tot]=cc;next[tot]=head[a];head[a]=tot;}
void adde2(int a, int b, int cc){adde(a,b,cc),adde(b,a,0);}
int isap(int pos, int in)
{
int flow=0, p, t;
if(pos==T)return in;
for(p=last[pos];p;last[pos]=p=next[p])
if(c[p] and d[to[p]]+1==d[pos])
{
flow+= t=isap(to[p],min(c[p],in-flow));
c[p]-=t, c[p xor 1]+=t;
if(Exit or in==flow)return flow;
}
Exit=--num[d[pos]]==0;
++num[++d[pos]];
last[pos]=head[pos];
return flow;
}
void build()
{
int i, x, y;
scanf("%d%d%d",&N1,&N2,&N3);
S=N2+N1*2+N3+10;T=S+1;
scanf("%d",&M1);
for(i=1;i<=M1;i++)scanf("%d%d",&x,&y),adde2(y,N2+x,1);
scanf("%d",&M2);
for(i=1;i<=M2;i++)scanf("%d%d",&x,&y),adde2(N2+x+N1,N2+N1*2+y,1);
for(i=1;i<=N2;i++)adde2(S,i,1);
for(i=1;i<=N1;i++)adde2(N2+i,N2+i+N1,1);
for(i=1;i<=N3;i++)adde2(N2+N1*2+i,T,1);
}
int main()
{
int ans=0;
build();
while(!Exit)ans+=isap(S,inf);
printf("%d",ans);
return 0;
}