#include <bits/stdc++.h>
/// #pragma GCC optimize ("Ofast")
/// #pragma GCC target ("avx2")
/// #pragma GCC optimize("unroll-loops")
using namespace std;
using ll = long long;
using ii = pair<ll, ll>;
using vi = vector<int>;
#define ff first
#define ss second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lb lower_bound
const int MOD = 1000 * 1000 * 1000 + 7;
vector<pair<int, ll>> adj[500001];
int par[500001], level[500001], sz[500001];
ll dist[500001][22], ans[500001];
bool rem[500001];
/// CENTROID DECOMPOSITION :
int dfs_size(int v, int p) {
sz[v] = 1;
for(auto u: adj[v]) {
if(u.ff == p || rem[u.ff]) continue;
sz[v] += dfs_size(u.ff, v);
}
return sz[v];
}
int centroid(int v, int p, int N) {
for(auto u : adj[v]) {
if(u.ff == p || rem[u.ff]) continue;
if(sz[u.ff] >= N / 2)
return centroid(u.ff, v, N);
}
return v;
}
void dfs(int v, int p, int L) {
for(auto u : adj[v]) {
if(u.ff == p || rem[u.ff]) continue;
dist[u.ff][L] = dist[v][L] + u.ss;
dfs(u.ff, v, L);
}
}
void build(int v, int p) {
int N = dfs_size(v, p);
int C = centroid(v, p, N);
level[C] = (p != -1 ? level[p] + 1 : 0);
par[C] = p; dfs(C, C, level[C]); rem[C] = 1;
for(auto u : adj[C]) {
if(rem[u.ff]) continue;
build(u.ff, C);
}
}
void Init(int N, int A[], int B[], int D[]) {
for(int l = 0; l < N - 1; l++) {
int U = A[l], V = B[l], W = D[l];
adj[U].pb({V, W}), adj[V].pb({U, W});
}
for(int l = 0; l < N; l++) ans[l] = 1e18 + 7;
build(0, -1);
}
void update(int U) {
int V = U;
for(; V != -1; V = par[V])
ans[V] = min(ans[V], dist[U][level[V]]);
}
ll query(int U) {
int V = U; ll res = 1e18 + 7;
for(; V != -1; V = par[V])
res = min(res, ans[V] + dist[U][level[V]]);
return res;
}
void reset(int U) {
for(; U != -1; U = par[U])
ans[U] = 1e18 + 7;
}
long long Query(int S, int X[], int T, int Y[]) {
ll res = 1e18 + 7;
for(int l = 0; l < S; l++) update(X[l]);
for(int l = 0; l < T; l++)
res = min(res, query(Y[l]));
for(int l = 0; l < S; l++) reset(X[l]);
return res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
12628 KB |
Output is correct |
2 |
Correct |
255 ms |
30776 KB |
Output is correct |
3 |
Correct |
282 ms |
30820 KB |
Output is correct |
4 |
Correct |
266 ms |
30768 KB |
Output is correct |
5 |
Correct |
320 ms |
31128 KB |
Output is correct |
6 |
Correct |
186 ms |
29448 KB |
Output is correct |
7 |
Correct |
317 ms |
29576 KB |
Output is correct |
8 |
Correct |
268 ms |
30872 KB |
Output is correct |
9 |
Correct |
293 ms |
31048 KB |
Output is correct |
10 |
Correct |
189 ms |
30796 KB |
Output is correct |
11 |
Correct |
273 ms |
30824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12244 KB |
Output is correct |
2 |
Correct |
1538 ms |
154116 KB |
Output is correct |
3 |
Correct |
2530 ms |
167772 KB |
Output is correct |
4 |
Correct |
546 ms |
162912 KB |
Output is correct |
5 |
Correct |
3229 ms |
193092 KB |
Output is correct |
6 |
Correct |
2479 ms |
169932 KB |
Output is correct |
7 |
Correct |
738 ms |
61500 KB |
Output is correct |
8 |
Correct |
304 ms |
61412 KB |
Output is correct |
9 |
Correct |
902 ms |
65612 KB |
Output is correct |
10 |
Correct |
809 ms |
63036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
12628 KB |
Output is correct |
2 |
Correct |
255 ms |
30776 KB |
Output is correct |
3 |
Correct |
282 ms |
30820 KB |
Output is correct |
4 |
Correct |
266 ms |
30768 KB |
Output is correct |
5 |
Correct |
320 ms |
31128 KB |
Output is correct |
6 |
Correct |
186 ms |
29448 KB |
Output is correct |
7 |
Correct |
317 ms |
29576 KB |
Output is correct |
8 |
Correct |
268 ms |
30872 KB |
Output is correct |
9 |
Correct |
293 ms |
31048 KB |
Output is correct |
10 |
Correct |
189 ms |
30796 KB |
Output is correct |
11 |
Correct |
273 ms |
30824 KB |
Output is correct |
12 |
Correct |
7 ms |
12244 KB |
Output is correct |
13 |
Correct |
1538 ms |
154116 KB |
Output is correct |
14 |
Correct |
2530 ms |
167772 KB |
Output is correct |
15 |
Correct |
546 ms |
162912 KB |
Output is correct |
16 |
Correct |
3229 ms |
193092 KB |
Output is correct |
17 |
Correct |
2479 ms |
169932 KB |
Output is correct |
18 |
Correct |
738 ms |
61500 KB |
Output is correct |
19 |
Correct |
304 ms |
61412 KB |
Output is correct |
20 |
Correct |
902 ms |
65612 KB |
Output is correct |
21 |
Correct |
809 ms |
63036 KB |
Output is correct |
22 |
Correct |
1782 ms |
172648 KB |
Output is correct |
23 |
Correct |
1931 ms |
175404 KB |
Output is correct |
24 |
Correct |
3026 ms |
176096 KB |
Output is correct |
25 |
Correct |
3067 ms |
179784 KB |
Output is correct |
26 |
Correct |
3198 ms |
176096 KB |
Output is correct |
27 |
Correct |
3828 ms |
197184 KB |
Output is correct |
28 |
Correct |
703 ms |
160308 KB |
Output is correct |
29 |
Correct |
2815 ms |
175824 KB |
Output is correct |
30 |
Correct |
2813 ms |
175180 KB |
Output is correct |
31 |
Correct |
2883 ms |
175808 KB |
Output is correct |
32 |
Correct |
870 ms |
66492 KB |
Output is correct |
33 |
Correct |
319 ms |
61800 KB |
Output is correct |
34 |
Correct |
554 ms |
59140 KB |
Output is correct |
35 |
Correct |
635 ms |
58992 KB |
Output is correct |
36 |
Correct |
747 ms |
59944 KB |
Output is correct |
37 |
Correct |
726 ms |
59848 KB |
Output is correct |