#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
#define ar array
#define ll long long
const int MXN = 5e5+9, LGN = 20;
const ll INF = 1e17;
ll n, sz[MXN], lvl[MXN], par[MXN], dist[LGN][MXN];
bool dead[MXN];
vector<ar<ll,2>> g[MXN];
vector<ll> d(MXN, INF);
void get_sz(int u, int pu) {
sz[u] = 1;
for (auto v : g[u]) if (v[1] != pu && !dead[v[1]]) {
get_sz(v[1], u);
sz[u] += sz[v[1]];
}
}
int centroid(int u, int pu, int rt) {
for (auto v : g[u]) if (v[1] != pu && !dead[v[1]]) {
if (sz[v[1]] > sz[rt]/2) return centroid(v[1], u, rt);
}
return u;
}
int OneCentroid (int rt) { get_sz(rt, rt); return centroid(rt, rt, rt); }
void get_dist(int u, int pu, int l) {
for (auto v : g[u]) if (v[1] != pu && !dead[v[1]]) {
dist[l][v[1]] = dist[l][u]+v[0];
get_dist(v[1], u, l);
}
}
void decompose(int start, int pcent, int l) {
int cent = OneCentroid(start);
lvl[cent] = l;
par[cent] = pcent;
dist[l][cent] = 0;
get_dist(cent, cent, l);
dead[cent] = 1;
for (auto v : g[cent]) if (!dead[v[1]]) decompose(v[1], cent, l+1);
dead[cent] = 0;
}
void Init(int N, int A[], int B[], int D[]) {
n = N;
for (int i = 0; i < N-1; i++) {
g[A[i]].push_back({D[i], B[i]});
g[B[i]].push_back({D[i], A[i]});
}
decompose(0, -1, 0);
}
void add(int x, bool remove) {
for (int u = x; u >= 0; u = par[u]) {
if (remove) d[u] = INF;
else d[u] = min(d[u], dist[lvl[u]][x]);
}
}
ll qry(int x) {
ll ans = INF;
for (int u = x; u >= 0; u = par[u]) {
ans = min(ans, d[u] + dist[lvl[u]][x]);
}
return ans;
}
long long Query(int S, int X[], int T, int Y[]) {
for (int i = 0; i < S; i++) add(X[i], 0);
ll ans = INF;
for (int i = 0; i < T; i++) ans = min(ans, qry(Y[i]));
for (int i = 0; i < S; i++) add(X[i], 1);
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
16364 KB |
Output is correct |
2 |
Correct |
392 ms |
34544 KB |
Output is correct |
3 |
Correct |
458 ms |
34552 KB |
Output is correct |
4 |
Correct |
441 ms |
34668 KB |
Output is correct |
5 |
Correct |
475 ms |
34892 KB |
Output is correct |
6 |
Correct |
281 ms |
34028 KB |
Output is correct |
7 |
Correct |
442 ms |
34540 KB |
Output is correct |
8 |
Correct |
454 ms |
34560 KB |
Output is correct |
9 |
Correct |
477 ms |
34924 KB |
Output is correct |
10 |
Correct |
284 ms |
34156 KB |
Output is correct |
11 |
Correct |
441 ms |
34540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
16236 KB |
Output is correct |
2 |
Correct |
2610 ms |
126764 KB |
Output is correct |
3 |
Correct |
3957 ms |
145628 KB |
Output is correct |
4 |
Correct |
854 ms |
90824 KB |
Output is correct |
5 |
Correct |
5158 ms |
187564 KB |
Output is correct |
6 |
Correct |
4059 ms |
164460 KB |
Output is correct |
7 |
Correct |
1459 ms |
62208 KB |
Output is correct |
8 |
Correct |
425 ms |
50188 KB |
Output is correct |
9 |
Correct |
1633 ms |
67172 KB |
Output is correct |
10 |
Correct |
1370 ms |
63468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
16364 KB |
Output is correct |
2 |
Correct |
392 ms |
34544 KB |
Output is correct |
3 |
Correct |
458 ms |
34552 KB |
Output is correct |
4 |
Correct |
441 ms |
34668 KB |
Output is correct |
5 |
Correct |
475 ms |
34892 KB |
Output is correct |
6 |
Correct |
281 ms |
34028 KB |
Output is correct |
7 |
Correct |
442 ms |
34540 KB |
Output is correct |
8 |
Correct |
454 ms |
34560 KB |
Output is correct |
9 |
Correct |
477 ms |
34924 KB |
Output is correct |
10 |
Correct |
284 ms |
34156 KB |
Output is correct |
11 |
Correct |
441 ms |
34540 KB |
Output is correct |
12 |
Correct |
12 ms |
16236 KB |
Output is correct |
13 |
Correct |
2610 ms |
126764 KB |
Output is correct |
14 |
Correct |
3957 ms |
145628 KB |
Output is correct |
15 |
Correct |
854 ms |
90824 KB |
Output is correct |
16 |
Correct |
5158 ms |
187564 KB |
Output is correct |
17 |
Correct |
4059 ms |
164460 KB |
Output is correct |
18 |
Correct |
1459 ms |
62208 KB |
Output is correct |
19 |
Correct |
425 ms |
50188 KB |
Output is correct |
20 |
Correct |
1633 ms |
67172 KB |
Output is correct |
21 |
Correct |
1370 ms |
63468 KB |
Output is correct |
22 |
Correct |
3383 ms |
150428 KB |
Output is correct |
23 |
Correct |
3401 ms |
155260 KB |
Output is correct |
24 |
Correct |
5264 ms |
170368 KB |
Output is correct |
25 |
Correct |
5309 ms |
174112 KB |
Output is correct |
26 |
Correct |
5260 ms |
170692 KB |
Output is correct |
27 |
Correct |
6464 ms |
191508 KB |
Output is correct |
28 |
Correct |
1104 ms |
101016 KB |
Output is correct |
29 |
Correct |
5183 ms |
170260 KB |
Output is correct |
30 |
Correct |
5195 ms |
169768 KB |
Output is correct |
31 |
Correct |
5156 ms |
170224 KB |
Output is correct |
32 |
Correct |
1721 ms |
67436 KB |
Output is correct |
33 |
Correct |
437 ms |
50780 KB |
Output is correct |
34 |
Correct |
1076 ms |
56940 KB |
Output is correct |
35 |
Correct |
1027 ms |
57196 KB |
Output is correct |
36 |
Correct |
1431 ms |
60268 KB |
Output is correct |
37 |
Correct |
1509 ms |
60432 KB |
Output is correct |