1 条题解

  • 0
    @ 2021-8-8 10:44:39

    C++ :

    #pragma GCC optimize(2)
    
    #include <bits/stdc++.h>
    
    using namespace std;
    const int N = 105, M = 2e3 + 10;
    bool pic[N][N];
    int n, m, k;
    int match[N];
    bool vis[N];
    int ans;
    
    void read() {
        cin >> m >> k;
        for (int i = 1; i <= k; i++) {
            int j, a, b;
            scanf("%d%d%d", &j, &a, &b);
            if (!a || !b)continue;
            pic[a][b] = true;
        }
    }
    
    void init() {
        memset(pic, 0, sizeof(pic));
        memset(match, 0, sizeof(match));
        memset(vis, false, sizeof(vis));
        ans = 0;
    }
    
    bool dfs(int x) {
        for (int y = 1; y < m; y++)
            if (pic[x][y] && !vis[y]) {
                vis[y] = true;
                if (!match[y] || dfs(match[y])) {
                    match[y] = x;
                    return true;
                }
            }
        return false;
    }
    
    void solve() {
        for (int x = 1; x < n; x++) {
            memset(vis, false, sizeof(vis));
            if (dfs(x))
                ans++;
        }
        cout << ans << endl;
    }
    
    int main() {
        while (cin >> n && n) {
            init();
            read();
            solve();
        }
        return 0;
    }
    
    • 1

    信息

    ID
    286
    时间
    1000ms
    内存
    128MiB
    难度
    10
    标签
    递交数
    4
    已通过
    4
    上传者