QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#478802#898. 二分图最大匹配A_programmer#TL 0ms0kbC++172.0kb2024-07-15 11:15:092024-07-15 11:15:10

Judging History

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

  • [2024-07-15 11:15:10]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-07-15 11:15:09]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn = 2e5 + 5;
const int inf = 0x3f3f3f3f;

struct edge
{
    int v, next, flow;
}e[maxn << 2];
int head[maxn], tot, s, t;

inline void addedge(int u, int v, int c)
{
    e[++tot] = (edge){v, head[u], c}, head[u] = tot;
    e[++tot] = (edge){u, head[v], 0}, head[v] = tot;
}

int d[maxn], cur[maxn];
bool bfs()
{
    for (int i = s; i <= t; i++) d[i] = 0, cur[i] = head[i];
    queue<int> q; q.push(s);
    while (q.size())
    {
        int u = q.front(); q.pop();
        for (int i = head[u]; ~i; i = e[i].next)
        {
            int v = e[i].v;
            if (!d[v] && e[i].flow)
            {
                d[v] = d[u] + 1;
                if (v == t) return true;
                q.push(v);
            }
        }
    }
    return false;
}

int dfs(int u, int flow)
{
    if (u == t || !flow) return flow;
    int rest = flow;
    for (int i = cur[u]; ~i; i = e[i].next)
    {
        int v = e[i].v;
        if (d[v] == d[u] + 1 && e[i].flow)
        {
            int k = dfs(v, min(rest, e[i].flow));
            if (!k) d[v] = 0;
            e[i].flow -= k, e[i ^ 1].flow += k;
            rest -= k; if (!rest) break;
        }
        cur[u] = i;
    }
    return flow - rest;
}

int Dinic()
{
    int maxflow = 0;
    while (bfs()) maxflow += dfs(s, inf);
    return maxflow;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n1, n2, m;
    cin >> n1 >> n2 >> m;
    s = 0, t = n1 + n2 + 1;
    memset(head, -1, sizeof(head)), tot = -1;
    for (int i = 1; i <= n1; i++) addedge(s, i, 1);
    for (int i = 1; i <= m; i++)
    {
        int u, v;
        cin >> u >> v, u++, v++;
        addedge(u, v + n1, 1);
    }
    for (int i = 1; i <= n2; i++) addedge(i + n1, t, 1);
    cout << Dinic() << "\n";

    for (int i = 0; i <= tot; i += 2)
        if (e[i].v != t && e[i ^ 1].v != s && !e[i].flow) cout << e[i ^ 1].v - 1 << " " << e[i].v - n1 - 1 << "\n";
    return 0;
}

详细

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: