답안 #964752

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
964752 2024-04-17T13:29:50 Z unkn0t 공장들 (JOI14_factories) C++17
0 / 100
1521 ms 206936 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          500000
 
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, int 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));
    memset(cls, 0, sizeof(cls));
    for (int i = 0; i < N; ++i) gr[i].clear();
    now = 1;

    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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 43612 KB Output is correct
2 Correct 248 ms 59552 KB Output is correct
3 Incorrect 230 ms 59604 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 43676 KB Output is correct
2 Incorrect 1521 ms 206936 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 43612 KB Output is correct
2 Correct 248 ms 59552 KB Output is correct
3 Incorrect 230 ms 59604 KB Output isn't correct
4 Halted 0 ms 0 KB -