首页 > 代码库 > 二分匹配模版

二分匹配模版


const int maxn = 10010;
int vis[maxn];
int y[maxn];
vector <int> G[maxn];
int n;

bool dfs(int u)
{
	for(int i = 0; i < G[u].size(); i++)
	{
		int v = G[u][i];
		if(vis[v])
			continue;
		vis[v] = true;
		if(y[v] == -1 || dfs(y[v]))
		{
			y[v] = u;
			return true;
		}
	}
	return false;
}
int match()
{
	int ans = 0;
	memset(y, -1, sizeof(y));
	for(int i = 0; i < n; i++)
	{
		memset(vis, 0, sizeof(vis));
		if(dfs(i))
			ans++;
	}
	return ans;
}