题目
思路
三分图匹配,不过看上去裸匈牙利应该能过吧,我们考虑网络流,但是由于一本书只能匹配一次,那么我们考虑裂点来做流量限制,然后直接跑dinic就好了。
代码实现
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
#define rep(i,f_start,f_end) for (int i=f_start;i<=f_end;++i)
#define per(i,n,a) for (int i=n;i>=a;i--)
#define MT(x,i) memset(x,i,sizeof(x) )
#define rev(i,start,end) for (int i=start;i<end;i++)
#define inf 0x3f3f3f3f
#define mp(x,y) make_pair(x,y)
#define lowbit(x) (x&-x)
#define MOD 1000000007
#define exp 1e-8
#define N 1000005
#define fi first
#define se second
#define pb push_back
typedef long long ll;
typedef pair<int ,int> PII;
typedef pair<int ,PII> PIII;
ll gcd (ll a,ll b) {return b?gcd (b,a%b):a; }
inline int read() {
char ch=getchar(); int x=0, f=1;
while(ch<'0'||ch>'9') {
if(ch=='-') f=-1;
ch=getchar();
} while('0'<=ch&&ch<='9') {
x=x*10+ch-'0';
ch=getchar();
} return x*f;
}
const int maxn=5e5+10;
int head[maxn],cnt=1;
struct edge {
int v,flow,next;
}e[maxn];
inline void add (int u,int v,int flow) {
e[++cnt]= (edge) {v,flow,head[u]};
head[u]=cnt;
}
inline void add_edge (int u,int v,int flow) {
add (u,v,flow);
add (v,u,0);
}
int dis[maxn],cur[maxn];
int s,t,n,m,c1,c2;
int bfs () {
MT (dis,0);
queue <int > q;
dis[s]=1;
q.push (s);
while (q.size ()) {
int x=q.front (); q.pop ();
for (int i=head[x];~i;i=e[i].next) {
if (dis[e[i].v]==0&&e[i].flow) {
dis[e[i].v]=dis[x]+1;
q.push (e[i].v);
}
}
}
return dis[t];
}
int dfs (int now,int nowflow) {
if (now==t) return nowflow;
for (int &i=cur[now];i!=-1;i=e[i].next) {
if (dis[e[i].v]==dis[now]+1&&e[i].flow) {
int canflow=dfs (e[i].v,min (e[i].flow,nowflow));
if (canflow) {
e[i].flow-=canflow;
e[i^1].flow+=canflow;
return canflow;
}
}
}
return 0;
}
int ans=0;
int x,y;
void Dinic () {
while (bfs ()) {
memcpy (cur,head,sizeof (cur));
while (int val=dfs (s,inf)) ans+=val;
}
}
int n1,n2,n3,m1,m2;
int main () {
MT (head,-1);
scanf ("%d%d%d",&n1,&n2,&n3);
int num=2*n1+n2+n3;
scanf ("%d",&m1);
s=0,t=num+1;
rep (i,n3+1,n1+n3) add_edge (i,i+n1,1);
rep (i,1,m1) {
int x,y;
scanf ("%d%d",&x,&y);
add_edge (n3+x+n1,y+2*n1+n3,inf);
}
scanf ("%d",&m2);
rep (i,1,m2) {
scanf ("%d%d",&x,&y);
add_edge (y,n3+x,inf);
}
rep (i,1,n3) add_edge (s,i,1);
rep (i,1,n2) add_edge (2*n1+n3+i,t,1);
Dinic ();
cout<<ans<<endl;
return 0;
}