QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#454872 | #6546. Greedy Bipartite Matching | pandapythoner | WA | 25ms | 12620kb | C++20 | 3.2kb | 2024-06-25 15:59:19 | 2024-06-25 15:59:21 |
Judging History
answer
#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define flt double
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define rep(i, n) for(int i = 0; i < n; i += 1)
const ll inf = 1e18;
mt19937 rnd(234);
int n, q;
vector<vector<int>> g;
vector<vector<pair<int, int>>> edges;
vector<int> result;
vector<int> is_good_left, is_good_right, good_left;
int usdc;
vector<int> usd;
vector<int> p;
vector<int> usd_left;
bool dfs(int v) {
usd[v] = usdc;
for (auto to : g[v]) {
if (p[to] == -1) {
p[to] = v;
usd_left[v] = true;
return true;
}
if (usd[p[to]] == usdc) {
continue;
}
if (dfs(p[to])) {
p[to] = v;
usd_left[v] = true;
return true;
}
}
return false;
}
void solve() {
is_good_left.assign(n, true);
is_good_right.assign(n, true);
good_left.resize(n);
rep(i, n) good_left[i] = i;
usd.assign(n, 0);
usdc = 1;
g.assign(n, vector<int>());
result.resize(q);
p.assign(n, -1);
int cur_matching = 0;
usd_left.assign(n, false);
rep(eid, q) {
for (auto [u, v] : edges[eid]) {
if (is_good_left[u] and is_good_right[v]) {
g[u].push_back(v);
}
}
++usdc;
for (auto v : good_left) {
if (usd_left[v]) {
continue;
}
if (dfs(v)) {
cur_matching += 1;
}
}
result[eid] = cur_matching;
if (eid + 1 == q) {
break;
}
++usdc;
queue<int> q;
for (auto v : good_left) {
if (usd_left[v]) {
continue;
}
q.push(v);
usd[v] = usdc;
}
while (!q.empty()) {
int v = q.front();
q.pop();
for (auto to : g[v]) {
assert(p[to] != -1);
is_good_right[to] = false;
if (usd[p[to]] == usdc) {
continue;
}
usd[p[to]] = usdc;
q.push(p[to]);
}
}
vector<int> new_good_left;
for (auto v : good_left) {
if (usd[v] == usdc) {
new_good_left.push_back(v);
} else {
is_good_left[v] = false;
}
}
good_left.swap(new_good_left);
}
}
int32_t main() {
if (1) {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
cin >> n >> q;
edges.assign(q, vector<pair<int, int>>());
rep(i, q) {
int t;
cin >> t;
edges[i].resize(t);
rep(j, t) {
int u, v;
cin >> u >> v;
--u;
--v;
edges[i][j] = make_pair(u, v);
}
}
solve();
for (int i = 0; i < q; i += 1) {
cout << result[i] << " ";
}
cout << "\n";
return 0;
}
/*
3 4
2
1 1
1 2
2
1 1
2 2
2
1 3
3 2
1
3 3
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3612kb
input:
3 4 2 1 1 1 2 2 1 1 2 2 2 1 3 3 2 1 3 3
output:
1 2 2 3
result:
ok 4 number(s): "1 2 2 3"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3848kb
input:
0 0
output:
result:
ok 0 number(s): ""
Test #3:
score: 0
Accepted
time: 0ms
memory: 3628kb
input:
2 2 0 2 1 2 1 2
output:
0 1
result:
ok 2 number(s): "0 1"
Test #4:
score: -100
Wrong Answer
time: 25ms
memory: 12620kb
input:
100000 1 200000 78475 45796 32145 46393 92550 13904 73461 34145 96461 92851 56319 77067 77530 84437 76343 51543 77507 99269 76411 89382 1779 61393 43608 96136 84269 74828 14858 35967 32085 94909 19877 175 1482 94391 12424 55020 64369 92588 81296 7903 25433 46033 36294 61129 73556 84837 8419 10215 12...
output:
87591
result:
wrong answer 1st numbers differ - expected: '100000', found: '87591'