https://www.luogu.org/problem/P3386
#include<cstdio> #include<vector> using namespace std; vector<int>e[2005]; int vis[2005]; int t; int link[2005]; int find(int x) { for(int i=0;i<e[x].size();i++) { int y=e[x][i]; if(vis[y]!=t) { vis[y]=t; if(link[y]==0||find(link[y])) { link[y]=x; return 1; } } } return 0; } int main() { int n,m,q; scanf("%d%d%d",&n,&m,&q); while(q--) { int x,y; scanf("%d%d",&x,&y); if(x<1||x>n||y<1||y>m)continue; e[x].push_back(y); //e[y].push_back(x); } int ans=0; for(int i=1;i<=n;i++) { t++; if(find(i)) { ans++; } } printf("%d\n",ans); return 0; }