#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define bit(n,i) ((n>>i)&1)
#define all(x) x.begin(),x.end()
#define int long long
typedef long long ll;
typedef pair<int,int> pii;
typedef double ld;
typedef pair<ld,ld> pdd;
typedef pair<ll,ll> pll;
const ll maxn=5e5+15;
const ll offset=2e5;
const ll inf=1e18;
const int base=350;
const ll mod=998244353;
vector<pii> ke[maxn],adj[maxn];
int dp[maxn],par[maxn][21],in[maxn],out[maxn],Time,dep[maxn];
void dfs(int u,int pre)
{
in[u]=++Time;
par[u][0]=pre;
for1(i,1,20) par[u][i]=par[par[u][i-1]][i-1];
for(auto x: ke[u])
{
int v=x.fi,w=x.se;
if (v==pre) continue;
dep[v]=dep[u]+w;
dfs(v,u);
}
out[u]=Time;
}
bool is_ancestor(int u,int v) {return in[u]<=in[v] && out[v]<=out[u];}
int lca(int u,int v)
{
if (is_ancestor(u,v)) return u;
if (is_ancestor(v,u)) return v;
for2(i,20,0) if (!is_ancestor(par[u][i],v)) u=par[u][i];
return par[u][0];
}
void Init(int N, int A[], int B[], int D[]) {
for1(i,0,N-2)
{
ke[A[i]].pb({B[i],D[i]});
ke[B[i]].pb({A[i],D[i]});
}
dfs(0,0);
for1(i,0,N-1) dp[i]=inf;
}
priority_queue<pii,vector<pii>,greater<pii>> pq;
long long Query(int S, int X[], int T, int Y[]) {
vector<int> L;
for1(i,0,S-1) L.pb(X[i]);
for1(i,0,T-1) L.pb(Y[i]);
sort(all(L),[](int u,int v)
{
return in[u]<in[v];
});
for1(i,1,S+T-1)
{
L.pb(lca(L[i],L[i-1]));
}
L.pb(0);
sort(all(L),[](int u,int v)
{
return in[u]<in[v];
});
L.resize(unique(all(L))-L.begin());
vector<int> Q;
for(int u: L)
{
while(!Q.empty() && !is_ancestor(Q.back(),u)) Q.pop_back();
if (!Q.empty())
{
adj[Q.back()].pb({u,dep[u]-dep[Q.back()]});
adj[u].pb({Q.back(),dep[u]-dep[Q.back()]});
}
Q.pb(u);
}
// return 0;
for1(i,0,S-1)
{
dp[X[i]]=0;
pq.push({0LL,X[i]});
}
while (!pq.empty())
{
int _=pq.top().fi, u=pq.top().se;
pq.pop();
if (_!=dp[u]) continue;
for(pii x:adj[u])
{
int v=x.fi,w=x.se;
if (dp[u]+w<dp[v])
{
dp[v]=dp[u]+w;
pq.push({dp[v],v});
}
}
}
int res=inf;
for1(i,0,T-1)
{
res=min(res,dp[Y[i]]);
}
for(int u:L)
{
adj[u].clear();
dp[u]=inf;
}
return res;
}
/*
#define MAX_N 500000
#define MAX_Q 100000
#define MAX_SUM_ST 1000000
#define MAX_VALUE 1000000000
static int N, Q;
static int A[MAX_N], B[MAX_N], D[MAX_N];
static int S[MAX_N];
static int T[MAX_N];
static int X[MAX_SUM_ST];
static int Y[MAX_SUM_ST];
static int Qx[MAX_N];
static int Qy[MAX_N];
int32_t main() {
// freopen("sample-in-01.txt","r",stdin);
int i, j, k;
int STop, TTop;
if (2 != scanf("%d%d", &N, &Q)) {
fprintf(stderr, "error: cannot read N and Q.\n");
exit(1);
}
if (!(2 <= N && N <= MAX_N)) {
fprintf(stderr, "error: N is out of bounds.\n");
exit(1);
}
if (!(1 <= Q && Q <= MAX_Q)) {
fprintf(stderr, "error: Q is out of bounds.\n");
exit(1);
}
for (i = 0; i < N - 1; ++i) {
if (1 != scanf("%d", &A[i])) {
fprintf(stderr, "error: cannot read A[%d].\n", i);
exit(1);
}
if (!(0 <= A[i] && A[i] <= N - 1)) {
fprintf(stderr, "error: A[%d] is out of bounds.\n", i);
exit(1);
}
if (1 != scanf("%d", &B[i])) {
fprintf(stderr, "error: cannot read B[%d].\n", i);
exit(1);
}
if (!(0 <= B[i] && B[i] <= N - 1)) {
fprintf(stderr, "error: B[%d] is out of bounds.\n", i);
exit(1);
}
if (A[i] == B[i]) {
fprintf(stderr, "error: B[%d] is equal to A[%d].\n", i, i);
exit(1);
}
if (1 != scanf("%d", &D[i])) {
fprintf(stderr, "error: cannot read D[%d].\n", i);
exit(1);
}
if (!(1 <= D[i] && D[i] <= MAX_VALUE)) {
fprintf(stderr, "error: D[%d] is out of bounds.\n", i);
exit(1);
}
}
// return 0;
STop = 0;
TTop = 0;
for (j = 0; j < Q; ++j) {
if (2 != scanf("%d%d", &S[j], &T[j])) {
fprintf(stderr, "error: cannot read L[%d] and R[%d].\n", j, j);
exit(1);
}
if(STop + S[j] > MAX_SUM_ST) {
fprintf(stderr, "error: S[0] + S[1] + ... + S[%d] is out of bounds.\n", j);
exit(1);
}
if(TTop + T[j] > MAX_SUM_ST) {
fprintf(stderr, "error: T[0] + T[1] + ... + T[%d] is out of bounds.\n", j);
exit(1);
}
for (k = 0; k < S[j]; ++k, ++STop) {
if (1 != scanf("%d", &X[STop])) {
fprintf(stderr, "error: cannot read X[%d][%d].\n", j, k);
exit(1);
}
if (!(0 <= X[STop] && X[STop] <= N - 1)) {
fprintf(stderr, "error: cannot read X[%d][%d].\n", j, k);
exit(1);
}
}
for (k = 0; k < T[j]; ++k, ++TTop) {
if (1 != scanf("%d", &Y[TTop])) {
fprintf(stderr, "error: cannot read Y[%d][%d].\n", j, k);
exit(1);
}
if (!(0 <= Y[TTop] && Y[TTop] <= N - 1)) {
fprintf(stderr, "error: cannot read Y[%d][%d].\n", j, k);
exit(1);
}
}
}
STop = 0;
TTop = 0;
Init(N, A, B, D);
// return 0;
for (j = 0; j < Q; ++j) {
for (k = 0; k < S[j]; k++) {
Qx[k] = X[STop++];
}
for (k = 0; k < T[j]; k++) {
Qy[k] = Y[TTop++];
}
printf("%lld\n", Query(S[j], Qx, T[j], Qy));
}
return 0;
}
*/
Compilation message
/usr/bin/ld: /tmp/ccBq6c3V.o: in function `main':
grader.cpp:(.text.startup+0x37d): undefined reference to `Init(int, int*, int*, int*)'
/usr/bin/ld: grader.cpp:(.text.startup+0x412): undefined reference to `Query(int, int*, int, int*)'
collect2: error: ld returned 1 exit status