#include "factories.h"
#include<bits/stdc++.h>
using namespace std;
struct edg{
int v;
long long w;
};
vector<edg>g[500009];
long long dis[500009];
int dep[500009];
int st[500009][21];
int sub[500009];
int use[500009];
void dfs(int u,int par){
for (auto v:g[u]){
if (v.v==par)continue;
dis[v.v]=dis[u]+v.w;
dep[v.v]=dep[u]+1;
st[v.v][0]=u;
dfs(v.v,u);
}
}
int lca(int u,int v){
if (dep[u]<dep[v])swap(u,v);
int k=dep[u]-dep[v];
for (int i=20;i>=0;i--){
if (k>>i&1){
u=st[u][i];
}
}
if (u==v)return u;
for (int i=20;i>=0;i--){
if (st[u][i]==0||st[v][i]==0)continue;
if (st[u][i]!=st[v][i]){
u=st[u][i];
v=st[v][i];
}
}
return st[u][0];
}
long long cal(int u,int v){
return dis[u]+dis[v]-2*dis[lca(u,v)];
}
int dfs2(int u,int par){
sub[u]=1;
for (auto v:g[u]){
int lm=v.v;
if (use[lm]||lm==par)continue;
dfs2(lm,u);
sub[u]+=sub[lm];
}
return sub[u];
}
int centroid(int u,int p,int sz){
for (auto v:g[u]){
if (use[v.v]||v.v==p)continue;
if (sub[v.v]*2>sz)return centroid(v.v,u,sz);
}
return u;
}
int par[500009];
long long mxx[500009];
int decompose(int u,int p){
int sz=dfs2(u,0);
int cen=centroid(u,0,sz);
use[cen]=1;
if (p!=0){
par[cen]=p;
}
for (auto v:g[cen]){
if (use[v.v])continue;
decompose(v.v,cen);
}
return cen;
}
void Init(int N,int A[],int B[],int D[]){
for (int i=0;i<N-1;i++){
g[A[i]+1].push_back({B[i]+1,D[i]});
g[B[i]+1].push_back({A[i]+1,D[i]});
}
dfs(1,0);
for (int i=1;i<=20;i++){
for (int j=1;j<=N;j++){
st[j][i]=st[st[j][i-1]][i-1];
}
}
for (int i=1;i<=N;i++){
mxx[i]=1e17;
}
//decompose(1,0);
}
/*void update(int u,int pos){
if (pos==0)return;
mxx[pos]=min(mxx[pos],cal(u,pos));
update(u,par[pos]);
}
void res(int u,int pos){
if (pos==0)return;
mxx[pos]=1e17;
res(u,par[pos]);
}
long long get(int u,int pos){
long long ans=1e17;
if (pos==0)return ans;
ans=min(ans,cal(u,pos)+mxx[pos]);
ans=min(ans,get(u,par[pos]));
return ans;
}*/
long long Query(int S,int X[],int T,int Y[]){
long long ans=1e17;
for (int i=0;i<S;i++){
for (int j=0;j<T;j++){
ans=min(ans,cal(X[i]+1,Y[j]+1));
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
100 ms |
12432 KB |
Output is correct |
2 |
Execution timed out |
8055 ms |
20776 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12276 KB |
Output is correct |
2 |
Correct |
1267 ms |
101392 KB |
Output is correct |
3 |
Correct |
2600 ms |
105464 KB |
Output is correct |
4 |
Correct |
820 ms |
99132 KB |
Output is correct |
5 |
Correct |
2240 ms |
135712 KB |
Output is correct |
6 |
Correct |
2068 ms |
106452 KB |
Output is correct |
7 |
Correct |
3684 ms |
38876 KB |
Output is correct |
8 |
Correct |
1006 ms |
38556 KB |
Output is correct |
9 |
Correct |
3105 ms |
43612 KB |
Output is correct |
10 |
Correct |
2299 ms |
40024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
100 ms |
12432 KB |
Output is correct |
2 |
Execution timed out |
8055 ms |
20776 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |