Submission #964755

# Submission time Handle Problem Language Result Execution time Memory
964755 2024-04-17T13:37:54 Z unkn0t Factories (JOI14_factories) C++17
0 / 100
8000 ms 191820 KB
#include <bits/stdc++.h>

#ifdef DEBUG
    #include <debug.hpp>
#else
    #define debug(...) 42
#endif

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using namespace std;

const ll INF = 1e18;
#define MAX_N          500005
 
vector<pair<int, ll>> gr[MAX_N];
int sz[MAX_N], used[MAX_N], act[MAX_N], cur[MAX_N];
ll cls[MAX_N];
int cent[MAX_N][20];
ll dist[MAX_N][20];
int now = 1;

int find_sz(int v, int p) {
    sz[v] = 1;
    for (auto [to, w] : gr[v]) {
        if (used[to] || to == p) continue;
        sz[v] += find_sz(to, v);
    }
    return sz[v];
}

int centr(int v, int p, int cn) {
    bool flag = 1;
    while (flag) {
        flag = 0;
        for (auto [to, w] : gr[v]) {
            if (used[to] || to == p) continue;
            if (sz[to] > cn / 2) {
                p = v;
                v = to;
                flag = 1;
                break;
            }
        }
    }
    return v;
}

void dfs(int v, int c, int p = -1, ll d = 0) {
    cent[v][cur[v]] = c;
    dist[v][cur[v]] = d;
    ++cur[v];
    for (auto [to, w] : gr[v]) {
        if (used[to] || to == p) continue;
        dfs(to, c, v, d + w);
    }
} 

void Decomp(int v) {
    int cn = find_sz(v, v);
    int c = centr(v, v, cn); 
    used[c] = 1;
    dfs(c, c);
    for (auto [to, w] : gr[c]) {
        if (used[to]) continue;
        Decomp(to);
    }
}

void Init(int N, int A[], int B[], int D[]) {
    memset(used, 0, sizeof(used));
    memset(act, 0, sizeof(act));
    memset(cur, 0, sizeof(cur));
    for (int i = 0; i < N; ++i) {
        cls[i] = INF;
        gr[i].clear();
    }
    now = 0;

    for (int i = 0; i < N; ++i) {
        gr[A[i]].emplace_back(B[i], D[i]);
        gr[B[i]].emplace_back(A[i], D[i]);
    }

    Decomp(0);
}

void Update(int v) {
    for (int j = 0; j < cur[v]; ++j) {
        int c = cent[v][j];
        if (act[c] == now) {
            cls[c] = min(cls[c], dist[v][j]);
        } else {
            cls[c] = dist[v][j]; 
            act[c] = now;
        }
    }
}

ll GetMin(int v) {
    ll res = INF;
    for (int j = 0; j < cur[v]; ++j) {
        int c = cent[v][j];
        if (act[c] == now) {
            res = min(res, dist[v][j] + cls[c]);
        }
    }
    return res;
}

ll Query(int S, int X[], int T, int Y[]) {
    // go through all X[i] centroids and update cls  
    for (int i = 0; i < S; ++i) {
        Update(X[i]);
    }

    // go through all Y[i] centroids and update min(res, dist[Y[i]][c] + cls[c])
    ll res = INF;
    for (int i = 0; i < T; ++i) {
        res = min(res, GetMin(Y[i])); 
    }

    // Update actuality
    ++now;

    return res;
}
# Verdict Execution time Memory Grader output
1 Correct 19 ms 41560 KB Output is correct
2 Correct 208 ms 48100 KB Output is correct
3 Correct 211 ms 47952 KB Output is correct
4 Correct 221 ms 48080 KB Output is correct
5 Incorrect 286 ms 48344 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 41412 KB Output is correct
2 Correct 1694 ms 188156 KB Output is correct
3 Execution timed out 8010 ms 191820 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 19 ms 41560 KB Output is correct
2 Correct 208 ms 48100 KB Output is correct
3 Correct 211 ms 47952 KB Output is correct
4 Correct 221 ms 48080 KB Output is correct
5 Incorrect 286 ms 48344 KB Output isn't correct
6 Halted 0 ms 0 KB -