#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
const long long inf = (long long)1e18 + 20;
const int ms = 5e5 + 20;
const int st = 22;
vector<pair<int, int>> adj[ms];
long long dt[ms][st];
long long mi[ms];
int de[ms];
int par[ms];
bool f[ms];
int sz[ms];
void dfs1(int u, int pr) {
sz[u] = 1;
for (auto x: adj[u]) {
int v = x.first;
if (v != pr && !f[v]) {
dfs1(v, u);
sz[u] += sz[v];
}
}
}
int ctr(int r, int u, int pr) {
for (auto x: adj[u]) {
int v = x.first;
if (v != pr && !f[v]) {
if (sz[v] * 2 > sz[r]) {
return ctr(r, v, u);
}
}
}
return u;
}
void dfs2(int u, int pr, int d) {
for (auto x: adj[u]) {
int v = x.first;
int w = x.second;
if (v != pr && !f[v]) {
dt[v][d] = dt[u][d] + w;
dfs2(v, u, d);
}
}
}
void build(int u, int pr, int d) {
dfs1(u, -1);
int v = ctr(u, u, -1);
f[v] = true;
de[v] = d;
par[v] = pr;
dt[v][d] = 0;
dfs2(v, -1, d);
for (auto x: adj[v]) {
int w = x.first;
if (!f[w]) {
build(w, v, d + 1);
}
}
}
void Init(int N, int A[], int B[], int D[]) {
for (int i = 1; i <= N; i++) {
mi[i] = inf;
}
for (int i = 0; i < N - 1; i++) {
int u = A[i] + 1;
int v = B[i] + 1;
int w = D[i];
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
build(1, -1, 1);
}
long long Query(int S, int X[], int T, int Y[]) {
for (int i = 0; i < S; i++) {
int u = X[i] + 1;
int d = de[u];
int cc = u;
while (d >= 1) {
mi[cc] = min(mi[cc], dt[u][d]);
cc = par[cc];
d--;
}
}
long long res = inf;
for (int i = 0; i < T; i++) {
int u = Y[i] + 1;
int d = de[u];
int cc = u;
while (d >= 1) {
res = min(res, mi[cc] + dt[u][d]);
cc = par[cc];
d--;
}
}
for (int i = 0; i < S; i++) {
int u = X[i] + 1;
int d = de[u];
int cc = u;
while (d >= 1) {
mi[cc] = inf;
cc = par[cc];
d--;
}
}
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12372 KB |
Output is correct |
2 |
Correct |
226 ms |
21340 KB |
Output is correct |
3 |
Correct |
235 ms |
22396 KB |
Output is correct |
4 |
Correct |
236 ms |
22472 KB |
Output is correct |
5 |
Correct |
252 ms |
22712 KB |
Output is correct |
6 |
Correct |
175 ms |
22464 KB |
Output is correct |
7 |
Correct |
238 ms |
22532 KB |
Output is correct |
8 |
Correct |
238 ms |
22476 KB |
Output is correct |
9 |
Correct |
266 ms |
22680 KB |
Output is correct |
10 |
Correct |
185 ms |
22368 KB |
Output is correct |
11 |
Correct |
243 ms |
22444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12244 KB |
Output is correct |
2 |
Correct |
1403 ms |
139956 KB |
Output is correct |
3 |
Correct |
2168 ms |
142400 KB |
Output is correct |
4 |
Correct |
523 ms |
140608 KB |
Output is correct |
5 |
Correct |
3021 ms |
170700 KB |
Output is correct |
6 |
Correct |
2293 ms |
143696 KB |
Output is correct |
7 |
Correct |
676 ms |
46196 KB |
Output is correct |
8 |
Correct |
288 ms |
46712 KB |
Output is correct |
9 |
Correct |
772 ms |
50316 KB |
Output is correct |
10 |
Correct |
670 ms |
47456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12372 KB |
Output is correct |
2 |
Correct |
226 ms |
21340 KB |
Output is correct |
3 |
Correct |
235 ms |
22396 KB |
Output is correct |
4 |
Correct |
236 ms |
22472 KB |
Output is correct |
5 |
Correct |
252 ms |
22712 KB |
Output is correct |
6 |
Correct |
175 ms |
22464 KB |
Output is correct |
7 |
Correct |
238 ms |
22532 KB |
Output is correct |
8 |
Correct |
238 ms |
22476 KB |
Output is correct |
9 |
Correct |
266 ms |
22680 KB |
Output is correct |
10 |
Correct |
185 ms |
22368 KB |
Output is correct |
11 |
Correct |
243 ms |
22444 KB |
Output is correct |
12 |
Correct |
7 ms |
12244 KB |
Output is correct |
13 |
Correct |
1403 ms |
139956 KB |
Output is correct |
14 |
Correct |
2168 ms |
142400 KB |
Output is correct |
15 |
Correct |
523 ms |
140608 KB |
Output is correct |
16 |
Correct |
3021 ms |
170700 KB |
Output is correct |
17 |
Correct |
2293 ms |
143696 KB |
Output is correct |
18 |
Correct |
676 ms |
46196 KB |
Output is correct |
19 |
Correct |
288 ms |
46712 KB |
Output is correct |
20 |
Correct |
772 ms |
50316 KB |
Output is correct |
21 |
Correct |
670 ms |
47456 KB |
Output is correct |
22 |
Correct |
1677 ms |
142028 KB |
Output is correct |
23 |
Correct |
1765 ms |
144300 KB |
Output is correct |
24 |
Correct |
2754 ms |
144460 KB |
Output is correct |
25 |
Correct |
2729 ms |
148132 KB |
Output is correct |
26 |
Correct |
2689 ms |
144816 KB |
Output is correct |
27 |
Correct |
3427 ms |
167884 KB |
Output is correct |
28 |
Correct |
673 ms |
145072 KB |
Output is correct |
29 |
Correct |
2651 ms |
144280 KB |
Output is correct |
30 |
Correct |
2687 ms |
143820 KB |
Output is correct |
31 |
Correct |
2697 ms |
144412 KB |
Output is correct |
32 |
Correct |
823 ms |
61740 KB |
Output is correct |
33 |
Correct |
319 ms |
57432 KB |
Output is correct |
34 |
Correct |
526 ms |
54636 KB |
Output is correct |
35 |
Correct |
529 ms |
54636 KB |
Output is correct |
36 |
Correct |
685 ms |
55224 KB |
Output is correct |
37 |
Correct |
681 ms |
55000 KB |
Output is correct |