版权声明:转载请注明出处。 https://blog.csdn.net/u014427196/article/details/48345113

题目: http://acm.hdu.edu.cn/showproblem.php?pid=1151

DAG最小路径覆盖 = 结点数 - 最大匹配数

代码:

#include <iostream>  
#include <algorithm>  
#include <set>  
#include <map>  
#include <string.h>  
#include <queue>  
#include <sstream>  
#include <stdio.h>  
#include <math.h>  
#include <stdlib.h>  

using namespace std;

int n, m;
int p[1000][1000];
int book[1000];
int match[1000];

int dfs(int u)
{
    for (int i = 1; i <= n; i++)
    {
        if (book[i] == 0 && p[u][i] == 1)
        {
            book[i] = 1;
            if (match[i] == 0 || dfs(match[i]))
            {
                match[i] = u;
                return 1;
            }
        }
    }
    return 0;
}

int main()
{
    int t;
    scanf("%d",&t);
    while (t--)
    {
        scanf("%d %d", &n, &m);

        memset(match, 0, sizeof(match));
        memset(p, 0, sizeof(p));

        int u, v;
        while(m--)
        {
            scanf("%d %d", &u, &v);
            p[u][v] = 1;
        }

        int ans = 0;
        for (int i = 1; i <= n; i++)
        {
            memset(book, 0, sizeof(book));
            if (dfs(i))
                ans++;
        }
        printf("%d\n", n - ans);
    }
    return 0;
}