답안 #1084141

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1084141 2024-09-05T10:57:10 Z f0rizen 공장들 (JOI14_factories) C++17
100 / 100
2999 ms 236696 KB
#include "factories.h"

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const int inf = 1e9 + 7;
const ll infll = 1e18;

struct Edge {
    int to, w;
};

vector<vector<Edge>> g;
vector<bool> used;
vector<int> sz, par;
vector<vector<ll>> dist;
vector<ll> closest;

void dfs_sz(int v, int p = -1) {
    sz[v] = 1;
    for (auto [u, w] : g[v]) {
        if (u != p && !used[u]) {
            dfs_sz(u, v);
            sz[v] += sz[u];
        }
    }
}

void dfs_dist(int v, int p = -1, ll d = 0) {
    dist[v].push_back(d);
    for (auto [u, w] : g[v]) {
        if (u != p && !used[u]) {
            dfs_dist(u, v, d + w);
        }
    }
}

int centroid(int v, int p, int n) {
    for (auto [u, w] : g[v]) {
        if (u != p && !used[u]) {
            if (sz[u] * 2 > n) {
                return centroid(u, v, n);
            }
        }
    }
    return v;
}

void build(int v, int p = -1) {
    dfs_sz(v);
    dfs_dist(v);
    par[v] = p;
    used[v] = 1;
    for (auto [u, w] : g[v]) {
        if (!used[u]) {
            build(centroid(u, v, sz[u]), v);
        }
    }
}

void Init(int N, int A[], int B[], int D[]) {
    g.resize(N);
    for (int i = 0; i < N - 1; ++i) {
        int u = A[i], v = B[i], w = D[i];
        g[u].push_back({v, w});
        g[v].push_back({u, w});
    }
    used.resize(N);
    sz.resize(N);
    par.resize(N);
    dist.resize(N);
    dfs_sz(0);
    build(centroid(0, -1, sz[0]));
    closest.resize(N, infll);
}

long long Query(int S, int X[], int T, int Y[]) {
    for (int i = 0; i < S; ++i) {
        int v = X[i];
        int u = v;
        int j = (int) dist[v].size() - 1;
        while (u != -1) {
            closest[u] = min(closest[u], dist[v][j]);
            u = par[u];
            --j;
        }
    }
    ll ans = infll;
    for (int i = 0; i < T; ++i) {
        int v = Y[i];
        int u = v;
        int j = (int) dist[v].size() - 1;
        while (u != -1) {
            ans = min(ans, closest[u] + dist[v][j]);
            u = par[u];
            --j;
        }
    }
    for (int i = 0; i < S; ++i) {
        int v = X[i];
        int u = v;
        int j = (int) dist[v].size() - 1;
        while (u != -1) {
            closest[u] = infll;
            u = par[u];
            --j;
        }
    }
    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 604 KB Output is correct
2 Correct 184 ms 9312 KB Output is correct
3 Correct 207 ms 9620 KB Output is correct
4 Correct 202 ms 9588 KB Output is correct
5 Correct 238 ms 9808 KB Output is correct
6 Correct 135 ms 9044 KB Output is correct
7 Correct 195 ms 9536 KB Output is correct
8 Correct 209 ms 9556 KB Output is correct
9 Correct 218 ms 9936 KB Output is correct
10 Correct 131 ms 9040 KB Output is correct
11 Correct 206 ms 9500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 604 KB Output is correct
2 Correct 1487 ms 129672 KB Output is correct
3 Correct 2206 ms 183124 KB Output is correct
4 Correct 513 ms 97976 KB Output is correct
5 Correct 2803 ms 232496 KB Output is correct
6 Correct 2125 ms 185008 KB Output is correct
7 Correct 546 ms 49452 KB Output is correct
8 Correct 250 ms 39108 KB Output is correct
9 Correct 666 ms 57408 KB Output is correct
10 Correct 601 ms 50768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 604 KB Output is correct
2 Correct 184 ms 9312 KB Output is correct
3 Correct 207 ms 9620 KB Output is correct
4 Correct 202 ms 9588 KB Output is correct
5 Correct 238 ms 9808 KB Output is correct
6 Correct 135 ms 9044 KB Output is correct
7 Correct 195 ms 9536 KB Output is correct
8 Correct 209 ms 9556 KB Output is correct
9 Correct 218 ms 9936 KB Output is correct
10 Correct 131 ms 9040 KB Output is correct
11 Correct 206 ms 9500 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 1487 ms 129672 KB Output is correct
14 Correct 2206 ms 183124 KB Output is correct
15 Correct 513 ms 97976 KB Output is correct
16 Correct 2803 ms 232496 KB Output is correct
17 Correct 2125 ms 185008 KB Output is correct
18 Correct 546 ms 49452 KB Output is correct
19 Correct 250 ms 39108 KB Output is correct
20 Correct 666 ms 57408 KB Output is correct
21 Correct 601 ms 50768 KB Output is correct
22 Correct 1566 ms 154640 KB Output is correct
23 Correct 1632 ms 158208 KB Output is correct
24 Correct 2388 ms 191396 KB Output is correct
25 Correct 2431 ms 195188 KB Output is correct
26 Correct 2384 ms 191824 KB Output is correct
27 Correct 2993 ms 236696 KB Output is correct
28 Correct 549 ms 108220 KB Output is correct
29 Correct 2438 ms 191060 KB Output is correct
30 Correct 2564 ms 190992 KB Output is correct
31 Correct 2999 ms 191360 KB Output is correct
32 Correct 657 ms 58060 KB Output is correct
33 Correct 239 ms 39472 KB Output is correct
34 Correct 393 ms 44988 KB Output is correct
35 Correct 393 ms 45152 KB Output is correct
36 Correct 578 ms 47724 KB Output is correct
37 Correct 586 ms 47596 KB Output is correct