Air Raid---hdu1151(最小路径覆盖)

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

最小路径覆盖 == 顶点数 - 最大匹配。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 130

int maps[N][N], vis[N], used[N], n, m, ans;
bool Find(int u)
{
    for(int i=1; i<=n; i++)
    {
        if(!vis[i] && maps[u][i])
        {
            vis[i] = 1;
            if(!used[i] || Find(used[i]))
            {
                used[i] = u;
                return true;
            }
        }
    }
    return false;
}

int main()
{
    int T, x, y;
    scanf("%d", &T);
    while(T--)
    {
        memset(maps, 0, sizeof(maps));
        memset(used, 0, sizeof(used));
        scanf("%d%d", &n, &m);
        while(m--)
        {
            scanf("%d%d", &x, &y);
            maps[x][y] = 1;
        }
        ans=0;
        for(int i=1; i<=n; i++)
        {
            memset(vis, 0, sizeof(vis));
            if(Find(i))
                ans++;
        }
        printf("%d\n",n-ans);
    }
    return 0;
}

 

posted @ 2015-08-11 09:03  西瓜不懂柠檬的酸  Views(240)  Comments(0Edit  收藏  举报
levels of contents