hdu 1151 Air Raid

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

最小路径覆盖=N-最大匹配

View Code
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<bitset>
#include<string>
#include<climits>
#include<cstdio>
#include<vector>
#include<utility>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define IN puts("in")
#define OUT puts("out")
#define FR(x) freopen(x,"r",stdin)
#define FW(x) freopen(x,"w",stdout)
#define MSET(x,y) memset(x,y,sizeof(x))
#define ST system("pause")
#define lowbit(x) (x)&(-x)
#define L(x) (x)<<1
#define R(x) ((x)<<1)^1
using namespace std;
const int maxn = 125;
int as[maxn][maxn],vis[maxn],to[maxn];
int n,m;
int dfs(int u)
{
        for(int v = 1; v <= n; ++ v)
                if(as[u][v]&&!vis[v]){
                        vis[v] = 1;
                        if(to[v]==0||dfs(to[v])){
                                to[v] = u;
                                return 1;
                        }
                }
        return 0;
}
int maxmatch()
{
        int i,cnt=0;
        MSET(to,0);
        for(i = 1; i <= n; ++ i){
                MSET(vis,0);
                cnt+=dfs(i);
        }return cnt;
}
int main()
{
        int i,k,ans;
        scanf("%d",&k);
        while(k--)
        {
                scanf("%d %d",&n,&m);
                MSET(as,0);
                for(i = 0; i < m; ++ i){
                        int x,y;
                        scanf("%d %d",&x,&y);
                        as[x][y] = 1;
                }
                ans = maxmatch();
                printf("%d\n",n-ans);
        }
        return 0;
}

posted on 2012-07-23 17:05  aigoruan  阅读(144)  评论(0)    收藏  举报

导航