
今天复习二分匹配,A 了一道模板题。
二分匹配需要理解增广路的寻找。用dfs来更新最大匹配。注意一些点:赋初值;愚蠢地把==写成了= ; 然后match的记值;每个点都要重新走一遍。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define INF 80005
#define maxn 405
using namespace std;
int n,m,ans,match[maxn];
int tot,he[maxn],to[INF],ne[INF];
bool check[maxn];
void add(int a,int b)
{
tot++;to[tot]=b;ne[tot]=he[a];he[a]=tot;
}
bool findway(int x)
{
for (int i=he[x];i;i=ne[i])//dfs
if (!check[to[i]]){
check[to[i]]=true;
if (match[to[i]]==-||findway(match[to[i]])){ //not findway(to[i])
match[to[i]]=x;//<qwq> == not =
return true;
}
}
return false;
}
int KM ()//hungray
{
ans=;
memset(match,-,sizeof (match));
for (int i=;i<=n;i++) //if (match[i]=-1)
{
memset(check,,sizeof (check));
if (findway(i)) ans++;
}
return ans;
}
int main()
{
freopen("poj1274.in","r",stdin);
while (cin>>n>>m){
memset(ne,,sizeof(ne));
memset(to,,sizeof (to));
memset(he,,sizeof (he));
tot=;//赋初值
for (int i=;i<=n;i++)
{
int a;scanf("%d",&a);
for (int j=;j<=a;j++)
{
int b;scanf("%d",&b);
b+=n;//!!!
add(i,b);add(b,i);
}
}
printf("%d\n",KM());
}
return ;
}