Submission #930298

# Submission time Handle Problem Language Result Execution time Memory
930298 2024-02-19T10:14:04 Z cheat_when_I_was_young Factories (JOI14_factories) C++17
0 / 100
28 ms 68188 KB
#include "factories.h"
#include "bits/stdc++.h"
using namespace std;

const int NM = 5e5 + 5;
const int LG = __lg(NM);

int n;
vector<pair<int, int>> adj[NM];
bool visited[NM];
vector<int> tour;
int depth[NM], tin[NM], tout[NM];
long long dist[NM];
pair<int, int> ST[LG + 2][NM << 1];

void dfs_euler_tour(const int &u) {
    visited[u] = 1;

    tin[u] = tour.size();
    tour.emplace_back(u);

    for (auto &[v, w]: adj[u])
        if (!visited[v]) {
            depth[v] = depth[u] + 1;
            dist[v] = dist[u] + w;

            dfs_euler_tour(v);
            tour.emplace_back(u);
        }

    tout[u] = tour.size() - 1;
}

void build_sparse_table() {
    for (int i = 0; i < tour.size(); ++i)
        ST[0][i] = {depth[tour[i]], tour[i]};

    for (int i = 1; i <= LG + 1; ++i)
        for (int j = 0; j + (1 << i) - 1 < tour.size(); ++j)
            ST[i][j] = min(ST[i - 1][j], ST[i - 1][j + (1 << (i - 1))]);
}

int lca(const int &u, const int &v) {
    int l = tin[u], r = tin[v];
    if (l > r) swap(l, r);

    int k = __lg(r - l + 1);
    return min(ST[k][l], ST[k][r - (1 << k) + 1]).second;
}

void Init(int N, int A[], int B[], int D[]) {
    n = N;

    for (int i = 0; i < n - 1; ++i) {
        adj[A[i]].emplace_back(B[i], D[i]);
        adj[B[i]].emplace_back(A[i], D[i]);
    }

    dfs_euler_tour(0);
    build_sparse_table();
}

bool cmp(const int &u, const int &v) {
    return tin[u] < tin[v];
}

bool is_anc(const int &u, const int &v) {
    return tin[u] <= tin[v] && tin[v] <= tout[u];
}

vector<pair<int, long long>> new_adj[NM];
priority_queue<pair<long long, int>> q;
long long d[NM];

long long Query(int S, int X[], int T, int Y[]) {
    vector<int> node;

    for (int i = 0; i < S; ++i)
        node.emplace_back(X[i]);

    for (int i = 0; i < T; ++i)
        node.emplace_back(Y[i]);

    sort(node.begin(), node.end(), cmp);
    node.erase(unique(node.begin(), node.end()), node.end());

    int tmp = node.size();

    for (int i = 0; i + 1 < tmp; ++i)
        node.emplace_back(lca(node[i], node[i + 1]));

    sort(node.begin(), node.end(), cmp);
    node.erase(unique(node.begin(), node.end()), node.end());

    for (auto &i: node) {
        new_adj[i].clear();
        visited[i] = 0;
        d[i] = 1e18;
    }

    for (int i = 0; i < S; ++i) {
        d[X[i]] = 0;
        q.push({0, X[i]});
    }

    stack<int> st;
    st.push(node[0]);

    for (int i = 1; i < node.size(); ++i) {
        while (!is_anc(st.top(), node[i]))
            st.pop();

        new_adj[st.top()].emplace_back(node[i], dist[node[i]] - dist[st.top()]);
        new_adj[node[i]].emplace_back(st.top(), dist[node[i]] - dist[st.top()]);

        st.push(node[i]);
    }

    while (!q.empty()) {
        int u = q.top().second;
        q.pop();

        if (visited[u]) continue;
        visited[u] = 1;

        for (auto &[v, w]: new_adj[u])
            if (d[v] > d[u] + w) {
                d[v] = d[u] + w;
                q.push({d[v], v});
            }
    }

    long long ans = 1e18;

    for (int i = 0; i < T; ++i)
        ans = min(ans, d[Y[i]]);

    return ans;
}

Compilation message

factories.cpp: In function 'void build_sparse_table()':
factories.cpp:35:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |     for (int i = 0; i < tour.size(); ++i)
      |                     ~~^~~~~~~~~~~~~
factories.cpp:39:42: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |         for (int j = 0; j + (1 << i) - 1 < tour.size(); ++j)
      |                         ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
factories.cpp: In function 'long long int Query(int, int*, int, int*)':
factories.cpp:109:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  109 |     for (int i = 1; i < node.size(); ++i) {
      |                     ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 28 ms 68188 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 13 ms 68184 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 28 ms 68188 KB Output isn't correct
2 Halted 0 ms 0 KB -