#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
#define long long long
#define pii pair<int, int>
#define pll pair<long, long>
#define x first
#define y second
const int N = 5e5+5;
int dep[N], par[N], d[N];
long dis[N][20];
bool check[N];
vector<pii> g[N];
pll ans[N];
int find(int u, int p, int psz, pii &ret) {
d[u] = 1;
int mxsz = 0;
for(auto v : g[u]) if(v.x != p and !check[v.x]) {
int now = find(v.x, u, psz, ret);
mxsz = max(mxsz, now);
d[u] += now;
}
mxsz = max(mxsz, psz - d[u]);
if(mxsz < ret.x) ret = pii(mxsz, u);
return d[u];
}
void filldis(int u, int p, int ndep) {
for(auto v : g[u]) if(v.x != p and !check[v.x]) dis[v.x][ndep] = dis[u][ndep] + v.y, filldis(v.x, u, ndep);
}
void centroid(int u, int p, int sz) {
pii ret(sz, -1);
find(u, u, sz, ret);
u = ret.y, dep[u] = dep[p] + 1, check[u] = true, par[u] = p;
filldis(u, u, dep[u]);
for(auto v : g[u]) if(!check[v.x]) centroid(v.x, u, d[v.x] > d[u] ? sz - d[u] : d[v.x]);
}
void Init(int N, int A[], int B[], int D[]) {
for(int i = 0; i < N-1; ++i) A[i]++, B[i]++;
for(int i = 0; i < N-1; ++i) g[A[i]].emplace_back(B[i], D[i]), g[B[i]].emplace_back(A[i], D[i]);
centroid(1, 0, N);
}
int step = 0;
long Query(int S, int X[], int T, int Y[]) {
step++;
for(int i = 0; i < S; ++i) {
int now = X[i]+1;
for(int j = now; j; j = par[j]) {
if(ans[j].y != step) ans[j] = pll(1e18, step);
ans[j].x = min(ans[j].x, dis[now][dep[j]]);
}
}
long mn = 1e18;
for(int i = 0; i < T; ++i) {
int now = Y[i]+1;
for(int j = now; j; j = par[j]) {
if(ans[j].y != step) continue;
mn = min(mn, ans[j].x + dis[now][dep[j]]);
}
}
return mn;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
12664 KB |
Output is correct |
2 |
Correct |
409 ms |
30968 KB |
Output is correct |
3 |
Correct |
573 ms |
40692 KB |
Output is correct |
4 |
Correct |
410 ms |
50052 KB |
Output is correct |
5 |
Correct |
435 ms |
59768 KB |
Output is correct |
6 |
Correct |
300 ms |
60640 KB |
Output is correct |
7 |
Correct |
472 ms |
60692 KB |
Output is correct |
8 |
Correct |
401 ms |
60820 KB |
Output is correct |
9 |
Correct |
414 ms |
60924 KB |
Output is correct |
10 |
Correct |
310 ms |
60924 KB |
Output is correct |
11 |
Correct |
383 ms |
60924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
60924 KB |
Output is correct |
2 |
Correct |
2408 ms |
175608 KB |
Output is correct |
3 |
Correct |
3861 ms |
178128 KB |
Output is correct |
4 |
Correct |
1174 ms |
178128 KB |
Output is correct |
5 |
Correct |
4664 ms |
206500 KB |
Output is correct |
6 |
Correct |
3927 ms |
206500 KB |
Output is correct |
7 |
Correct |
1168 ms |
206500 KB |
Output is correct |
8 |
Correct |
498 ms |
206500 KB |
Output is correct |
9 |
Correct |
1324 ms |
206500 KB |
Output is correct |
10 |
Correct |
1177 ms |
206500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
12664 KB |
Output is correct |
2 |
Correct |
409 ms |
30968 KB |
Output is correct |
3 |
Correct |
573 ms |
40692 KB |
Output is correct |
4 |
Correct |
410 ms |
50052 KB |
Output is correct |
5 |
Correct |
435 ms |
59768 KB |
Output is correct |
6 |
Correct |
300 ms |
60640 KB |
Output is correct |
7 |
Correct |
472 ms |
60692 KB |
Output is correct |
8 |
Correct |
401 ms |
60820 KB |
Output is correct |
9 |
Correct |
414 ms |
60924 KB |
Output is correct |
10 |
Correct |
310 ms |
60924 KB |
Output is correct |
11 |
Correct |
383 ms |
60924 KB |
Output is correct |
12 |
Correct |
15 ms |
60924 KB |
Output is correct |
13 |
Correct |
2408 ms |
175608 KB |
Output is correct |
14 |
Correct |
3861 ms |
178128 KB |
Output is correct |
15 |
Correct |
1174 ms |
178128 KB |
Output is correct |
16 |
Correct |
4664 ms |
206500 KB |
Output is correct |
17 |
Correct |
3927 ms |
206500 KB |
Output is correct |
18 |
Correct |
1168 ms |
206500 KB |
Output is correct |
19 |
Correct |
498 ms |
206500 KB |
Output is correct |
20 |
Correct |
1324 ms |
206500 KB |
Output is correct |
21 |
Correct |
1177 ms |
206500 KB |
Output is correct |
22 |
Correct |
2762 ms |
206500 KB |
Output is correct |
23 |
Correct |
2840 ms |
206500 KB |
Output is correct |
24 |
Correct |
4306 ms |
206500 KB |
Output is correct |
25 |
Correct |
4349 ms |
206500 KB |
Output is correct |
26 |
Correct |
4546 ms |
206500 KB |
Output is correct |
27 |
Correct |
5225 ms |
206500 KB |
Output is correct |
28 |
Correct |
1285 ms |
206500 KB |
Output is correct |
29 |
Correct |
4367 ms |
228588 KB |
Output is correct |
30 |
Correct |
4062 ms |
252140 KB |
Output is correct |
31 |
Correct |
4325 ms |
276932 KB |
Output is correct |
32 |
Correct |
1455 ms |
276932 KB |
Output is correct |
33 |
Correct |
508 ms |
276932 KB |
Output is correct |
34 |
Correct |
830 ms |
276932 KB |
Output is correct |
35 |
Correct |
901 ms |
276932 KB |
Output is correct |
36 |
Correct |
1140 ms |
276932 KB |
Output is correct |
37 |
Correct |
1127 ms |
276932 KB |
Output is correct |