【題解】TIOJ 1089 . Asteroids

【題目敘述】https://tioj.ck.tp.edu.tw/problems/1089
【解題想法】二分圖的最大匹配

#include <iostream>
#include <cstring>
using namespace std;

int n, k, r, c, g[505][505], a[505], vis[505], ans;

bool dfs(int x){
    for (int i = 1; i <= n; i++){
        if (g[x][i] == 1 && vis[i]==0){
            vis[i] = 1;
            if (a[i]==0 || dfs(a[i])){
                a[i] = x;
                return 1;
            }
        }
    }
    return 0;
}

int main() {
    cin >> n >> k;
    for (int i = 0; i < k; i++){
        cin >> r >> c;
        g[r][c] = 1;
    }
    for (int i = 1; i <= n; i++){
        memset(vis, 0, sizeof(vis));
        if (dfs(i)) ans++;
    }
    cout << ans << "\n";
}
分享本文 Share with friends