POJ 2594 Treasure Exploration

POJ 2594 Treasure Exploration

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
const int N = 510;
int n, m;
int g[N][N];

// 匈牙利
int match[N], st[N];
int dfs(int u) {
    for (int i = 1; i <= n; i++)
        if (g[u][i] && !st[i]) {
            st[i] = 1;
            if (match[i] == -1 | dfs(match[i])) {
                match[i] = u;
                return 1;
            }
        }
    return 0;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("POJ2594.in", "r", stdin);
#endif
    while (scanf("%d%d", &n, &m) && (n + m)) {
        memset(g, 0, sizeof g);
        while (m--) {
            int x, y;
            scanf("%d%d", &x, &y);
            g[x][y] = 1;
        }

        // Floyd传递闭包
        for (int k = 1; k <= n; ++k)
            for (int i = 1; i <= n; ++i)
                for (int j = 1; j <= n; ++j)
                    g[i][j] |= g[i][k] & g[k][j];

        int res = 0;
        memset(match, -1, sizeof match);
        for (int i = 1; i <= n; i++) {
            memset(st, 0, sizeof st);
            if (dfs(i)) res++;
        }

        printf("%d\n", n - res);
    }
    return 0;
}
posted @   糖豆爸爸  阅读(10)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
历史上的今天:
2021-08-02 P1364 医院设置 题解
2021-08-02 P5076 【深基16.例7】普通二叉树(简化版)题解
2021-08-02 二叉树的三种遍历方式
2021-08-02 P1030 [NOIP2001 普及组] 求先序排列题解
2021-08-02 P1827 [USACO3.4]美国血统 American Heritage 题解
2018-08-02 mysql备份与还原
2017-08-02 PostgreSQL9.6.3的REDIS测试
Live2D
点击右上角即可分享
微信分享提示