Air Raid HDU - 1151

#include <stdio.h>
#include <string.h>
using namespace std;
typedef long long int ll;
const int maxn=101010;
int e[maxn],ne[maxn],h[maxn],idx;
int N, M;
void add(int a,int b)
{
    e[idx]=b;
    ne[idx]=h[a];
    h[a]=idx++;
}
bool vis[maxn];
int math[maxn];
bool hungrmath(int x) {
    for (int i = h[x]; i !=-1; i=ne[i]) {
        int j=e[i];
        if (vis[j] == 0) {
            vis[j] = 1;
            if (math[j] == -1 || hungrmath(math[j])) {
                math[j] = x;
                return true;
            }
        }
    }
    return false;
}
int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d", &N, &M);
        int a, b;
        memset(h,-1,sizeof h);
        for (int i = 0; i < M; i++) {
            scanf("%d%d", &a, &b);
            add(a,b);
        }
        int ans = 0;
        memset(math, -1, sizeof(math));
        for (int i = 1; i <= N; i++) {
            memset(vis, 0, sizeof(vis));
            if (hungrmath(i))
                ans++;
        }
        printf("%d\n", N - ans);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/QingyuYYYYY/p/12431039.html
今日推荐