답안 #908180

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
908180 2024-01-16T09:10:37 Z typ_ik 공장들 (JOI14_factories) C++17
15 / 100
8000 ms 147544 KB
#include <bits/stdc++.h>
#include "factories.h"

#define ll long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define watch(x) cout << (#x) << " : " << x << '\n'
#define boost ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std; 

const int N = 5e5 + 128;
const ll INF = 1e15 + 128;
const int LOG = 19;
int lift[N][LOG], depth[N];
ll d[N];
vector < pair<int, int> > g[N];
int n, q;
bool was[N];
int h[N]; 
ll res[N];

void init(int v, ll val = 0, int p = 1) {
    d[v] = val;

    if (v != 1)
        depth[v] = depth[p] + 1;

    lift[v][0] = p;
    for (int b = 1; b < LOG; b++)
        lift[v][b] = lift[lift[v][b-1]][b-1];

    for (auto& [to, len] : g[v])
        if (to != p)
            init(to, val+len, v);
}

int lca(int u, int v) {
    if (depth[u] < depth[v])
        swap(u, v);
    int k = depth[u] - depth[v];
    for (int b = 0; b < LOG; b++)
        if ((k >> b) & 1)
            u = lift[u][b];
    if (u == v)
        return u;
    for (int b = LOG - 1; b >= 0; b--)
        if (lift[v][b] != lift[u][b])
            v = lift[v][b], u = lift[u][b];
    return lift[v][0];
}

ll get(int u, int v) {
    return d[u]+d[v]-2*d[lca(u,v)];
}

void dfs(int v, int p = -1) {
    h[v] = 1;   

    for (auto& [to, len] : g[v])
        if (to != p && !was[to])
            dfs(to, v), h[v] += h[to];
} 

int find_centroid(int v, int p, int sz) { 
    for (auto& [to, len] : g[v])
        if (to != p && h[to] > sz / 2 && !was[to])
            return find_centroid(to, v, sz);
    return v;
}

int p[N];

void decompose(int v, int pr = -1) { 
    dfs(v); 

    int c = find_centroid(v, -1, h[v]);   

    p[c] = pr;
    was[c] = true;

    for (auto& [to, len] : g[c])
        if (!was[to])
            decompose(to, c);
}

void update(int v) {
    int c = v;
    while (c != -1) 
        res[c] = min(res[c], get(c, v)), c = p[c];
}

void reset(int v) {
    int c = v;
    while (c != -1) 
        res[c] = INF, c = p[c]; 
}

ll get(int v) {
    ll ans = INF;
    int c = v;
    while (c != -1)
        ans = min(ans, get(c,v)+res[c]), c = p[c];
    return ans;
}

void Init(int N, int A[], int B[], int D[]) {
    n = N;
    for (int i = 0; i + 1 < n; i++) {
        int u = A[i], v = B[i], w = D[i];
        g[u].push_back(make_pair(v, w));
        g[v].push_back(make_pair(u, w));
    }
    for (int i = 0; i <= n; i++)
        res[i] = INF, p[i] = -1;
    decompose(1);
    init(1);
}

long long Query(int S, int X[], int T, int Y[]) {
    for (int i = 0; i < S; i++)
        update(X[i]);
    ll tot = INF;
    for (int i = 0; i < T; i++)
        tot = min(tot, get(Y[i]));
    for (int i = 0; i < S; i++)
        reset(X[i]);
    return tot;
}   
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 41508 KB Output is correct
2 Correct 520 ms 43716 KB Output is correct
3 Correct 1110 ms 44004 KB Output is correct
4 Correct 1073 ms 44004 KB Output is correct
5 Correct 1261 ms 44016 KB Output is correct
6 Correct 209 ms 43608 KB Output is correct
7 Correct 1083 ms 43760 KB Output is correct
8 Correct 1146 ms 43772 KB Output is correct
9 Correct 1318 ms 43908 KB Output is correct
10 Correct 216 ms 43768 KB Output is correct
11 Correct 1090 ms 43760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 39468 KB Output is correct
2 Correct 2534 ms 105328 KB Output is correct
3 Correct 5364 ms 107584 KB Output is correct
4 Correct 863 ms 106308 KB Output is correct
5 Execution timed out 8038 ms 147544 KB Time limit exceeded
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 41508 KB Output is correct
2 Correct 520 ms 43716 KB Output is correct
3 Correct 1110 ms 44004 KB Output is correct
4 Correct 1073 ms 44004 KB Output is correct
5 Correct 1261 ms 44016 KB Output is correct
6 Correct 209 ms 43608 KB Output is correct
7 Correct 1083 ms 43760 KB Output is correct
8 Correct 1146 ms 43772 KB Output is correct
9 Correct 1318 ms 43908 KB Output is correct
10 Correct 216 ms 43768 KB Output is correct
11 Correct 1090 ms 43760 KB Output is correct
12 Correct 10 ms 39468 KB Output is correct
13 Correct 2534 ms 105328 KB Output is correct
14 Correct 5364 ms 107584 KB Output is correct
15 Correct 863 ms 106308 KB Output is correct
16 Execution timed out 8038 ms 147544 KB Time limit exceeded
17 Halted 0 ms 0 KB -