二分图匹配

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
const int maxn = 2001;
int vis[maxn],have[maxn];
bool line[maxn][maxn];
int n,m,e,ans=0,tmp1,tmp2;
bool find(int x)
{
for(int i=1;i<=m;++i)
if(line[x][i]&&!vis[i])
{
vis[i]=1;
if(!have[i]||find(have[i]))
{
have[i]=x;
return 1;
}
}
return 0;
}
int main()
{
scanf("%d%d%d",&n,&m,&e);
for(int i=1;i<=e;++i)
scanf("%d%d",&tmp1,&tmp2),line[tmp1][tmp2]=1;
for(int i=1;i<=n;++i)
{
memset(vis,0,sizeof(vis));
if(find(i))ans++;
}
printf("%d",ans);
return 0;
}
显示 Gitment 评论