Submission #571324

# Submission time Handle Problem Language Result Execution time Memory
571324 2022-06-01T18:46:47 Z timreizin Factories (JOI14_factories) C++17
100 / 100
3823 ms 155660 KB
#include "factories.h"
#include <vector>
#include <array>
#include <algorithm>
#include <stack>
#include <queue>

using ll = long long;
using namespace std;

vector<int> tin, tout;
vector<vector<pair<int, ll>>> adj;
vector<ll> d, h;
vector<bool> used;
vector<array<int, 20>> up;

void Init(int n, int A[], int B[], int D[])
{
    tin.resize(n);
    tout.resize(n);
    h.resize(n);
    up.resize(n);
    d.resize(n, 1e18);
    used.resize(n);
    ::adj.resize(n);
    vector<vector<pair<int, ll>>> adj(n);
    for (int i = 0; i + 1 < n; ++i)
    {
        adj[A[i]].emplace_back(B[i], D[i]);
        adj[B[i]].emplace_back(A[i], D[i]);
    }
    int t = 0;
    auto dfs = [&t, &adj](auto self, int v, int p) -> void
    {
        up[v][0] = p;
        for (int i = 1; i < 20; ++i) up[v][i] = up[up[v][i - 1]][i - 1];
        tin[v] = t++;
        for (auto [u, w] : adj[v])
        {
            if (u != p)
            {
                h[u] = h[v] + w;
                self(self, u, v);
            }
        }
        tout[v] = t - 1;
    };
    dfs(dfs, 0, 0);
}

bool isParent(int p, int c)
{
    return tin[p] <= tin[c] && tout[p] >= tout[c];
}

int lca(int a, int b)
{
    if (isParent(a, b)) return a;
    if (isParent(b, a)) return b;
    for (int i = 19; i >= 0; --i) if (!isParent(up[a][i], b)) a = up[a][i];
    return up[a][0];
}

ll dist(int a, int b)
{
    return h[a] + h[b] - 2 * h[lca(a, b)];
}

ll Query(int s, int x[], int t, int y[])
{
    vector<int> vertices;
    for (int i = 0; i < s; ++i) vertices.push_back(x[i]);
    for (int i = 0; i < t; ++i) vertices.push_back(y[i]);
    sort(vertices.begin(), vertices.end(), [](int a, int b){ return tin[a] < tin[b]; });
    int sz = vertices.size();
    for (int i = 0; i + 1 < sz; ++i) vertices.push_back(lca(vertices[i], vertices[i + 1]));
    sort(vertices.begin(), vertices.end(), [](int a, int b){ return tin[a] < tin[b]; });
    vertices.erase(unique(vertices.begin(), vertices.end()), vertices.end());
    stack<int> help;
    help.push(vertices.front());
    for (int i = 1; i < vertices.size(); ++i)
    {
        while (!isParent(help.top(), vertices[i])) help.pop();
        adj[help.top()].emplace_back(vertices[i], dist(help.top(), vertices[i]));
        adj[vertices[i]].emplace_back(help.top(), dist(help.top(), vertices[i]));
        help.push(vertices[i]);
    }
    priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> pq;
    for (int i = 0; i < s; ++i)
    {
        pq.push({0, x[i]});
        d[x[i]] = 0;
    }
    while (!pq.empty())
    {
        auto [ndn, v] = pq.top();
        pq.pop();
        if (used[v]) continue;
        used[v] = true;
        for (auto [u, w] : adj[v])
        {
            if (d[u] > d[v] + w)
            {
                d[u] = d[v] + w;
                pq.push({d[u], u});
            }
        }
    }
    ll res = 1e18;
    for (int i = 0; i < t; ++i) res = min(res, d[y[i]]);
    for (int i : vertices)
    {
        used[i] = false;
        adj[i].clear();
        d[i] = 1e18;
    }
    return res;
}

Compilation message

factories.cpp: In function 'll Query(int, int*, int, int*)':
factories.cpp:81:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |     for (int i = 1; i < vertices.size(); ++i)
      |                     ~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 22 ms 596 KB Output is correct
2 Correct 1083 ms 18764 KB Output is correct
3 Correct 1097 ms 18916 KB Output is correct
4 Correct 980 ms 19220 KB Output is correct
5 Correct 840 ms 19096 KB Output is correct
6 Correct 678 ms 18836 KB Output is correct
7 Correct 1040 ms 19016 KB Output is correct
8 Correct 984 ms 18836 KB Output is correct
9 Correct 797 ms 19088 KB Output is correct
10 Correct 716 ms 18832 KB Output is correct
11 Correct 1008 ms 18916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 468 KB Output is correct
2 Correct 1668 ms 130076 KB Output is correct
3 Correct 2157 ms 132224 KB Output is correct
4 Correct 1214 ms 127632 KB Output is correct
5 Correct 1645 ms 150696 KB Output is correct
6 Correct 2351 ms 133460 KB Output is correct
7 Correct 1938 ms 44204 KB Output is correct
8 Correct 1159 ms 43612 KB Output is correct
9 Correct 1016 ms 46600 KB Output is correct
10 Correct 1879 ms 44792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 596 KB Output is correct
2 Correct 1083 ms 18764 KB Output is correct
3 Correct 1097 ms 18916 KB Output is correct
4 Correct 980 ms 19220 KB Output is correct
5 Correct 840 ms 19096 KB Output is correct
6 Correct 678 ms 18836 KB Output is correct
7 Correct 1040 ms 19016 KB Output is correct
8 Correct 984 ms 18836 KB Output is correct
9 Correct 797 ms 19088 KB Output is correct
10 Correct 716 ms 18832 KB Output is correct
11 Correct 1008 ms 18916 KB Output is correct
12 Correct 3 ms 468 KB Output is correct
13 Correct 1668 ms 130076 KB Output is correct
14 Correct 2157 ms 132224 KB Output is correct
15 Correct 1214 ms 127632 KB Output is correct
16 Correct 1645 ms 150696 KB Output is correct
17 Correct 2351 ms 133460 KB Output is correct
18 Correct 1938 ms 44204 KB Output is correct
19 Correct 1159 ms 43612 KB Output is correct
20 Correct 1016 ms 46600 KB Output is correct
21 Correct 1879 ms 44792 KB Output is correct
22 Correct 3645 ms 138416 KB Output is correct
23 Correct 3226 ms 139424 KB Output is correct
24 Correct 3788 ms 141168 KB Output is correct
25 Correct 3764 ms 142764 KB Output is correct
26 Correct 3823 ms 141564 KB Output is correct
27 Correct 2824 ms 155660 KB Output is correct
28 Correct 2136 ms 137324 KB Output is correct
29 Correct 3720 ms 141356 KB Output is correct
30 Correct 3715 ms 140868 KB Output is correct
31 Correct 3582 ms 141372 KB Output is correct
32 Correct 1432 ms 50204 KB Output is correct
33 Correct 1186 ms 46672 KB Output is correct
34 Correct 1795 ms 42776 KB Output is correct
35 Correct 1722 ms 42804 KB Output is correct
36 Correct 1866 ms 43524 KB Output is correct
37 Correct 1822 ms 43532 KB Output is correct