QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#99493 | #6308. Magic | rgnerdplayer | RE | 0ms | 0kb | C++20 | 4.2kb | 2023-04-22 18:23:44 | 2023-04-22 18:23:47 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3", "avx2")
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] = true; }
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)) {
dis[r[v]] = dis[u] + 1;
q.push_back(r[v]);
}
}
}
bool dfs(int u) {
if (cur[u] == 0) {
cur[u] = g[u]._Find_first();
}
for (int &v = cur[u]; v < m; v = g[u]._Find_next(cur[u])) {
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;
}
};
// constexpr int MAXN = 5000;
// #define SZ(v) int((v).size())
// #define pb push_back
// struct Bipartite_Matching { // 0-base
// int l, r;
// int mp[MAXN], mq[MAXN];
// int dis[MAXN], cur[MAXN];
// bitset<N> G[MAXN];
// bool dfs(int u) {
// for (int &i = cur[u]; i < SZ(G[u]); ++i) {
// int e = G[u][i];
// if (!~mq[e] || (dis[mq[e]] == dis[u] + 1 && dfs(mq[e])))
// return mp[mq[e] = u] = e, 1;
// }
// dis[u] = -1;
// return 0;
// }
// bool bfs() {
// int rt = 0;
// queue<int> q;
// fill_n(dis, l, -1);
// for (int i = 0; i < l; ++i)
// if (!~mp[i])
// q.push(i), dis[i] = 0;
// while (!q.empty()) {
// int u = q.front();
// q.pop();
// for (int e : G[u])
// if (!~mq[e])
// rt = 1;
// else if (!~dis[mq[e]]) {
// q.push(mq[e]);
// dis[mq[e]] = dis[u] + 1;
// }
// }
// return rt;
// }
// int matching() {
// int rt = 0;
// fill_n(mp, l, -1);
// fill_n(mq, r, -1);
// while (bfs()) {
// fill_n(cur, l, 0);
// for (int i = 0; i < l; ++i)
// if (!~mp[i] && dfs(i))
// ++rt;
// }
// return rt;
// }
// void add_edge(int s, int t) {
// G[s].pb(t);
// }
// void init(int _l, int _r) {
// l = _l, r = _r;
// for (int i = 0; i < l; ++i)
// G[i].clear();
// }
// };
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(2 * 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(id[l[j]], id[r[i]] - n);
}
}
}
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