给一个DAG图,一个人可以走一条路,或者就在一个点(路径长度为0),问至少需要多少人可以覆盖所有点。
根据二分图的性质:
DAG的最小路径覆盖,将每个点拆点后求最大匹配数m,结果为n-m,求具体路径的时候顺着匹配边走就可以,匹配边i→j',j→k',k→l'....构成一条有向路径。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
const int maxn=125;
using namespace std;
int mx[maxn],my[maxn],vis[maxn],e[maxn][maxn],n,m;
int path(int u)
{
int i;
for(i=1;i<=n;i++)
{
if(e[u][i]&&!vis[i])
{
vis[i]=1;
if(my[i]==-1||path(my[i]))
{
mx[u]=i;
my[i]=u;
return 1;
}
}
}
return 0;
}
int hungry()
{
int res=0;
memset(mx,-1,sizeof mx);
memset(my,-1,sizeof my);
for(int i=1;i<=n;i++)
{
if(mx[i]==-1)
{
memset(vis,0,sizeof vis);
res+=path(i);
}
}
return res;
}
int main()
{
int ans,T,a,b,i;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
memset(e,0,sizeof e);
while(m--)
{
scanf("%d%d",&a,&b);
e[a][b]=1;
}
ans=hungry();
printf("%d\n",n-ans);
}
return 0;
}