QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#667493#7898. I Just Want... One More...foyonaczyRE 0ms0kbC++203.4kb2024-10-22 23:34:422024-10-22 23:34:49

Judging History

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

  • [2024-10-22 23:34:49]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-10-22 23:34:42]
  • 提交

answer

#include<bits/stdc++.h>

#define int long long
using i64 = long long;
constexpr i64 INF = 1e18;
struct Edge {
    int y;
    i64 w;
};
std::vector<int> v[200005];
std::vector<Edge> e;
std::vector<int> cur(200005), lv(200005), gap;
int n, s, t;

void bfs() {//分层
    for (int i = 1; i <= n; i++)lv[i] = -1;
    lv[t] = 0;
    gap[0] = 1;
    std::queue<int> q;
    q.push(t);
    while (!q.empty()) {
        int x = q.front();
        q.pop();
        for (int i: v[x]) {
            int y = e[i].y;
            if (lv[y] == -1) {
                lv[y] = lv[x] + 1;
                gap[lv[y]]++;
                q.push(y);
            }
        }
    }
}

i64 dfs(int x = s, i64 flow = INF) {
    if (x == t)return flow;
    i64 rmn = flow; // 剩余的流量
    for (int i = cur[x]; i < v[x].size() && rmn > 0; i++) // 如果已经没有剩余流量则退出
    {
        cur[x] = i; // 当前弧优化,更新当前弧
        int y = e[v[x][i]].y;
        i64 w = e[v[x][i]].w;
        if (w > 0 && lv[y] == lv[x] - 1) // 往层数低的方向增广
        {
            i64 c = dfs(y, std::min(w, rmn)); // 尽可能多地传递流量
            rmn -= c; // 剩余流量减少
            e[v[x][i]].w -= c; // 更新残余容量
            e[v[x][i] ^ 1].w += c;
        }
    }
    if (rmn > 0) {//GAP
        gap[lv[x]]--;
        if (gap[lv[x]] == 0)lv[s] = n + 1;
        lv[x]++;
        gap[lv[x]]++;
    }
    return flow - rmn; // 返回传递出去的流量的大小
}

i64 MaxFlow() {
    bfs();
    i64 ans = 0;
    if (lv[s] == -1)return 0;
    while (lv[s] <= n) {
        for (int i = 1; i <= n; i++)cur[i] = 0;
        ans += dfs();
    }
    return ans;
}

void solve() {
    int m;
    std::cin >> n >> m;
    s = n * 2 + 1;
    t = n * 2 + 2;
    for (int i = 1; i <= n * 2 + 2; i++)v[i].clear(), gap[i] = 0;
    e.clear();
    auto AddEdge = [&](int x, int y, i64 w) -> void {
        e.emplace_back(x, 0);
        v[y].push_back((int) e.size() - 1);
        e.emplace_back(y, w);
        v[x].push_back((int) e.size() - 1);
    };
    for (int i = 1; i <= m; i++) {
        int x, y;
        std::cin >> x >> y;
        AddEdge(x, y + n, 1);
    }
    for (int i = 1; i <= n; i++) {
        AddEdge(s, i, 1);
        AddEdge(i + n, t, 1);
    }
    std::vector<bool> vis(n * 2 + 5);
    i64 suma = 0, sumb = 0;
    auto dfs1 = [&](auto &&self, int x) -> void {
        if (x <= n)suma++;
        for (int i: v[x]) {
            int y = e[i].y;
            i64 w = e[i].w;
            if (vis[y])continue;
            if (w > 0) {
                vis[y] = true;
                self(self, y);
            }
        }
    };
    auto dfs2 = [&](auto &&self, int x) -> void {
        if (x > n && x <= 2 * n)sumb++;
        for (int i: v[x]) {
            int y = e[i].y;
            i64 w = e[i].w;
            if (vis[y])continue;
            if (w == 0) {
                vis[y] = true;
                self(self, y);
            }
        }
    };
    n = n * 2 + 2;
    MaxFlow();
    n = (n - 2) / 2;
    vis[s] = true;
    vis[t] = true;
    dfs1(dfs1, s);
    vis.assign(n * 2 + 5, false);
    vis[s] = true;
    vis[t] = true;
    dfs2(dfs2, t);
    std::cout << suma * sumb << '\n';
}

signed main() {
    std::ios::sync_with_stdio(false), std::cin.tie(nullptr);
    int T;
    std::cin >> T;
    while (T--)solve();
}

详细

Test #1:

score: 0
Runtime Error

input:

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

output:


result: