Submission #717097

# Submission time Handle Problem Language Result Execution time Memory
717097 2023-04-01T06:43:21 Z LittleCube Swapping Cities (APIO20_swap) C++17
0 / 100
115 ms 74400 KB
#include "swap.h"
#ifndef EVAL
#include "grader.cpp"
#endif
#include <bits/stdc++.h>
#define pii pair<int, int>
#define F first
#define S second
using namespace std;

namespace
{
    int N, M, t, pre[100000][20], out[100000][20], dsu[100000], rk[100000], jump[100000][20];
    pii io[100000];
    vector<pair<int, pii>> edge;
    vector<pii> E[100000];

    int find(int k)
    {
        return dsu[k] == k ? k : dsu[k] = find(dsu[k]);
    }

    void merge(int x, int y)
    {
        x = find(x), y = find(y);
        if (x == y)
            return;
        if (rk[x] < rk[y])
        {
            merge(y, x);
            return;
        }
        dsu[y] = x;
        rk[x] += rk[y];
    }

    void dfs(int u)
    {
        io[u].F = ++t;
        for (auto [v, w] : E[u])
            if (v != pre[u][0])
            {
                pre[v][0] = u;
                jump[v][0] = w;
                dfs(v);
            }
        io[u].S = t;
    }
    bool isChild(int r, int c)
    {
        return io[r].F <= io[c].F && io[c].S <= io[r].S;
    }
}

void init(int N, int M, vector<int> U, vector<int> V, vector<int> W)
{
    ::N = N, ::M = M;
    for (int i = 0; i < N; i++)
        out[i][0] = 2e9, dsu[i] = i, rk[i] = 1;

    for (int i = 0; i < M; i++)
        edge.emplace_back(make_pair(W[i], pii(U[i], V[i])));
    sort(edge.begin(), edge.end());
    for (auto [w, p] : edge)
    {
        auto [u, v] = p;
        if (find(u) == find(v))
            out[u][0] = min(out[u][0], w), out[v][0] = min(out[v][0], w);
        else
        {
            merge(u, v);
            E[u].emplace_back(pii(v, w));
            E[v].emplace_back(pii(u, w));
        }
    }
    dfs(0);
    for (int p = 0; p < 19; p++)
        for (int i = 0; i < N; i++)
        {
            pre[i][p + 1] = pre[pre[i][p]][p];
            jump[i][p + 1] = max(jump[i][p], jump[pre[i][p]][p]);
            out[i][p + 1] = min(out[i][p], out[jump[i][p]][p]);
        }
}

int getMinimumFuelCapacity(int X, int Y)
{
    int lca = X, path = 0, alt = 2e9;
    for (int p = 19; p >= 0; p--)
        if (!isChild(pre[lca][p], Y))
            lca = pre[lca][p];
    if (!isChild(lca, Y))
        lca = pre[lca][0];

    alt = out[lca][0];
    for (int p = 19; p >= 0; p--)
        if (!isChild(pre[X][p], lca))
        {
            path = max(path, jump[X][p]);
            alt = min(alt, out[X][p]);
            X = pre[X][p];
        }
    if (!isChild(X, lca))
    {
        path = max(path, jump[X][0]);
        alt = min(alt, out[X][0]);
        X = pre[X][0];
    }

    for (int p = 19; p >= 0; p--)
        if (!isChild(pre[Y][p], lca))
        {
            path = max(path, jump[Y][p]);
            alt = min(alt, out[Y][p]);
            Y = pre[Y][p];
        }
    if (!isChild(Y, lca))
    {
        path = max(path, jump[Y][0]);
        alt = min(alt, out[Y][0]);
        Y = pre[Y][0];
    }
    int ans = max(path, alt);
    return (ans > 1e9 ? -1 : ans);
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Runtime error 4 ms 5460 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Runtime error 115 ms 74400 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Runtime error 4 ms 5460 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Runtime error 4 ms 5460 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Runtime error 4 ms 5460 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Runtime error 4 ms 5460 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -