Asteroids poj3041

   一道简单的二分匹配,用匈牙利算法就行。

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
vector<int>map[501];
int fy[501],match[501];
int n,k;

int path(int u)
{
	for(int i=0,j;i<map[u].size();i++)
	{
		j=map[u][i];
		if(!fy[j])
		{
			fy[j]=1;
			if(match[j]==-1||path(match[j]))
			{
				match[j]=u;
				return 1;
			}
		}
	}
	return 0;
}

int main()
{
	int i,j,x,y,ans;
	while(~scanf("%d%d",&n,&k))
	{
		for(i=1;i<=n;i++)
		{
			match[i]=-1;
			map[i].clear();
		}
		while(k--)
		{
			scanf("%d%d",&x,&y);
			map[x].push_back(y);
		}
		ans=0;
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=n;j++)
				fy[j]=0;
			if(path(i))
				ans++;
		}
		printf("%d\n",ans);
	}
	return 0;
}


 

你可能感兴趣的:(算法,Path)