#include"factories.h"
#include<bits/stdc++.h>
#ifndef EVAL
#include"grader.cpp"
#endif
template<class T>void umin(T &a,T b){if(b<a)a=b;}
using namespace std;
typedef long long ll;
vector<pair<int,int>>g[500001];
int sz[500001];
bool black[500001];
void build_sz(int v,int p){
sz[v]=1;
for(auto &[to,wegiht]:g[v])if(to!=p&&!black[to]){
build_sz(to,v);
sz[v]+=sz[to];
}
}
int get_centroid(int v,int p){
int siz=sz[v]>>1;
while(true){
bool can=true;
for(auto &[to,weight]:g[v]){
if(black[to]||to==p)continue;
if(sz[to]>siz){
can=false;
p=v;
v=to;
break;
}
}
if(can)return v;
}
}
ll dist[20][500001];
void build_dist(int v,int p,int lv){
for(auto &[to,weight]:g[v])if(to!=p&&!black[to]){
dist[lv][to]=dist[lv][v]+weight;
build_dist(to,v,lv);
}
}
int parent[500001];
int level[500001];
ll mn[500001];
int n;
void decompose(int centroid,int parent_centroid){
build_sz(centroid,0);
centroid=get_centroid(centroid,0);
parent[centroid]=parent_centroid;
level[centroid]=level[parent_centroid]+1;
build_dist(centroid,0,level[centroid]);
black[centroid]=true;
for(auto &[to,weight]:g[centroid])if(!black[to])decompose(to,centroid);
}
void Init(int n,int a[],int b[],int c[]){
::n=n;
for(int i=0;i<n-1;i++){
int &x=a[i];
int &y=b[i];
int &z=c[i];
g[x+1].emplace_back(y+1,z);
g[y+1].emplace_back(x+1,z);
}
level[0]=-1;
decompose(1,0);
for(int i=1;i<=n;i++)mn[i]=1e15;
}
ll Query(int s,int x[],int t,int y[]){
for(int i=0;i<s;i++){
int ver=x[i]+1;
while(ver){
umin(mn[ver],dist[level[ver]][x[i]+1]);
ver=parent[ver];
}
}
ll ans=1e15;
for(int i=0;i<t;i++){
int ver=y[i]+1;
while(ver){
umin(ans,mn[ver]+dist[level[ver]][y[i]+1]);
ver=parent[ver];
}
}
for(int i=0;i<s;i++){
int ver=x[i]+1;
while(ver){
mn[ver]=1e15;
ver=parent[ver];
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
12472 KB |
Output is correct |
2 |
Correct |
386 ms |
20892 KB |
Output is correct |
3 |
Correct |
405 ms |
20904 KB |
Output is correct |
4 |
Correct |
388 ms |
20984 KB |
Output is correct |
5 |
Correct |
404 ms |
21192 KB |
Output is correct |
6 |
Correct |
232 ms |
20576 KB |
Output is correct |
7 |
Correct |
382 ms |
20868 KB |
Output is correct |
8 |
Correct |
381 ms |
20936 KB |
Output is correct |
9 |
Correct |
427 ms |
21152 KB |
Output is correct |
10 |
Correct |
251 ms |
20408 KB |
Output is correct |
11 |
Correct |
468 ms |
20940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12236 KB |
Output is correct |
2 |
Correct |
2062 ms |
109068 KB |
Output is correct |
3 |
Correct |
3166 ms |
125844 KB |
Output is correct |
4 |
Correct |
678 ms |
58384 KB |
Output is correct |
5 |
Correct |
4179 ms |
148348 KB |
Output is correct |
6 |
Correct |
3305 ms |
127096 KB |
Output is correct |
7 |
Correct |
1093 ms |
41412 KB |
Output is correct |
8 |
Correct |
360 ms |
30388 KB |
Output is correct |
9 |
Correct |
1328 ms |
45548 KB |
Output is correct |
10 |
Correct |
1100 ms |
42552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
12472 KB |
Output is correct |
2 |
Correct |
386 ms |
20892 KB |
Output is correct |
3 |
Correct |
405 ms |
20904 KB |
Output is correct |
4 |
Correct |
388 ms |
20984 KB |
Output is correct |
5 |
Correct |
404 ms |
21192 KB |
Output is correct |
6 |
Correct |
232 ms |
20576 KB |
Output is correct |
7 |
Correct |
382 ms |
20868 KB |
Output is correct |
8 |
Correct |
381 ms |
20936 KB |
Output is correct |
9 |
Correct |
427 ms |
21152 KB |
Output is correct |
10 |
Correct |
251 ms |
20408 KB |
Output is correct |
11 |
Correct |
468 ms |
20940 KB |
Output is correct |
12 |
Correct |
8 ms |
12236 KB |
Output is correct |
13 |
Correct |
2062 ms |
109068 KB |
Output is correct |
14 |
Correct |
3166 ms |
125844 KB |
Output is correct |
15 |
Correct |
678 ms |
58384 KB |
Output is correct |
16 |
Correct |
4179 ms |
148348 KB |
Output is correct |
17 |
Correct |
3305 ms |
127096 KB |
Output is correct |
18 |
Correct |
1093 ms |
41412 KB |
Output is correct |
19 |
Correct |
360 ms |
30388 KB |
Output is correct |
20 |
Correct |
1328 ms |
45548 KB |
Output is correct |
21 |
Correct |
1100 ms |
42552 KB |
Output is correct |
22 |
Correct |
2571 ms |
109212 KB |
Output is correct |
23 |
Correct |
2677 ms |
138296 KB |
Output is correct |
24 |
Correct |
4173 ms |
152008 KB |
Output is correct |
25 |
Correct |
4464 ms |
155804 KB |
Output is correct |
26 |
Correct |
4160 ms |
152080 KB |
Output is correct |
27 |
Correct |
5276 ms |
171136 KB |
Output is correct |
28 |
Correct |
882 ms |
87212 KB |
Output is correct |
29 |
Correct |
4130 ms |
151744 KB |
Output is correct |
30 |
Correct |
4206 ms |
151072 KB |
Output is correct |
31 |
Correct |
4413 ms |
151088 KB |
Output is correct |
32 |
Correct |
1321 ms |
60116 KB |
Output is correct |
33 |
Correct |
371 ms |
44640 KB |
Output is correct |
34 |
Correct |
849 ms |
50552 KB |
Output is correct |
35 |
Correct |
828 ms |
50480 KB |
Output is correct |
36 |
Correct |
1105 ms |
53460 KB |
Output is correct |
37 |
Correct |
1115 ms |
53572 KB |
Output is correct |