QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#99479#6308. Magicrgnerdplayer#RE 0ms0kbC++202.5kb2023-04-22 17:58:592023-04-22 17:59:02

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-22 17:59:02]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2023-04-22 17:58:59]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

using i64 = long long;

constexpr int N = 5000;

struct BipartiteMatching {
    int n, m;
    vector<bitset<N>> g;
    vector<int> l, r, dis, cur;
    BipartiteMatching(int n, int m) : n(n), m(m), g(n), l(n, -1), r(m, -1), dis(n), cur(n) {}
    void addEdge(int u, int v) { g[u][v] = 1; }
    void bfs() {
        vector<int> q;
        for (int u = 0; u < n; u++) {
            if (l[u] == -1) {
                q.push_back(u), dis[u] = 0;
            } else {
                dis[u] = -1;
            }
        }
        for (int i = 0; i < int(q.size()); i++) {
            int u = q[i];
            for (int v = g[u]._Find_first(); v < m; v = g[u]._Find_next(v)) {
                if (r[v] != -1 && dis[r[v]] == -1) {
                    dis[r[v]] = dis[u] + 1;
                    q.push_back(r[v]);
                }
            }
        }
    }
    bool dfs(int u) {
        for (int v = g[u]._Find_first(); v < m; v = g[u]._Find_next(v)) {
            if (r[v] == -1 || dis[r[v]] == dis[u] + 1 && dfs(r[v])) {
                l[u] = v, r[v] = u;
                return true;
            }
        }
        return false;
    }
    int maxMatching() {
        int match = 0;
        while (true) {
            bfs();
            fill(cur.begin(), cur.end(), 0);
            int cnt = 0;
            for (int u = 0; u < n; u++) {
                if (l[u] == -1) {
                    cnt += dfs(u);
                }
            }
            if (!cnt) {
                break;
            }
            match += cnt;
        }
        return match;
    }
};

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    auto solve = [&]() {
        int n;
        cin >> n;

        vector<int> l(n), r(n), cnt(2), id(n);

        for (int i = 0; i < n; i++) {
            cin >> l[i] >> r[i];
            l[i]--, r[i]--;
            id[l[i]] = cnt[0]++;
            id[r[i]] = cnt[1]++ + n;
        }

        BipartiteMatching bm(n, n);

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (i != j && l[i] < l[j] && l[j] < r[i] && r[i] < r[j]) {
                    bm.addEdge(i, j);
                }
            }
        }

        cout << 2 * n - bm.maxMatching() << '\n';
    };
    
    solve();
    
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Dangerous Syscalls

input:

5
2 3
6 7
1 9
5 10
4 8

output:


result: