#include<iostream>
#include<cstring>
using namespace std;
const int N = 510;
const int M = 1e5+10;
int n1,n2,m;
//我们用邻接表存储n1到n2的边就可以啦
int h[N],e[M],ne[M],idx;
int match[N];//存储n2那边的所有点的匹配情况
bool st[N];
void add(int a,int b)
{
e[idx] = b;
ne[idx] = h[a];
h[a] = idx++;
}
bool find(int x)
{
//这个函数的作用是判断n1中的某个点是否能在自己
//已经与之建立边的人那里找到成功配对的
for(int i = h[x];i!=-1;i=ne[i])
{
int j = e[i];
if(!st[j])
{
st[j] = true;
if(match[j] ==0||find(match[j]))
{
match[j] = x;
return true;
}
}
}
return false;
}
int main()
{
cin>>n1>>n2>>m;
memset(h,-1,sizeof h);
while(m--)
{
int a,b;
cin>>a>>b;
add(a,b);
}
int res = 0;
for(int i = 1;i<=n1;i++)
{
memset(st,false,sizeof st);
if(find(i))res++;
}
cout<<res<<endl;
return 0;
}
AcWing 861. 二分图的最大匹配
最新推荐文章于 2024-07-15 15:17:48 发布