QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#454869#6546. Greedy Bipartite MatchingpandapythonerTL 76ms20080kbC++203.2kb2024-06-25 15:55:552024-06-25 15:55:55

Judging History

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

  • [2024-06-25 15:55:55]
  • 评测
  • 测评结果:TL
  • 用时:76ms
  • 内存:20080kb
  • [2024-06-25 15:55:55]
  • 提交

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;
                ++usdc;
            }
        }
        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

*/

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3556kb

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: 3544kb

input:

0 0

output:



result:

ok 0 number(s): ""

Test #3:

score: 0
Accepted
time: 0ms
memory: 3568kb

input:

2 2
0
2
1 2
1 2

output:

0 1 

result:

ok 2 number(s): "0 1"

Test #4:

score: 0
Accepted
time: 76ms
memory: 20080kb

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:

100000 

result:

ok 1 number(s): "100000"

Test #5:

score: 0
Accepted
time: 67ms
memory: 15464kb

input:

100000 1
200000
56816 52517
2577 76202
40378 1758
50464 66497
15834 50880
9829 16331
80693 9963
51096 17591
15871 35192
91302 65510
90775 57493
11891 8967
44787 41896
3387 35479
93471 47453
84804 93636
90746 34877
18202 38718
7473 34258
36581 19533
13249 27525
6442 69870
8822 61871
94537 67714
41396...

output:

100000 

result:

ok 1 number(s): "100000"

Test #6:

score: 0
Accepted
time: 0ms
memory: 3636kb

input:

4 1
7
2 2
3 3
1 1
4 2
2 3
3 1
4 3

output:

3 

result:

ok 1 number(s): "3"

Test #7:

score: -100
Time Limit Exceeded

input:

100000 1
199999
25371 25371
85965 85964
416 416
16797 16797
12438 12438
45410 45409
63006 63005
22156 22156
87829 87828
84014 84014
37308 37308
72325 72325
83704 83704
55391 55390
6781 6780
78091 78091
9376 9376
82193 82193
74695 74695
49842 49842
15799 15799
69856 69855
82949 82948
97390 97389
205 ...

output:


result: