菜鸡开始学习网络流
https://loj.ac/problem/6001
#include <iostream>
#include <cstring>
#include <queue>
#include <stdio.h>
using namespace std;
const int maxn = 2000;
int tot=0;
int head[maxn];
struct edge{
int v,nex,w;
}e[maxn*2];
void addedge(int u,int v,int w){
e[tot] = (edge){v,head[u],w};
head[u] = tot++;
e[tot] = (edge){u,head[v],0};
head[v] = tot++;
}
int deep[maxn];
bool bfs(int S,int T){
queue<int> q;
memset(deep,0,sizeof(deep));
deep[S] = 1;
q.push(S);
while(!q.empty()){
int now = q.front();
q.pop();
for(int i=head[now];i!=-1;i=e[i].nex){
int v = e[i].v;
int w = e[i].w;
if(w<=0 || deep[v]!=0) continue;
deep[v] = deep[now]+1;
q.push(v);
}
}
return deep[T];
}
int dfs(int now,int T,int maxflow){
if(now==T) return maxflow;
int all = 0;
for(int i=head[now];i!=-1 && all<maxflow;i=e[i].nex){
int v = e[i].v;
int w = e[i].w;
if(deep[v]!=deep[now]+1 || w<=0) continue;
int tt = dfs(v,T,min(maxflow-all,w));
e[i].w-=tt;
e[i^1].w+=tt;
all+=tt;
}
return all;
}
int dinic(int S,int T){
int ret = 0;
while(bfs(S,T)){
ret+=dfs(S,T,0x3f3f3f3f);
}
return ret;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
memset(head,-1,sizeof(head));
int u,v;
while(~scanf("%d%d",&u,&v)){
addedge(u,v,1);
}
for(int i=1;i<=m;i++){
addedge(0,i,1);
}
for(int i=m+1;i<=n;i++){
addedge(i,n+1,1);
}
int ans = dinic(0,n+1);
printf("%d\n",ans);
return 0;
}