#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define lc (i << 1)
#define rc (i << 1 | 1)
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int MN = 5e5 + 5, LN = 17, MOD = 1e9 + 7, INF = 0x3f3f3f3f, BSZ = 320;
vector<pii> g[MN];
int sz[MN], par[MN], dep[MN], qid[MN], id = 1;
ll dis[20][MN], dp[MN];
bool vis[MN];
int get_sz(int u, int p) {
sz[u] = 1;
for (pii e : g[u]) if (e.first != p && !vis[e.first])
sz[u] += get_sz(e.first, u);
return sz[u];
}
int centroid(int u, int p, int s) {
for (pii e : g[u]) if (e.first != p && !vis[e.first] && sz[e.first] > s / 2)
return centroid(e.first, u, s);
return u;
}
void dfs(int u, int p, ll d, int lvl) {
dis[lvl][u] = d;
for (pii e : g[u]) if (e.first != p && !vis[e.first])
dfs(e.first, u, d + e.second, lvl);
}
void go(int u, int p, int lvl) {
int s = get_sz(u, -1);
int c = centroid(u, -1, s);
dep[c] = lvl;
par[c] = p;
dfs(c, -1, 0, lvl);
vis[c] = true;
for (pii e : g[c])
if (!vis[e.first])
go(e.first, c, lvl + 1);
}
void Init(int N, int A[], int B[], int D[]) {
for (int i = 0; i < N - 1; i++) {
int u = A[i], v = B[i], w = D[i];
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
go(0, -1, 0);
}
ll Query(int S, int X[], int T, int Y[]) {
ll ret = LLONG_MAX;
for (int i = 0; i < S; i++) {
int cur = X[i];
while (cur != -1) {
if (qid[cur] != id) dp[cur] = dis[dep[cur]][X[i]], qid[cur] = id;
else dp[cur] = min(dp[cur], dis[dep[cur]][X[i]]);
cur = par[cur];
}
}
for (int i = 0; i < T; i++) {
int cur = Y[i];
while (cur != -1) {
if (qid[cur] == id) ret = min(ret, dp[cur] + dis[dep[cur]][Y[i]]);
cur = par[cur];
}
}
id++;
return ret;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
12544 KB |
Output is correct |
2 |
Correct |
383 ms |
30456 KB |
Output is correct |
3 |
Correct |
411 ms |
30584 KB |
Output is correct |
4 |
Correct |
411 ms |
30584 KB |
Output is correct |
5 |
Correct |
442 ms |
30840 KB |
Output is correct |
6 |
Correct |
305 ms |
30072 KB |
Output is correct |
7 |
Correct |
416 ms |
30588 KB |
Output is correct |
8 |
Correct |
420 ms |
30528 KB |
Output is correct |
9 |
Correct |
439 ms |
30712 KB |
Output is correct |
10 |
Correct |
311 ms |
30012 KB |
Output is correct |
11 |
Correct |
416 ms |
30456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
12288 KB |
Output is correct |
2 |
Correct |
2028 ms |
115704 KB |
Output is correct |
3 |
Correct |
3123 ms |
131528 KB |
Output is correct |
4 |
Correct |
757 ms |
64992 KB |
Output is correct |
5 |
Correct |
4215 ms |
148364 KB |
Output is correct |
6 |
Correct |
3196 ms |
132984 KB |
Output is correct |
7 |
Correct |
982 ms |
55032 KB |
Output is correct |
8 |
Correct |
453 ms |
44012 KB |
Output is correct |
9 |
Correct |
1143 ms |
58568 KB |
Output is correct |
10 |
Correct |
992 ms |
56316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
12544 KB |
Output is correct |
2 |
Correct |
383 ms |
30456 KB |
Output is correct |
3 |
Correct |
411 ms |
30584 KB |
Output is correct |
4 |
Correct |
411 ms |
30584 KB |
Output is correct |
5 |
Correct |
442 ms |
30840 KB |
Output is correct |
6 |
Correct |
305 ms |
30072 KB |
Output is correct |
7 |
Correct |
416 ms |
30588 KB |
Output is correct |
8 |
Correct |
420 ms |
30528 KB |
Output is correct |
9 |
Correct |
439 ms |
30712 KB |
Output is correct |
10 |
Correct |
311 ms |
30012 KB |
Output is correct |
11 |
Correct |
416 ms |
30456 KB |
Output is correct |
12 |
Correct |
13 ms |
12288 KB |
Output is correct |
13 |
Correct |
2028 ms |
115704 KB |
Output is correct |
14 |
Correct |
3123 ms |
131528 KB |
Output is correct |
15 |
Correct |
757 ms |
64992 KB |
Output is correct |
16 |
Correct |
4215 ms |
148364 KB |
Output is correct |
17 |
Correct |
3196 ms |
132984 KB |
Output is correct |
18 |
Correct |
982 ms |
55032 KB |
Output is correct |
19 |
Correct |
453 ms |
44012 KB |
Output is correct |
20 |
Correct |
1143 ms |
58568 KB |
Output is correct |
21 |
Correct |
992 ms |
56316 KB |
Output is correct |
22 |
Correct |
2630 ms |
116008 KB |
Output is correct |
23 |
Correct |
2467 ms |
120440 KB |
Output is correct |
24 |
Correct |
4215 ms |
133756 KB |
Output is correct |
25 |
Correct |
4169 ms |
137032 KB |
Output is correct |
26 |
Correct |
4175 ms |
133900 KB |
Output is correct |
27 |
Correct |
5332 ms |
147880 KB |
Output is correct |
28 |
Correct |
942 ms |
68832 KB |
Output is correct |
29 |
Correct |
3982 ms |
133940 KB |
Output is correct |
30 |
Correct |
4089 ms |
133476 KB |
Output is correct |
31 |
Correct |
4017 ms |
133672 KB |
Output is correct |
32 |
Correct |
1198 ms |
58744 KB |
Output is correct |
33 |
Correct |
454 ms |
44008 KB |
Output is correct |
34 |
Correct |
772 ms |
50280 KB |
Output is correct |
35 |
Correct |
766 ms |
50040 KB |
Output is correct |
36 |
Correct |
994 ms |
53048 KB |
Output is correct |
37 |
Correct |
1004 ms |
53104 KB |
Output is correct |