#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define pb push_back
#define el "\n"
#define f first
#define s second
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
typedef long long ll;
const ll MM=5e5+5, MOD=1e9+7, LOG=19;
int dep[MM], sz[MM], nxt[MM], in[MM], ot[MM], ct; ll dst[MM], spc[MM];
vector<pair<int,int>> adj[MM];
pair<int,int> st[LOG][MM];
bool blk[MM];
void df1(int u, int pa) {
dep[u]=dep[pa]+1; st[0][++ct]={dep[u], u}; in[u]=ct;
for(pair<int,int> &e:adj[u]) {
int v=e.f, w=e.s; if(v==pa) continue;
dst[v]=dst[u]+w; df1(v, u); sz[u]+=sz[v];
st[0][++ct]={dep[u], u}; } ot[u]=ct; }
inline int qry(int l, int r) {
int lg=log2(r-l+1);
return min(st[lg][l], st[lg][r-(1<<lg)+1]).s; }
inline int lca(int u, int v) {
if(in[u]>in[v]) swap(u, v);
return qry(in[u], in[v]); }
void gsz(int u, int pa) {
sz[u]=1;
for(pair<int,int> &e:adj[u]) {
int v=e.f; if(blk[v]||v==pa) continue;
gsz(v, u); sz[u]+=sz[v]; } }
int ctr(int u, int pa, int nd) {
for(pair<int,int> &e:adj[u]) {
int v=e.f; if(blk[v]||v==pa) continue;
if(sz[v]*2>nd) return ctr(v, u, nd); }
return u; }
inline ll dist(int u, int v) {
return dst[u]+dst[v]-2LL*dst[lca(u, v)]; }
void slv(int u, int pa) {
gsz(u, -1); u=ctr(u, -1, sz[u]); blk[u]=1; nxt[u]=pa;
for(pair<int,int> &e:adj[u]) {
int v=e.f; if(!blk[v]) slv(v, u); } }
void Init(int N, int A[], int B[], int D[]) {
for(int i=0; i<N; ++i) spc[i]=1e18;
for(int i=1; i<N; ++i) {
adj[A[i]].emplace_back(B[i], D[i]); adj[B[i]].emplace_back(A[i], D[i]); }
df1(0, 0); slv(0, -1);
for(int i=1; i<LOG; ++i)
for(int j=1; j+(1<<i)-1<=ct; ++j)
st[i][j]=min(st[i-1][j], st[i-1][j+(1<<(i-1))]); }
long long Query(int S, int X[], int T, int Y[]) {
for(int i=0, c; i<S; ++i) {
c=X[i];
while(c!=-1)
spc[c]=min(spc[c], dist(X[i], c)), c=nxt[c], assert(c!=nxt[c]); }
long long ans=1e18;
for(int i=0, c; i<T; ++i) {
c=Y[i];
while(c!=-1)
ans=min(ans, dist(Y[i], c)+spc[c]), c=nxt[c], assert(c!=nxt[c]); }
for(int i=0, c; i<S; ++i) {
c=X[i];
while(c!=-1)
spc[c]=1e18, c=nxt[c], assert(c!=nxt[c]); }
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
27 ms |
12540 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
12356 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
27 ms |
12540 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |