POJ 3401 Asteroids (二分图)

一个裸的二分图。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define mem(x,v) memset(x,v,sizeof(x))
const int N = 509;
bool map[N][N];
int n,m;
bool used[N];
int linker[N];
bool dfs(int u){
    for (int i = 1; i <= n; i++)
        if (map[u][i] && !used[i]){
            used[i] = 1;
            if (linker[i] == -1 || dfs(linker[i])){
                linker[i] = u;
                return 1;
            }
        }
        return 0;
}
int big_matching(){
    int ans = 0;
    mem(linker,-1);
    for (int i = 1; i <= n; i++){
        mem(used,0);
        if (dfs(i)) ans++;
    }
    return ans;
}
int main() {
    int x,y;
    scanf("%d%d",&n,&m);
    for (int i = 0; i < m; i++){
        scanf("%d%d",&x,&y);
        map[x][y] = 1;
    }
    printf("%d\n",big_matching());
    return 0;
}

猜你喜欢

转载自blog.csdn.net/kidsummer/article/details/81257388