Skip to content

Commit 8bba1fb

Browse files
committed
atcoder/abc091C (feedback from editorial)
1 parent 248a8e4 commit 8bba1fb

File tree

1 file changed

+17
-63
lines changed

1 file changed

+17
-63
lines changed

atcoder/abc091/C/main.cpp

Lines changed: 17 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,5 @@
11
#include <bits/stdc++.h>
22

3-
class BipartiteMaximumMatching {
4-
size_t N;
5-
std::vector<std::vector<size_t>> adj;
6-
7-
public:
8-
// Time: O(N)
9-
BipartiteMaximumMatching(size_t n = 0)
10-
: N(n)
11-
, adj(n)
12-
{
13-
// Do nothing
14-
}
15-
// Time: O(1)
16-
size_t size() const {
17-
return N;
18-
}
19-
// u = [0,N), v = [0,N)
20-
// Time: O(1)
21-
void add_edge(size_t u, size_t v) {
22-
if (u >= N) throw std::out_of_range("u");
23-
if (v >= N) throw std::out_of_range("v");
24-
adj[u].push_back(v);
25-
adj[v].push_back(u);
26-
}
27-
// Time: O( V(V+E) )
28-
size_t solve(std::vector<size_t>& match) {
29-
size_t ans = 0;
30-
std::vector<bool> used(N, false);
31-
match = std::vector<size_t>(N, N);
32-
for (size_t v = 0; v < N; ++v) {
33-
if (match[v] != N) continue;
34-
used.assign(N, false);
35-
if (dfs(match, used, v)) ans++;
36-
}
37-
return ans;
38-
}
39-
40-
private:
41-
bool dfs(
42-
std::vector<size_t>& match,
43-
std::vector<bool>& used,
44-
size_t v
45-
) {
46-
used[v] = true;
47-
for (size_t u : adj[v]) {
48-
size_t w = match[u];
49-
if (w == N || (!used[w] && dfs(match, used, w))) {
50-
match[v] = u;
51-
match[u] = v;
52-
return true;
53-
}
54-
}
55-
return false;
56-
}
57-
};
58-
59-
603
using namespace std;
614
using ll = int64_t;
625
using ff = long double;
@@ -65,16 +8,27 @@ int N;
658
vector<pair<int,int>> rs, bs;
669

6710
int solve() {
68-
BipartiteMaximumMatching solver(2*N);
11+
// x asc, y asc
12+
sort(bs.begin(), bs.end());
13+
// y desc x desc
14+
sort(rs.begin(), rs.end(), [](auto const& lhs, auto const& rhs) {
15+
if (lhs.second != rhs.second) return lhs.second > rhs.second;
16+
return lhs.first > rhs.first;
17+
});
18+
19+
int count = 0;
20+
vector<bool> used(N, false);
6921
for (int i = 0; i < N; ++i) {
7022
for (int j = 0; j < N; ++j) {
71-
if (rs[i].first < bs[j].first && rs[i].second < bs[j].second) {
72-
solver.add_edge(i, N + j);
73-
}
23+
if (used[j]) continue;
24+
if (bs[i].first <= rs[j].first) continue;
25+
if (bs[i].second <= rs[j].second) continue;
26+
used[j] = true;
27+
++count;
28+
break;
7429
}
7530
}
76-
vector<size_t> match;
77-
return solver.solve(match);
31+
return count;
7832
}
7933

8034
int main() {

0 commit comments

Comments
 (0)