#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;
#define f(i,a,b) for(int i = a; i < b; i++)
#define fa(i,a,b) for(int i = a; i >= b; i--)
#define ff first
#define ss second
const int N = 5e5 + 5;
const ll inf = 1e17 + 100;
int n, p[N], sz[N], LVL[N];
ll h[N][20], s[N];
bool vis[N];
vector <ii> adj[N];
vector <int> ra;
int size(int u, int f){
sz[u] = 1;
for(auto p: adj[u]){
if(p.ff != f and !vis[p.ff]) sz[u] += size(p.ff, u);
}
return sz[u];
}
int get_centroid(int u, int f, int curr_size){
for(auto p: adj[u]) if(p.ff != f and !vis[p.ff] and sz[p.ff]*2 > curr_size)
return get_centroid(p.ff, u, curr_size);
return u;
}
void dfs(int u, int f, int lvl){
for(auto p: adj[u]){
ll v = p.ff, w = p.ss;
if(vis[v] or v == f) continue;
h[v][lvl] = h[u][lvl] + w;
dfs(v, u, lvl);
}
}
void descompose(int u, int f, int lvl){
int cen = get_centroid(u, f, size(u, f));
vis[cen] = 1;
LVL[cen] = ++lvl;
p[cen] = f, h[cen][lvl] = 0;
dfs(cen, f, lvl);
for(auto p: adj[cen]) if(!vis[p.ff]) descompose(p.ff, cen, lvl);
}
void Init(int ni, int a[], int b[], int d[]){
n = ni;
f(i,0,n-1){
adj[a[i]].push_back({b[i], d[i]});
adj[b[i]].push_back({a[i], d[i]});
}
descompose(0, -1, 0);
f(i,0,n) s[i] = inf;
}
void paint_red(int u){
int aux = u;
while(u != -1){
ra.push_back(u);
s[u] = min(s[u], h[aux][LVL[u]]);
u = p[u];
}
}
ll get(int u){
ll ans = inf, aux = u;
while(u != -1){
ans = min(ans, h[aux][LVL[u]] + s[u]);
u = p[u];
}
return ans;
}
ll Query(int S, int X[], int T, int Y[]){
ra.clear();
f(i,0,S) paint_red(X[i]);
ll ans = inf;
f(i,0,T) ans = min(ans, get(Y[i]));
for(int x: ra) s[x] = inf;
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
12500 KB |
Output is correct |
2 |
Correct |
254 ms |
21464 KB |
Output is correct |
3 |
Correct |
354 ms |
21520 KB |
Output is correct |
4 |
Correct |
351 ms |
21752 KB |
Output is correct |
5 |
Correct |
336 ms |
21880 KB |
Output is correct |
6 |
Correct |
184 ms |
21324 KB |
Output is correct |
7 |
Correct |
290 ms |
21488 KB |
Output is correct |
8 |
Correct |
325 ms |
21484 KB |
Output is correct |
9 |
Correct |
327 ms |
21952 KB |
Output is correct |
10 |
Correct |
185 ms |
21388 KB |
Output is correct |
11 |
Correct |
282 ms |
21408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12244 KB |
Output is correct |
2 |
Correct |
1546 ms |
139076 KB |
Output is correct |
3 |
Correct |
2368 ms |
141984 KB |
Output is correct |
4 |
Correct |
580 ms |
136700 KB |
Output is correct |
5 |
Correct |
3079 ms |
166544 KB |
Output is correct |
6 |
Correct |
2492 ms |
143368 KB |
Output is correct |
7 |
Correct |
741 ms |
46200 KB |
Output is correct |
8 |
Correct |
341 ms |
45896 KB |
Output is correct |
9 |
Correct |
935 ms |
50644 KB |
Output is correct |
10 |
Correct |
752 ms |
47452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
12500 KB |
Output is correct |
2 |
Correct |
254 ms |
21464 KB |
Output is correct |
3 |
Correct |
354 ms |
21520 KB |
Output is correct |
4 |
Correct |
351 ms |
21752 KB |
Output is correct |
5 |
Correct |
336 ms |
21880 KB |
Output is correct |
6 |
Correct |
184 ms |
21324 KB |
Output is correct |
7 |
Correct |
290 ms |
21488 KB |
Output is correct |
8 |
Correct |
325 ms |
21484 KB |
Output is correct |
9 |
Correct |
327 ms |
21952 KB |
Output is correct |
10 |
Correct |
185 ms |
21388 KB |
Output is correct |
11 |
Correct |
282 ms |
21408 KB |
Output is correct |
12 |
Correct |
7 ms |
12244 KB |
Output is correct |
13 |
Correct |
1546 ms |
139076 KB |
Output is correct |
14 |
Correct |
2368 ms |
141984 KB |
Output is correct |
15 |
Correct |
580 ms |
136700 KB |
Output is correct |
16 |
Correct |
3079 ms |
166544 KB |
Output is correct |
17 |
Correct |
2492 ms |
143368 KB |
Output is correct |
18 |
Correct |
741 ms |
46200 KB |
Output is correct |
19 |
Correct |
341 ms |
45896 KB |
Output is correct |
20 |
Correct |
935 ms |
50644 KB |
Output is correct |
21 |
Correct |
752 ms |
47452 KB |
Output is correct |
22 |
Correct |
1837 ms |
142708 KB |
Output is correct |
23 |
Correct |
1857 ms |
146748 KB |
Output is correct |
24 |
Correct |
2983 ms |
148204 KB |
Output is correct |
25 |
Correct |
2936 ms |
150556 KB |
Output is correct |
26 |
Correct |
3030 ms |
144412 KB |
Output is correct |
27 |
Correct |
3665 ms |
168516 KB |
Output is correct |
28 |
Correct |
808 ms |
141704 KB |
Output is correct |
29 |
Correct |
3067 ms |
143916 KB |
Output is correct |
30 |
Correct |
2848 ms |
143512 KB |
Output is correct |
31 |
Correct |
2872 ms |
144096 KB |
Output is correct |
32 |
Correct |
885 ms |
55640 KB |
Output is correct |
33 |
Correct |
331 ms |
48952 KB |
Output is correct |
34 |
Correct |
561 ms |
45676 KB |
Output is correct |
35 |
Correct |
605 ms |
46284 KB |
Output is correct |
36 |
Correct |
792 ms |
46528 KB |
Output is correct |
37 |
Correct |
765 ms |
46540 KB |
Output is correct |