This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
using ll = long long;
const int mxN = (int)5e5+10;
const int mxLg = (int)21;
const ll LINF = (ll)4e18;
int n, sub[mxN], par[mxN], vis[mxN];
ll D[mxN], depth[mxN];
int jmp[mxLg][mxN];
vector<pair<int,int>> adj[mxN];
set<int> S[mxN];
int fcentroid(int s, int p, int n){
    for(auto x : adj[s]){
        int u = x.fi;
        if(u!=p and sub[u]>n/2 and !vis[u])
            return fcentroid(u,s,n);
    }
    return s;
}
int dfs(int s, int p, bool general){
    sub[s]=1;
    if(general){
        jmp[0][s]=p;
        if(p!=-1) depth[s] = depth[p]+1;
    }
    for(auto x : adj[s]){
        int u = x.fi, w = x.se;
        if(general) D[u] = D[s]+w;
        if(u!=p and !vis[u])
            sub[s]+=dfs(u,s,general);
    }
    return sub[s];
}
void centroid_decomp(int s, int p){
    int n = dfs(s,p,0);
    int centroid = fcentroid(s,p,n);
    vis[centroid] = 1; par[s] = p;
    for(auto x : adj[centroid]){
        int u = x.fi;
        if(u!=p and !vis[u])
            centroid_decomp(u,centroid);
    }
}
void Init(int N, int a[], int b[], int d[]) {
    n = N; memset(jmp,-1,sizeof(jmp));
    for(int i = 0; i < n-1; i++){
        adj[a[i]].pb({b[i],d[i]});
        adj[b[i]].pb({a[i],d[i]});
    }
    dfs(0,-1,1); centroid_decomp(0,-1);
    for(int i = 1; i < mxLg; i++)
        for(int j = 0; j < n; j++)
            if(jmp[i-1][j]!=-1)
                jmp[i][j] = jmp[i-1][jmp[i-1][j]];
}
int getPath(int x, int k){
    for(int i = 0; i < mxLg; i++)
        if(k&(1<<i) and x!=-1) x=jmp[i][x];
    return x;
}
int lca(int a, int b){
    if(a==b) return a;
    if(jmp[0][a]==jmp[0][b]) return jmp[0][a];
    if(depth[a]>depth[b]) swap(a,b);
    b = getPath(b,depth[b]-depth[a]);
    for(int i = mxLg-1; i >= 0; i--)
        if(jmp[i][a]!=-1 and jmp[i][b]!=-1 and jmp[i][a]!=jmp[i][b])
            a=jmp[i][a], b = jmp[i][n];
    return lca(a,b);
}
int dis(int a, int b){
    return D[a]+D[b]-2*D[lca(a,b)];
}
ll Query(int s, int x[], int t, int y[]) {
    vector<int> rem; rem.clear();
    for(int i = 0; i < s; i++){
        S[x[i]].insert(0);
        int a = x[i];
        while(a!=-1){
            S[a].insert(dis(x[i],a)); rem.pb(a);
            a = par[a];
        }
    }
    int ans = LINF;
    for(int i = 0; i < t; i++){
        int x = y[i];
        while(x!=-1){
            if(!S[x].empty())
                ans = min(ans, dis(x,y[i])+*S[x].begin());
            //from x to nearest active node + dis from y[i] to x)
            x=par[x];
        }
    }
    for(auto u : rem) S[u].clear();
}
Compilation message (stderr)
factories.cpp: In function 'll Query(int, int*, int, int*)':
factories.cpp:96:15: warning: overflow in conversion from 'll' {aka 'long long int'} to 'int' changes value from '4000000000000000000' to '-1651507200' [-Woverflow]
   96 |     int ans = LINF;
      |               ^~~~
factories.cpp:107:1: warning: no return statement in function returning non-void [-Wreturn-type]
  107 | }
      | ^| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |