POJ3041 Asteroids

来源:互联网 发布:计算机java语言培训 编辑:程序博客网 时间:2024/05/22 15:17

传送门

二分图最大匹配,把行和列分别看做两个点集,求得是最小点覆盖集。

二分图最大匹配König定理:最大匹配数=最小点覆盖集,然后建好图以后直接套匈牙利算法模板

像我这种图论的渣渣,到现在还真的是图论只会套模板。。

#include <cstdio>#include <cstring>#include <vector>using namespace std;const int maxn = 555;const int n = 500;vector<int>g[maxn];int from[maxn], tot;bool use[maxn];bool match(int x){    for (int i = 0; i < g[x].size(); i++)        if (!use[g[x][i]]){            use[g[x][i]] = 1;            if (from[g[x][i]] == -1 || match(from[g[x][i]])){                from[g[x][i]] = x;                return 1;            }        }    return 0;}int hungary(){    tot = 0;    memset(from, 255, sizeof(from));    for (int i = 1; i <= n; i++){        memset(use, 0, sizeof(use));        if (match(i)) tot += 1;    }    return tot;}int main(){    int n, k;    while(~scanf("%d%d", &n, &k)){        for (int i = 0; i < maxn; i++) g[i].clear();        while(k--){            int x, y;            scanf("%d%d", &x, &y);            g[x].push_back(y);        }        int ans = hungary();        printf("%d\n", ans);    }    return 0;}


原创粉丝点击