二分图匹配【模板】
2019-05-22 14:03 一只弱鸡丶 阅读(276) 评论(0) 编辑 收藏 举报#include <cstdio> #include <cstring> #include <cmath> using namespace std; #define ll long long #define re register const int N=1005; inline void read(int &a) { a=0; int d=1; char ch; while(ch=getchar(),ch>'9'||ch<'0') if(ch=='-') d=-1; a=ch^48; while(ch=getchar(),ch>='0'&&ch<='9') a=(a<<3)+(a<<1)+(ch^48); a*=d; } bool vis[N][N],book[N]; int mata[N],matb[N],n,m; inline bool dfs(int u) { for(re int i=1;i<=m;i++) { if(book[i]==0&&vis[u][i]==1) { book[i]=1; if(matb[i]==0||dfs(matb[i])) { matb[i]=u; mata[u]=i; return 1; } } } return 0; } int main() { int e; read(n); read(m); read(e); for(re int i=1;i<=e;i++) { int a,b; read(a); read(b); if(a<1||b<1||a>n||b>m) continue; vis[a][b]=1; //vis[b][a]=1; } int ans=0; for(re int i=1;i<=n;i++) { memset(book,0,sizeof(book)); ans+=dfs(i); } printf("%d",ans); return 0; }