3,882
社区成员
发帖
与我相关
我的任务
分享#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
const int maxn = 150;
int mat[maxn][maxn];
int gn, gm;
int match[maxn];
bool used[maxn];
bool dfs(int u)
{
for(int v = 1;v <= gm; v++) {
if(!used[v] && mat[u][v]) {
used[v] = true;
if(match[v] < 0 || dfs(match[v])) {
match[v] = u;
return true;
}
}
}
return false;
}
int MaxMatching()
{
int res = 0;
memset(match, -1, sizeof(match));
for(int i = 1;i <= gn;i++) {
memset(used, 0, sizeof(used));
if(dfs(i))
res++;
}
return res;
}
int main()
{
int T, n, m, u, v;
scanf("%d", &T);
while(T--) {
scanf("%d%d", &n, &m);
memset(mat, 0, sizeof(mat));
for(int i = 0;i < m;i++) {
scanf("%d%d", &u, &v);
mat[u][v] = 1;
}
gn = gm = n;
printf("%d\n", n - MaxMatching());
}
return 0;
}