Submission #922485

# Submission time Handle Problem Language Result Execution time Memory
922485 2024-02-05T14:47:12 Z AlphaMale06 Factories (JOI14_factories) C++14
100 / 100
6119 ms 159004 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
#define pb push_back
#define F first
#define S second


const int mxn = 5e5+3;

vector<pair<int, int>> adj[mxn];
int tin[mxn], tout[mxn];
int p[mxn][20];
ll dst[mxn];
int timer = -1;
int sz[mxn];
bool mark[mxn];
int cp[mxn];
ll cendist[mxn];
int en;

void dfs(int v, int par){
    p[v][0]=par;
    tin[v]=++timer;
    dst[v]+=dst[par];
    for(int i=1; i<20; i++){
        p[v][i]=p[p[v][i-1]][i-1];
    }
    for(auto to : adj[v]){
        if(to.F!=par){
            dst[to.F]=to.S;
            dfs(to.F, v);
        }
    }
    tout[v]=timer;
}


bool anc(int u, int v){
    if(tin[u]<=tin[v] && tout[u]>=tout[v])return 1;
    return 0;
}

int lca(int u, int v){
    if(anc(u, v))return u;
    if(anc(v, u))return v;
    for(int i=19; i>=0; i--){
        if(!anc(p[u][i], v))u=p[u][i];
    }
    return p[u][0];
}


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

void subtreesize(int v, int par){
    sz[v]=1;
    for(auto to : adj[v]){
        if(to.F!=par && !mark[to.F]){
            subtreesize(to.F, v);
            sz[v]+=sz[to.F];
        }
    }
}

int centr(int v, int par, int siz){
    for(auto to : adj[v]){
        if(to.F==par || mark[to.F])continue;
        if(sz[to.F]*2>=siz)return centr(to.F, v, siz);
    }
    return v;
}

void decompose(int v, int par){
    subtreesize(v, par);
    int centroid = centr(v, par, sz[v]);
    mark[centroid]=1;
    if(par!=-1){
        cp[centroid]=par;
    }
    for(auto to : adj[centroid]){
        if(!mark[to.F])decompose(to.F, centroid);
    }
}


void Init(int n, int u[], int v[], int w[]) {
    en=n;
    for(int i=0; i<n-1; i++){
        adj[u[i]].pb({v[i], w[i]});
        adj[v[i]].pb({u[i], w[i]});
    }
    dfs(0, 0);
    for(int i=0; i< n; i++)cp[i]=i;
    decompose(0, -1);
    for(int i=0; i< n; i++)cendist[i]=1e14;
}

long long Query(int sz1, int a[], int sz2, int b[]) {
    for(int i=0; i<sz2; i++){
        int v=b[i];
        cendist[v]=0;
        while(v!=cp[v]){
            v=cp[v];
            cendist[v]=min(cendist[v], dist(b[i], v));
        }
    }
    ll ans=1e18;
    for(int i=0; i<sz1; i++){
        int v=a[i];
        ans=min(ans, cendist[v]+dist(v, a[i]));
        while(v!=cp[v]){
            v=cp[v];
            ans=min(ans, cendist[v]+dist(v, a[i]));
        }
    }
    for(int i=0; i< sz2; i++){
        int v=b[i];
        cendist[v]=1e14;
        while(v!=cp[v]){
            v=cp[v];
            cendist[v]=1e14;
        }
    }
    return ans;
}

# Verdict Execution time Memory Grader output
1 Correct 17 ms 39256 KB Output is correct
2 Correct 498 ms 45872 KB Output is correct
3 Correct 951 ms 45892 KB Output is correct
4 Correct 902 ms 45892 KB Output is correct
5 Correct 441 ms 46184 KB Output is correct
6 Correct 161 ms 45792 KB Output is correct
7 Correct 934 ms 45908 KB Output is correct
8 Correct 938 ms 46052 KB Output is correct
9 Correct 448 ms 46044 KB Output is correct
10 Correct 162 ms 45912 KB Output is correct
11 Correct 906 ms 45904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 39260 KB Output is correct
2 Correct 1654 ms 109444 KB Output is correct
3 Correct 3487 ms 130904 KB Output is correct
4 Correct 565 ms 128432 KB Output is correct
5 Correct 2732 ms 159004 KB Output is correct
6 Correct 3500 ms 131672 KB Output is correct
7 Correct 2147 ms 77320 KB Output is correct
8 Correct 253 ms 77240 KB Output is correct
9 Correct 933 ms 80744 KB Output is correct
10 Correct 2196 ms 78000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 39256 KB Output is correct
2 Correct 498 ms 45872 KB Output is correct
3 Correct 951 ms 45892 KB Output is correct
4 Correct 902 ms 45892 KB Output is correct
5 Correct 441 ms 46184 KB Output is correct
6 Correct 161 ms 45792 KB Output is correct
7 Correct 934 ms 45908 KB Output is correct
8 Correct 938 ms 46052 KB Output is correct
9 Correct 448 ms 46044 KB Output is correct
10 Correct 162 ms 45912 KB Output is correct
11 Correct 906 ms 45904 KB Output is correct
12 Correct 8 ms 39260 KB Output is correct
13 Correct 1654 ms 109444 KB Output is correct
14 Correct 3487 ms 130904 KB Output is correct
15 Correct 565 ms 128432 KB Output is correct
16 Correct 2732 ms 159004 KB Output is correct
17 Correct 3500 ms 131672 KB Output is correct
18 Correct 2147 ms 77320 KB Output is correct
19 Correct 253 ms 77240 KB Output is correct
20 Correct 933 ms 80744 KB Output is correct
21 Correct 2196 ms 78000 KB Output is correct
22 Correct 2192 ms 132696 KB Output is correct
23 Correct 2413 ms 133892 KB Output is correct
24 Correct 6119 ms 135492 KB Output is correct
25 Correct 5770 ms 137884 KB Output is correct
26 Correct 5521 ms 136060 KB Output is correct
27 Correct 3235 ms 157684 KB Output is correct
28 Correct 615 ms 134932 KB Output is correct
29 Correct 5003 ms 135588 KB Output is correct
30 Correct 5571 ms 134952 KB Output is correct
31 Correct 5325 ms 135752 KB Output is correct
32 Correct 924 ms 81500 KB Output is correct
33 Correct 248 ms 77252 KB Output is correct
34 Correct 864 ms 75844 KB Output is correct
35 Correct 932 ms 75776 KB Output is correct
36 Correct 2012 ms 76416 KB Output is correct
37 Correct 2063 ms 76624 KB Output is correct