QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#99479 | #6308. Magic | rgnerdplayer# | RE | 0ms | 0kb | C++20 | 2.5kb | 2023-04-22 17:58:59 | 2023-04-22 17:59:02 |
Judging History
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