又到了喜闻乐见的wyf烧情侣环节
今天我们要解决的是ll和lrh
伪代码
for (遍历所有ll集合中的点)
{
if(这个ll莫得lrh)
if(作为一个成熟的ll是不吃回头草的,所以只能找莫得被找过的lrh)
看看她能不能找到一个lrh,找到就ans++;
}
void find()
{
for (当前ll的所有lrh)
if (这个lrh莫得ll或者能找到其他ll)
这个lrh就跟当前ll匹配了
}
#include<bits/stdc++.h>
#define maxn 2000010
using namespace std;
struct edge
{
int end,next;
}e[maxn];
int tot;
int head[maxn];
void add(int x,int y)
{
e[++tot].next=head[x];
e[tot].end=y;
head[x]=tot;
}
int ll[maxn],lrh[maxn];
bool vis[maxn];
bool find(int x)
{
for (int i=head[x];i;i=e[i].next)
{
int to=e[i].end;
if (!vis[to])
{
vis[to]=true;
if (!lrh[to]||find(lrh[to]))
{
lrh[to]=x,ll[x]=to;
return true;
}
}
}
return false;
}
int main()
{
//freopen("ha.in","r",stdin);
//freopen("ha.out","w",stdout);
int n,m,num;
scanf("%d%d%d",&n,&m,&num);
for (int i=1;i<=num;i++)
{
int a,b;
scanf("%d%d",&a,&b);
if (a<=n&&b<=m)
{
add(a,b+n);
add(b+n,a);
}
}
int ans=0;
for (int i=1;i<=n;i++)
{
memset(vis,0,sizeof(vis));
ans+=find(i);
}
cout<<ans;
return 0;
}
tips
- 别忘了清理vis数组
- 匹配的实际上只有ll而已