题解:
二分图边覆盖
n-最大匹配
代码:
#include<cstdio> #include<cmath> #include<algorithm> #include<cstring> using namespace std; const int N=505; int fi[N],num,T,n,m,ne[N*N],zz[N*N],f[N],match[N],x,y; void jb(int x,int y) { ne[++num]=fi[x]; fi[x]=num; zz[num]=y; } int dfs(int x) { for (int i=fi[x];i;i=ne[i]) if (!f[zz[i]]) { f[zz[i]]=1; if (!match[zz[i]]||dfs(match[zz[i]])) { match[zz[i]]=x; return 1; } } return 0; } int main() { scanf("%d",&T); while (T--) { memset(match,0,sizeof match); memset(fi,0,sizeof fi); num=0; scanf("%d%d",&n,&m); while (m--) { scanf("%d%d",&x,&y); jb(x,y); } int ans=0; for (int i=1;i<=n;i++) { memset(f,0,sizeof f); ans+=dfs(i); } printf("%d\n",n-ans); } }