Submission #951592

# Submission time Handle Problem Language Result Execution time Memory
951592 2024-03-22T07:09:27 Z noyancanturk Factories (JOI14_factories) C++17
100 / 100
3657 ms 222744 KB
#include "factories.h"

#pragma GCC optimize("O3,unroll-loops")
    
#include "bits/stdc++.h"
using namespace std;
    
#define pb push_back
    
const int lim=5e5+100;
const int64_t inf=lim*1e9;

using pii=pair<int,int64_t>;

vector<pii>v[lim];

bool vis[lim];
int tot,cent,layer,sz[lim];
int onlayer[lim],centpar[20][lim];
int64_t dist[20][lim];

inline int sbs(int node,int par){
    sz[node]=1;
    for(pii p:v[node]){
        if(!vis[p.first]&&(p.first^par)){
            sz[node]+=sbs(p.first,node);
        }
    }
    return sz[node];
}

inline int findcent(int node,int par){
    for(pii p:v[node]){
        if(!vis[p.first]&&(p.first^par)&&tot<2*sz[p.first]){
            return findcent(p.first,node);
        }
    }
    return node;
}

inline void distdfs(int node,int par){
    centpar[layer][node]=cent;
    for(pii p:v[node]){
        if(!vis[p.first]&&(p.first^par)){
            dist[layer][p.first]=dist[layer][node]+p.second;
            distdfs(p.first,node);
        }
    }
}

inline void decomp(int node,int dep=0){
    layer=dep;
    tot=sbs(node,-1);
    onlayer[cent=findcent(node,-1)]=dep;
    vis[cent]=1;
    dist[layer][cent]=0;
    distdfs(cent,-1);
    for(pii p:v[cent]){
        if(!vis[p.first]){
            decomp(p.first,dep+1);
        }
    }
}

int64_t curvals[lim];

void Init(int N, int A[], int B[], int D[]) {
    for(int i=0;i<N-1;i++){
        v[A[i]].pb({B[i],D[i]});
        v[B[i]].pb({A[i],D[i]});
    }
    for(int i=0;i<N;i++){
        curvals[i]=inf;
    }
    decomp(0);
}

long long Query(int S, int X[], int T, int Y[]) {
    for(int i=0;i<S;i++){
        for(int j=onlayer[X[i]];0<=j;j--){
            curvals[centpar[j][X[i]]]=min(curvals[centpar[j][X[i]]],dist[j][X[i]]);
        }
    }
    int64_t ans=inf;
    for(int i=0;i<T;i++){
        for(int j=onlayer[Y[i]];0<=j;j--){
            ans=min(ans,curvals[centpar[j][Y[i]]]+dist[j][Y[i]]);
        }
    }
    for(int i=0;i<S;i++){
        for(int j=onlayer[X[i]];0<=j;j--){
            curvals[centpar[j][X[i]]]=inf;
        }
    }
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 27 ms 66128 KB Output is correct
2 Correct 210 ms 94288 KB Output is correct
3 Correct 222 ms 102464 KB Output is correct
4 Correct 222 ms 102340 KB Output is correct
5 Correct 229 ms 102736 KB Output is correct
6 Correct 162 ms 55376 KB Output is correct
7 Correct 217 ms 98384 KB Output is correct
8 Correct 222 ms 102436 KB Output is correct
9 Correct 227 ms 102720 KB Output is correct
10 Correct 158 ms 55380 KB Output is correct
11 Correct 222 ms 98260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 66140 KB Output is correct
2 Correct 1347 ms 175548 KB Output is correct
3 Correct 2247 ms 199492 KB Output is correct
4 Correct 434 ms 100240 KB Output is correct
5 Correct 3075 ms 212548 KB Output is correct
6 Correct 2452 ms 201320 KB Output is correct
7 Correct 668 ms 146076 KB Output is correct
8 Correct 247 ms 73408 KB Output is correct
9 Correct 833 ms 147676 KB Output is correct
10 Correct 691 ms 146772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 66128 KB Output is correct
2 Correct 210 ms 94288 KB Output is correct
3 Correct 222 ms 102464 KB Output is correct
4 Correct 222 ms 102340 KB Output is correct
5 Correct 229 ms 102736 KB Output is correct
6 Correct 162 ms 55376 KB Output is correct
7 Correct 217 ms 98384 KB Output is correct
8 Correct 222 ms 102436 KB Output is correct
9 Correct 227 ms 102720 KB Output is correct
10 Correct 158 ms 55380 KB Output is correct
11 Correct 222 ms 98260 KB Output is correct
12 Correct 10 ms 66140 KB Output is correct
13 Correct 1347 ms 175548 KB Output is correct
14 Correct 2247 ms 199492 KB Output is correct
15 Correct 434 ms 100240 KB Output is correct
16 Correct 3075 ms 212548 KB Output is correct
17 Correct 2452 ms 201320 KB Output is correct
18 Correct 668 ms 146076 KB Output is correct
19 Correct 247 ms 73408 KB Output is correct
20 Correct 833 ms 147676 KB Output is correct
21 Correct 691 ms 146772 KB Output is correct
22 Correct 1775 ms 182000 KB Output is correct
23 Correct 2119 ms 187816 KB Output is correct
24 Correct 3184 ms 212368 KB Output is correct
25 Correct 2948 ms 214336 KB Output is correct
26 Correct 2954 ms 212644 KB Output is correct
27 Correct 3657 ms 222744 KB Output is correct
28 Correct 616 ms 106632 KB Output is correct
29 Correct 2814 ms 212652 KB Output is correct
30 Correct 2881 ms 212172 KB Output is correct
31 Correct 2907 ms 212496 KB Output is correct
32 Correct 799 ms 148164 KB Output is correct
33 Correct 241 ms 73348 KB Output is correct
34 Correct 464 ms 136532 KB Output is correct
35 Correct 445 ms 136292 KB Output is correct
36 Correct 709 ms 144916 KB Output is correct
37 Correct 677 ms 145172 KB Output is correct