QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#587127#898. 二分图最大匹配xorzjTL 0ms0kbC++173.2kb2024-09-24 17:43:332024-09-24 17:43:34

Judging History

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

  • [2024-09-24 17:43:34]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-09-24 17:43:33]
  • 提交

answer

#include <bits/stdc++.h>
#define rep(a, b, c) for (int a = b; a <= c; a++)
#define ALL(x) (x).begin(), (x).end()
#define IOS cin.tie(0)->sync_with_stdio(false)
#ifdef LOCAL
#include "debug.h"
#else
#define deb(...) 42
#endif
#define OPENSTACK
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int MAXN = 2e5 + 5;
const int INF = 0x3f3f3f3f;
namespace flow {
    const int N = 6e4 + 5, M = 9e6 + 5;
    struct edge {
        int next, to, w;
    } e[M << 1];
    int n, s, t;
    int head[N], cnt, cur[N], dis[N];
    void init(int n_, int s_, int t_)
    {
        s = s_, t = t_, n = n_, cnt = 0;
        for (int i = 0; i < n; i++) head[i] = -1;
    }
    void add_edge(int u, int v, int w)
    {
        e[cnt] = edge{ head[u], v, w };
        head[u] = cnt;
        cnt++;
        e[cnt] = edge{ head[v], u, 0 };
        head[v] = cnt;
        cnt++;
    }
    bool bfs()
    {
        for (int i = 0; i < n; i++) cur[i] = head[i], dis[i] = -1;
        queue<int> q;
        dis[s] = 0;
        q.push(s);
        while (!q.empty()) {
            int x = q.front();
            q.pop();
            for (int i = head[x]; ~i; i = e[i].next) {
                if (e[i].w) {
                    int y = e[i].to;
                    if (~dis[y]) continue;
                    dis[y] = dis[x] + 1;
                    q.push(y);
                }
            }
        }
        return ~dis[t];
    }
    int dfs(int x, int flow)
    {
        int ans = 0;
        if (x == t) return flow;
        for (int i = cur[x]; ~i; i = e[i].next, cur[x] = i) {
            int y = e[i].to, newflow = min(e[i].w, flow);
            if (dis[y] != dis[x] + 1 || !e[i].w) continue;
            newflow = dfs(y, newflow);
            e[i].w -= newflow;
            e[i ^ 1].w += newflow;
            flow -= newflow;
            ans += newflow;
            if (!flow) break;
        }
        return ans;
    }
    int dinic()
    {
        int ans = 0;
        while (bfs()) ans += dfs(s, numeric_limits<int>::max());
        return ans;
    }
}  // namespace flow
using flow::add_edge;
using flow::dinic;
using flow::init;

void solve()
{
    int L, R, m;
    cin >> L >> R >> m;
    int s = L + R, t = s + 1;
    init(t + 1, s, t);
    vector<array<int, 3>>ec;
    for (int i = 1; i <= m; i++) {
        int x, y;
        cin >> x >> y;
        ec.push_back({ flow::cnt,x,y });
        add_edge(x, y + L, 1);
    }
    for (int i = 0; i < L; i++)add_edge(s, i, 1);
    for (int i = 0; i < R; i++)add_edge(i + L, t, 1);
    auto fl = dinic();
    cout << fl << "\n";
    for (auto [e, x, y] : ec) {
        if (flow::e[e].w == 0) {
            cout << x << " " << y << "\n";
        }
    }
}
signed main()
{
#ifdef LOCAL
#ifdef OPENSTACK
    int size = 128 << 20; // 64MB
    char* p = (char*) malloc(size) + size;
#if (defined _WIN64) or (defined __unix)
    __asm__("movq %0, %%rsp\n" ::"r"(p));
#else
    __asm__("movl %0, %%esp\n" ::"r"(p));
#endif
#endif
#endif
    IOS;
    int _ = 1;
    while (_--) {
        solve();
    }
#ifdef LOCAL
#ifdef OPENSTACK
    exit(0);
#else
    return 0;
#endif
#endif
}

详细

Test #1:

score: 0
Time Limit Exceeded

input:

100000 100000 200000
78474 45795
32144 46392
92549 13903
73460 34144
96460 92850
56318 77066
77529 84436
76342 51542
77506 99268
76410 89381
1778 61392
43607 96135
84268 74827
14857 35966
32084 94908
19876 174
1481 94390
12423 55019
64368 92587
81295 7902
25432 46032
36293 61128
73555 84836
8418 102...

output:


result: