#include <bits/stdc++.h>
#include "factories.h"
using namespace std;
const int N = 5e5 + 10;
const long long INF = 1e18 + 7;
vector <pair <int, int>> adj[N];
// Centroid Decomposition
int sz[N], ancestor[N], level[N];
long long dist[N][20];
long long min_dist[N];
bool processed[N];
int find_size(int u, int p) {
sz[u] = 1;
for(auto [v, w] : adj[u]) {
if(v == p or processed[v] == true) {
continue;
}
sz[u] += find_size(v, u);
}
return sz[u];
}
int find_centroid(int u, int p, int n) {
for(auto [v, w] : adj[u]) {
if(v == p or processed[v] == true) {
continue;
}
if(sz[v] > n/2) {
return find_centroid(v, u, n);
}
}
return u;
}
void get_distance(int u, int p, int l) {
for(auto [v, w] : adj[u]) {
if(v == p or processed[v] == true) {
continue;
}
dist[v][l] = dist[u][l] + w;
get_distance(v, u, l);
}
}
void build_centroid(int u, int p) {
int c = find_centroid(u, -1, find_size(u, -1));
ancestor[c] = p;
processed[c] = true;
level[c] = level[p] + 1;
get_distance(c, -1, level[c]);
for(auto [v, w] : adj[c]) {
if(processed[v] == true) {
continue;
}
build_centroid(v, c);
}
}
void Init(int N, int A[], int B[], int D[]) {
for(int i = 0; i < N - 1; i++) {
A[i]++, B[i]++;
adj[A[i]].emplace_back(B[i], D[i]);
adj[B[i]].emplace_back(A[i], D[i]);
}
build_centroid(1, 0);
for(int i = 1; i <= N; i++) {
min_dist[i] = INF;
}
}
void reset(int u) {
int a = u;
while(a != 0) {
min_dist[a] = INF;
a = ancestor[a];
}
}
void update(int u) {
int a = u;
while(a != 0) {
min_dist[a] = min(min_dist[a], dist[u][level[a]]);
a = ancestor[a];
}
}
long long query(int u) {
int a = u;
long long res = INF;
while(a != 0) {
res = min(res, min_dist[a] + dist[u][level[a]]);
a = ancestor[a];
}
return res;
}
long long Query(int S, int X[], int T, int Y[]) {
for(int i = 0; i < S; i++) {
update(X[i] + 1);
}
long long ans = INF;
for(int i = 0; i < T; i++) {
ans = min(ans, query(Y[i] + 1));
}
for(int i = 0; i < S; i++) {
reset(X[i] + 1);
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
12628 KB |
Output is correct |
2 |
Correct |
256 ms |
30724 KB |
Output is correct |
3 |
Correct |
288 ms |
30660 KB |
Output is correct |
4 |
Correct |
291 ms |
30664 KB |
Output is correct |
5 |
Correct |
293 ms |
30864 KB |
Output is correct |
6 |
Correct |
208 ms |
30600 KB |
Output is correct |
7 |
Correct |
287 ms |
30684 KB |
Output is correct |
8 |
Correct |
335 ms |
30728 KB |
Output is correct |
9 |
Correct |
307 ms |
30916 KB |
Output is correct |
10 |
Correct |
191 ms |
30680 KB |
Output is correct |
11 |
Correct |
294 ms |
30780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12244 KB |
Output is correct |
2 |
Correct |
1631 ms |
150768 KB |
Output is correct |
3 |
Correct |
2493 ms |
151632 KB |
Output is correct |
4 |
Correct |
574 ms |
151140 KB |
Output is correct |
5 |
Correct |
3437 ms |
174900 KB |
Output is correct |
6 |
Correct |
2486 ms |
153792 KB |
Output is correct |
7 |
Correct |
738 ms |
58480 KB |
Output is correct |
8 |
Correct |
318 ms |
59156 KB |
Output is correct |
9 |
Correct |
928 ms |
61960 KB |
Output is correct |
10 |
Correct |
756 ms |
59808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
12628 KB |
Output is correct |
2 |
Correct |
256 ms |
30724 KB |
Output is correct |
3 |
Correct |
288 ms |
30660 KB |
Output is correct |
4 |
Correct |
291 ms |
30664 KB |
Output is correct |
5 |
Correct |
293 ms |
30864 KB |
Output is correct |
6 |
Correct |
208 ms |
30600 KB |
Output is correct |
7 |
Correct |
287 ms |
30684 KB |
Output is correct |
8 |
Correct |
335 ms |
30728 KB |
Output is correct |
9 |
Correct |
307 ms |
30916 KB |
Output is correct |
10 |
Correct |
191 ms |
30680 KB |
Output is correct |
11 |
Correct |
294 ms |
30780 KB |
Output is correct |
12 |
Correct |
8 ms |
12244 KB |
Output is correct |
13 |
Correct |
1631 ms |
150768 KB |
Output is correct |
14 |
Correct |
2493 ms |
151632 KB |
Output is correct |
15 |
Correct |
574 ms |
151140 KB |
Output is correct |
16 |
Correct |
3437 ms |
174900 KB |
Output is correct |
17 |
Correct |
2486 ms |
153792 KB |
Output is correct |
18 |
Correct |
738 ms |
58480 KB |
Output is correct |
19 |
Correct |
318 ms |
59156 KB |
Output is correct |
20 |
Correct |
928 ms |
61960 KB |
Output is correct |
21 |
Correct |
756 ms |
59808 KB |
Output is correct |
22 |
Correct |
1939 ms |
157904 KB |
Output is correct |
23 |
Correct |
2089 ms |
160584 KB |
Output is correct |
24 |
Correct |
3150 ms |
159756 KB |
Output is correct |
25 |
Correct |
3068 ms |
163788 KB |
Output is correct |
26 |
Correct |
3137 ms |
159900 KB |
Output is correct |
27 |
Correct |
4066 ms |
179088 KB |
Output is correct |
28 |
Correct |
724 ms |
161724 KB |
Output is correct |
29 |
Correct |
3145 ms |
159576 KB |
Output is correct |
30 |
Correct |
3147 ms |
159148 KB |
Output is correct |
31 |
Correct |
3135 ms |
159652 KB |
Output is correct |
32 |
Correct |
920 ms |
62968 KB |
Output is correct |
33 |
Correct |
323 ms |
59588 KB |
Output is correct |
34 |
Correct |
621 ms |
56108 KB |
Output is correct |
35 |
Correct |
622 ms |
56216 KB |
Output is correct |
36 |
Correct |
754 ms |
56540 KB |
Output is correct |
37 |
Correct |
753 ms |
56724 KB |
Output is correct |