计蒜客 B. Bounty Hunter II(二分图、最下路径覆盖)

转文:https://www.cnblogs.com/d-e-v-i-l/p/5452441.html

#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <cmath>
#define maxn 2005
#define maxz 2005
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
bool used[maxn];
int n;
bool linked[maxn][maxn];
int anp[maxn];
bool Max_match(int e)
{
	for(int i=0;i<n;i++)
	{
		if(linked[e][i]==false)
			continue;
		if(used[i]==true)
			continue;
		used[i]=true;
		if(anp[i]==-1||Max_match(anp[i]))
		{
			anp[i]=e;
			return true;
		}
	}
	return false;
}
void solve()
{
	int ans=0;
	for(int i=0;i<n;i++)
	{
		memset(used,0,sizeof(used));
		if(Max_match(i))
			ans++;
	}
	cout << n-ans << endl;
}
int main()
{
	cin>>n;
	memset(linked,false,sizeof(linked));
	memset(anp,-1,sizeof(anp)); 
	for(int i=0;i<n;i++)
	{
		int t;
		cin>>t;
		for(int j=0;j<t;j++)
		{
			int tz;
			cin>>tz;
			linked[i][tz]=true;
		}
	}
	solve();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/wwwlps/article/details/81408056