답안 #483269

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
483269 2021-10-28T12:04:14 Z Alexandruabcde 공장들 (JOI14_factories) C++14
100 / 100
5150 ms 364372 KB
#include <bits/stdc++.h>
#include "factories.h"

using namespace std;

constexpr int NMAX = 5e5 + 5;
typedef long long LL;
typedef pair <int, LL> PIL;

vector <PIL> G[NMAX];
vector <PIL> Root[NMAX];

bool viz[NMAX];
int Size[NMAX];

LL Dist[NMAX];

void Initial_Dfs (int Node, int dad = -1) {
    Size[Node] = 1;

    for (auto it : G[Node]) {
        int to = it.first;
        if (to == dad || viz[to]) continue;

        Initial_Dfs(to, Node);
        Size[Node] += Size[to];
    }
}

int GetCentroid (int Node, int Sz, int dad = -1) {
    int Max = Sz - Size[Node];
    for (auto it : G[Node]) {
        int to = it.first;
        if (to == dad || viz[to]) continue;

        int x = GetCentroid(to, Sz, Node);

        if (x != 0) return x;

        Max = max(Max, Size[to]);
    }

    if (Max <= (Sz + 1) / 2) return Node;

    return 0;
}

void Add (int Node, int cent, LL dist, int dad = -1) {
    Root[Node].push_back({cent, dist});

    for (auto it : G[Node]) {
        int to = it.first;

        if (to == dad || viz[to]) continue;

        Add(to, cent, dist + it.second, Node);
    }
}

void CentroidDecomposition (int Node) {
    Initial_Dfs(Node);

    int centroid = GetCentroid(Node, Size[Node]);
    Add(centroid, centroid, 0);

    viz[centroid] = 1;

    for (auto it : G[centroid]) {
        int to = it.first;

        if (!viz[to]) CentroidDecomposition(to);
    }
}

void Init (int N, int A[], int B[], int D[]) {
    for (int i = 0; i < N-1; ++ i ) {
        ++ A[i];
        ++ B[i];
        G[A[i]].push_back({B[i], D[i]});
        G[B[i]].push_back({A[i], D[i]});
    }

    CentroidDecomposition(1);

    for (int i = 1; i <= N; ++ i )
        Dist[i] = -1;
}

void Compute (int Node) {
    for (auto it : Root[Node]) {
        if (Dist[it.first] == -1) Dist[it.first] = it.second;

        Dist[it.first] = min(Dist[it.first], it.second);
    }
}

LL Minimum (int Node) {
    LL ans = 1000000000000000;
    for (auto it : Root[Node]) {
        if (Dist[it.first] == -1) continue;

        ans = min(ans, Dist[it.first] + it.second);
    }

    return ans;
}

void Delete (int Node) {
    for (auto it : Root[Node])
        Dist[it.first] = -1;
}

long long Query (int S, int X[], int T, int Y[]) {
    for (int i = 0; i < S; ++ i )
        Compute(X[i]+1);

    LL ans = 1000000000000000;

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

    for (int i = 0; i < S; ++ i )
        Delete(X[i] + 1);

    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 24268 KB Output is correct
2 Correct 300 ms 42336 KB Output is correct
3 Correct 331 ms 42952 KB Output is correct
4 Correct 319 ms 42840 KB Output is correct
5 Correct 364 ms 43340 KB Output is correct
6 Correct 210 ms 41868 KB Output is correct
7 Correct 318 ms 42804 KB Output is correct
8 Correct 397 ms 42840 KB Output is correct
9 Correct 342 ms 43264 KB Output is correct
10 Correct 209 ms 41916 KB Output is correct
11 Correct 321 ms 42956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 24004 KB Output is correct
2 Correct 2378 ms 195960 KB Output is correct
3 Correct 3741 ms 287532 KB Output is correct
4 Correct 887 ms 99496 KB Output is correct
5 Correct 4933 ms 364372 KB Output is correct
6 Correct 4139 ms 286496 KB Output is correct
7 Correct 1035 ms 83820 KB Output is correct
8 Correct 469 ms 59932 KB Output is correct
9 Correct 1188 ms 96656 KB Output is correct
10 Correct 1008 ms 85080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 24268 KB Output is correct
2 Correct 300 ms 42336 KB Output is correct
3 Correct 331 ms 42952 KB Output is correct
4 Correct 319 ms 42840 KB Output is correct
5 Correct 364 ms 43340 KB Output is correct
6 Correct 210 ms 41868 KB Output is correct
7 Correct 318 ms 42804 KB Output is correct
8 Correct 397 ms 42840 KB Output is correct
9 Correct 342 ms 43264 KB Output is correct
10 Correct 209 ms 41916 KB Output is correct
11 Correct 321 ms 42956 KB Output is correct
12 Correct 14 ms 24004 KB Output is correct
13 Correct 2378 ms 195960 KB Output is correct
14 Correct 3741 ms 287532 KB Output is correct
15 Correct 887 ms 99496 KB Output is correct
16 Correct 4933 ms 364372 KB Output is correct
17 Correct 4139 ms 286496 KB Output is correct
18 Correct 1035 ms 83820 KB Output is correct
19 Correct 469 ms 59932 KB Output is correct
20 Correct 1188 ms 96656 KB Output is correct
21 Correct 1008 ms 85080 KB Output is correct
22 Correct 2979 ms 203108 KB Output is correct
23 Correct 3042 ms 207884 KB Output is correct
24 Correct 4460 ms 278308 KB Output is correct
25 Correct 4449 ms 282240 KB Output is correct
26 Correct 4378 ms 279512 KB Output is correct
27 Correct 5150 ms 352392 KB Output is correct
28 Correct 1227 ms 101460 KB Output is correct
29 Correct 4325 ms 278616 KB Output is correct
30 Correct 4208 ms 278340 KB Output is correct
31 Correct 4217 ms 278632 KB Output is correct
32 Correct 1259 ms 96964 KB Output is correct
33 Correct 428 ms 60244 KB Output is correct
34 Correct 857 ms 75356 KB Output is correct
35 Correct 809 ms 76228 KB Output is correct
36 Correct 1004 ms 81892 KB Output is correct
37 Correct 1016 ms 82004 KB Output is correct