题意:一个城镇中有n个路口和m条单项的路径,图是无环图,现在要派一些伞兵去巡查这个城市,要到达所有的路口;
每个在一个路口着陆了的伞兵可以沿着街去到其他路口;求最少伞兵数量。
在有向图中找一些路径,使之覆盖了图中的所有顶点,就是任意一个顶点都跟那些路径中的某一条关联,且任何一个顶点有且只有一个与之关联;一个单独的顶点是一条路径。
最小路径覆盖就是最少的路径覆盖数。 最小路径覆盖=图的顶点数-最大匹配数
链接:hdu 1151
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 2555;
int used[maxn];
int link[maxn];
int mat[maxn][maxn];
int gn, gm;
int dfs(int t) {
for(int i = 1; i <= gm; i++) {
if(!used[i] && mat[t][i]) {
used[i] = 1;
if(link[i] == -1 || dfs(link[i])) {
link[i] = t;
return 1;
}
}
}
return 0;
}
int maxmatch() {
int num = 0;
memset(link, 0xff, sizeof(link));
for(int i = 1; i <= gn; i++) {
memset(used, 0, sizeof(used));
if(dfs(i)) {
num++;
}
}
return num;
}
int main()
{
int n, m, k, t;
cin >> t;
while(t--){
scanf("%d", &n);
scanf("%d", &m);
memset(mat, 0, sizeof(mat));
gn = n;
gm = n;
int a, x, y;
for(int i = 0; i < m; i++) {
scanf("%d %d", &x, &y);
mat[x][y] = 1;
}
int res = maxmatch();
printf("%d\n",n - res);
}
return 0;
}