#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long i64;
const int MAX_N = 500000 + 5;
const i64 INF = 1e18;
vector<pair<int, int>> g[MAX_N];
vector<pair<int, i64>> par[MAX_N];
int sub[MAX_N], del[MAX_N];
i64 best_way[MAX_N];
int calc_subtree(int node, int p) {
sub[node] = 1;
for (auto to : g[node]) {
if (to.first != p && !del[to.first]) {
sub[node] += calc_subtree(to.first, node);
}
}
return sub[node];
}
int calc_centroid(int node, int p, const int tam) {
for (auto to : g[node]) {
if (!del[to.first] && to.first != p && sub[to.first] > tam / 2) {
return calc_centroid(to.first, node, tam);
}
}
return node;
}
void solve(int node, int p, const int centroid, i64 w) {
par[node].emplace_back(centroid, w);
for (auto to : g[node]) {
if (!del[to.first] && to.first != p) {
solve(to.first, node, centroid, w + to.second);
}
}
}
void decompose(int node) {
const int centroid = calc_centroid(node, -1, calc_subtree(node, -1));
del[centroid] = 1;
par[centroid].emplace_back(centroid, 0);
for (auto to : g[centroid]) {
if (!del[to.first]) {
solve(to.first, -1, centroid, to.second);
decompose(to.first);
}
}
}
void Init(int N, int A[], int B[], int D[]) {
for (int i = 0; i < N - 1; i++) {
const int st = A[i];
const int et = B[i];
const int w = D[i];
g[st].emplace_back(et, w);
g[et].emplace_back(st, w);
}
decompose(0);
for (int i = 0; i < MAX_N; i++) {
best_way[i] = INF;
}
}
i64 Query(int S, int X[], int T, int Y[]) {
i64 mn = INF;
for (int i = 0; i < S; i++) {
for (auto to : par[X[i]]) {
best_way[to.first] = min(best_way[to.first], to.second);
}
}
for (int i = 0; i < T; i++) {
for (auto to : par[Y[i]]) {
mn = min(mn, best_way[to.first] + to.second);
}
}
for (int i = 0; i < S; i++) {
for (auto to : par[X[i]]) {
best_way[to.first] = INF;
}
}
return mn;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
28288 KB |
Output is correct |
2 |
Correct |
412 ms |
46328 KB |
Output is correct |
3 |
Correct |
443 ms |
46840 KB |
Output is correct |
4 |
Correct |
449 ms |
46712 KB |
Output is correct |
5 |
Correct |
466 ms |
47096 KB |
Output is correct |
6 |
Correct |
341 ms |
45692 KB |
Output is correct |
7 |
Correct |
446 ms |
46764 KB |
Output is correct |
8 |
Correct |
451 ms |
46840 KB |
Output is correct |
9 |
Correct |
466 ms |
47096 KB |
Output is correct |
10 |
Correct |
346 ms |
45816 KB |
Output is correct |
11 |
Correct |
435 ms |
46712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
28032 KB |
Output is correct |
2 |
Correct |
2594 ms |
208928 KB |
Output is correct |
3 |
Correct |
3792 ms |
266624 KB |
Output is correct |
4 |
Correct |
1050 ms |
105652 KB |
Output is correct |
5 |
Correct |
4930 ms |
344256 KB |
Output is correct |
6 |
Correct |
4038 ms |
277920 KB |
Output is correct |
7 |
Correct |
1327 ms |
84484 KB |
Output is correct |
8 |
Correct |
662 ms |
62728 KB |
Output is correct |
9 |
Correct |
1439 ms |
96928 KB |
Output is correct |
10 |
Correct |
1332 ms |
86008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
28288 KB |
Output is correct |
2 |
Correct |
412 ms |
46328 KB |
Output is correct |
3 |
Correct |
443 ms |
46840 KB |
Output is correct |
4 |
Correct |
449 ms |
46712 KB |
Output is correct |
5 |
Correct |
466 ms |
47096 KB |
Output is correct |
6 |
Correct |
341 ms |
45692 KB |
Output is correct |
7 |
Correct |
446 ms |
46764 KB |
Output is correct |
8 |
Correct |
451 ms |
46840 KB |
Output is correct |
9 |
Correct |
466 ms |
47096 KB |
Output is correct |
10 |
Correct |
346 ms |
45816 KB |
Output is correct |
11 |
Correct |
435 ms |
46712 KB |
Output is correct |
12 |
Correct |
19 ms |
28032 KB |
Output is correct |
13 |
Correct |
2594 ms |
208928 KB |
Output is correct |
14 |
Correct |
3792 ms |
266624 KB |
Output is correct |
15 |
Correct |
1050 ms |
105652 KB |
Output is correct |
16 |
Correct |
4930 ms |
344256 KB |
Output is correct |
17 |
Correct |
4038 ms |
277920 KB |
Output is correct |
18 |
Correct |
1327 ms |
84484 KB |
Output is correct |
19 |
Correct |
662 ms |
62728 KB |
Output is correct |
20 |
Correct |
1439 ms |
96928 KB |
Output is correct |
21 |
Correct |
1332 ms |
86008 KB |
Output is correct |
22 |
Correct |
3174 ms |
202252 KB |
Output is correct |
23 |
Correct |
3306 ms |
203044 KB |
Output is correct |
24 |
Correct |
4622 ms |
273136 KB |
Output is correct |
25 |
Correct |
4672 ms |
272068 KB |
Output is correct |
26 |
Correct |
4580 ms |
274272 KB |
Output is correct |
27 |
Correct |
5620 ms |
349588 KB |
Output is correct |
28 |
Correct |
1541 ms |
116140 KB |
Output is correct |
29 |
Correct |
4558 ms |
284712 KB |
Output is correct |
30 |
Correct |
4681 ms |
284388 KB |
Output is correct |
31 |
Correct |
4558 ms |
284908 KB |
Output is correct |
32 |
Correct |
1560 ms |
97184 KB |
Output is correct |
33 |
Correct |
695 ms |
63084 KB |
Output is correct |
34 |
Correct |
1083 ms |
77372 KB |
Output is correct |
35 |
Correct |
1092 ms |
78200 KB |
Output is correct |
36 |
Correct |
1302 ms |
82936 KB |
Output is correct |
37 |
Correct |
1310 ms |
82936 KB |
Output is correct |