#include <iostream>
#include <vector>
#include <algorithm>
#include "factories.h"
using namespace std;
using ll = long long;
const int maxn = 5e5 + 1;
vector <pair <int, int>> adj[maxn];
int anc[maxn][20];
bool blocked[maxn];
int sz[maxn], visit[maxn], finish[maxn];
ll depth[maxn];
vector <int> centroids[maxn];
ll dis[maxn];
int get_subtree_sz(int s, int p) {
sz[s] = 1;
for (auto i : adj[s]) {
if (blocked[i.first] || i.first == p)
continue;
sz[s] += get_subtree_sz(i.first, s);
}
return sz[s];
}
int get_centroid(int s, int p, int st_sz) {
for (auto i : adj[s]) {
if (i.first == p || blocked[i.first])
continue;
if (sz[i.first] * 2 > st_sz)
return get_centroid(i.first, s, st_sz);
}
return s;
}
void add_centroid(int s, int p, int centroid) {
centroids[s].push_back(centroid);
for (auto i : adj[s]) {
if (i.first == p || blocked[i.first])
continue;
add_centroid(i.first, s, centroid);
}
}
void dfs(int s) {
int centroid = get_centroid(s, s, get_subtree_sz(s, s));
blocked[centroid] = true;
add_centroid(centroid, centroid, centroid);
for (auto i : adj[centroid]) {
if (blocked[i.first])
continue;
dfs(i.first);
}
}
int t = 0;
void dfs2(int s, int p) {
anc[s][0] = p;
visit[s] = t++;
for (int i = 1; i < 20; i++)
anc[s][i] = anc[anc[s][i-1]][i-1];
for (auto i : adj[s]) {
if (i.first == p)
continue;
depth[i.first] = depth[s] + i.second;
dfs2(i.first, s);
}
finish[s] = t++;
}
bool is_ancestor(int a, int b) {
return visit[a] <= visit[b] && finish[a] >= finish[b];
}
int get_lca(int a, int b) {
if (is_ancestor(a, b))
return a;
if (is_ancestor(b, a))
return b;
int lca = a;
for (int i = 19; i >= 0; i--) {
if (!is_ancestor(anc[lca][i], b))
lca = anc[lca][i];
}
return anc[lca][0];
}
void update_dis(int s) {
for (auto centroid : centroids[s]) {
int lca = get_lca(centroid, s);
dis[centroid] = min(dis[centroid], depth[centroid] + depth[s] - 2 * depth[lca]);
}
}
ll get_ans(int s) {
ll ans = 1e16;
for (auto centroid : centroids[s]) {
int lca = get_lca(centroid, s);
ans = min(ans, dis[centroid] + depth[centroid] + depth[s] - 2 * depth[lca]);
}
return ans;
}
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]].push_back({B[i], D[i]});
adj[B[i]].push_back({A[i], D[i]});
}
dfs(1);
dfs2(1, 1);
fill(dis, dis + maxn, 1e16);
}
ll Query(int S, int X[], int T, int Y[]) {
for (int i = 0; i < S; i++) {
update_dis(X[i]+1);
}
ll ans = 1e16;
for (int i = 0; i < T; i++) {
ll tp = get_ans(Y[i] + 1);
ans = min(ans, tp);
}
// reiinitilise dis array so we don't have to reset all the values
for (int i = 0; i < S; i++) {
for (auto centroid : centroids[X[i] + 1])
dis[centroid] = 1e16;
}
return ans;
}
// int main() {
// int N, q;
// cin >> N >> q;
// int A[maxn], B[maxn], D[maxn];
// for (int i = 0; i < N-1; i++)
// cin >> A[i] >> B[i] >> D[i];
// Init(N, A, B, D);
// while (q--) {
// int S, T;
// cin >> S >> T;
// int X[maxn], Y[maxn];
// for (int i = 0; i < S; i++)
// cin >> X[i];
// for (int i = 0; i < T; i++)
// cin >> Y[i];
// cout << Query(S, X, T, Y) << '\n';
// }
// }
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
54104 KB |
Output is correct |
2 |
Correct |
437 ms |
59120 KB |
Output is correct |
3 |
Correct |
891 ms |
59220 KB |
Output is correct |
4 |
Correct |
878 ms |
59132 KB |
Output is correct |
5 |
Correct |
427 ms |
59220 KB |
Output is correct |
6 |
Correct |
159 ms |
58704 KB |
Output is correct |
7 |
Correct |
883 ms |
59064 KB |
Output is correct |
8 |
Correct |
949 ms |
59140 KB |
Output is correct |
9 |
Correct |
452 ms |
59308 KB |
Output is correct |
10 |
Correct |
162 ms |
58628 KB |
Output is correct |
11 |
Correct |
880 ms |
58964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
53848 KB |
Output is correct |
2 |
Correct |
2001 ms |
156220 KB |
Output is correct |
3 |
Correct |
4040 ms |
193560 KB |
Output is correct |
4 |
Correct |
721 ms |
154040 KB |
Output is correct |
5 |
Correct |
3429 ms |
229068 KB |
Output is correct |
6 |
Correct |
4607 ms |
194808 KB |
Output is correct |
7 |
Correct |
2708 ms |
93808 KB |
Output is correct |
8 |
Correct |
339 ms |
89172 KB |
Output is correct |
9 |
Correct |
966 ms |
98724 KB |
Output is correct |
10 |
Correct |
2585 ms |
94452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
54104 KB |
Output is correct |
2 |
Correct |
437 ms |
59120 KB |
Output is correct |
3 |
Correct |
891 ms |
59220 KB |
Output is correct |
4 |
Correct |
878 ms |
59132 KB |
Output is correct |
5 |
Correct |
427 ms |
59220 KB |
Output is correct |
6 |
Correct |
159 ms |
58704 KB |
Output is correct |
7 |
Correct |
883 ms |
59064 KB |
Output is correct |
8 |
Correct |
949 ms |
59140 KB |
Output is correct |
9 |
Correct |
452 ms |
59308 KB |
Output is correct |
10 |
Correct |
162 ms |
58628 KB |
Output is correct |
11 |
Correct |
880 ms |
58964 KB |
Output is correct |
12 |
Correct |
10 ms |
53848 KB |
Output is correct |
13 |
Correct |
2001 ms |
156220 KB |
Output is correct |
14 |
Correct |
4040 ms |
193560 KB |
Output is correct |
15 |
Correct |
721 ms |
154040 KB |
Output is correct |
16 |
Correct |
3429 ms |
229068 KB |
Output is correct |
17 |
Correct |
4607 ms |
194808 KB |
Output is correct |
18 |
Correct |
2708 ms |
93808 KB |
Output is correct |
19 |
Correct |
339 ms |
89172 KB |
Output is correct |
20 |
Correct |
966 ms |
98724 KB |
Output is correct |
21 |
Correct |
2585 ms |
94452 KB |
Output is correct |
22 |
Correct |
2564 ms |
179312 KB |
Output is correct |
23 |
Correct |
2729 ms |
180936 KB |
Output is correct |
24 |
Correct |
6160 ms |
198828 KB |
Output is correct |
25 |
Correct |
6043 ms |
200924 KB |
Output is correct |
26 |
Correct |
6014 ms |
199252 KB |
Output is correct |
27 |
Correct |
3808 ms |
230228 KB |
Output is correct |
28 |
Correct |
901 ms |
160436 KB |
Output is correct |
29 |
Correct |
5901 ms |
198980 KB |
Output is correct |
30 |
Correct |
5950 ms |
198436 KB |
Output is correct |
31 |
Correct |
6019 ms |
198896 KB |
Output is correct |
32 |
Correct |
928 ms |
99432 KB |
Output is correct |
33 |
Correct |
268 ms |
89028 KB |
Output is correct |
34 |
Correct |
849 ms |
91220 KB |
Output is correct |
35 |
Correct |
878 ms |
91480 KB |
Output is correct |
36 |
Correct |
2382 ms |
93176 KB |
Output is correct |
37 |
Correct |
2326 ms |
93012 KB |
Output is correct |