码迷,mamicode.com
首页 > 其他好文 > 详细

【模板】二分图匹配

时间:2018-03-01 21:46:45      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:int   printf   bool   二分图   turn   getch   highlight   tchar   amp   

#include<cstdio>
#include<algorithm>
#define N 1010
#define rg register
using namespace std;
int n,m,E,tot,ans,T,last[N],v[N],from[N];
struct edge{
	int to,pre;
}e[N*N];
inline int read(){
	int k=0,f=1; char c=getchar();
	while(c<‘0‘||c>‘9‘)c==‘-‘&&(f=-1),c=getchar();
	while(‘0‘<=c&&c<=‘9‘)k=k*10+c-‘0‘,c=getchar();
	return k*f;
}
inline void add(int x,int y){e[++tot]=(edge){y,last[x]}; last[x]=tot;}
bool dfs(int x){
	for(rg int i=last[x],to;i;i=e[i].pre){
		if(v[to=e[i].to]!=T){
			v[to]=T;
			if((!from[to])||dfs(from[to])){
				from[to]=x;
				return 1;
			}
		}
	}
	return 0;
}
int main(){
	n=read(); m=read(); E=read();
	for(rg int i=1,x,y;i<=E;i++){
		x=read(); y=read();
		if(y<=m) add(x,y);
	}
	for(rg int i=1;i<=n;i++) ++T,ans+=dfs(i);
	return printf("%d\n",ans),0;
}

  洛谷3386

【模板】二分图匹配

标签:int   printf   bool   二分图   turn   getch   highlight   tchar   amp   

原文地址:https://www.cnblogs.com/DriverLao/p/8490367.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!