#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 5e5 + 2;
const int M = 1e5 + 2;
int mod = 1000000007;
vector<array<int, 2>> g[N];
int sz[N];
bool was[N];
ll mn[N];
void Dfs(int s, int e) {
sz[s] = 1;
for (auto u : g[s]) {
if (u[0] == e || was[u[0]]) continue;
Dfs(u[0], s);
sz[s] += sz[u[0]];
}
}
int Find(int s, int e, int n) {
for (auto u : g[s]) {
if (u[0] == e || was[u[0]]) continue;
if (2 * sz[u[0]] > n) return Find(u[0], s, n);
}
return s;
}
int FindCentroid(int node) {
Dfs(node, -1);
return Find(node, -1, sz[node]);
}
vector<array<ll, 2>> p[N];
void Solve(int s, int e, int r, ll d) {
p[s].push_back({r, d});
for (auto u : g[s]) {
if (was[u[0]] || u[0] == e) continue;
Solve(u[0], s, r, d + u[1]);
}
}
void Decompose(int s) {
s = FindCentroid(s);
was[s] = 1;
Solve(s, -1, s, 0);
for (auto u : g[s]) {
if (!was[u[0]]) Decompose(u[0]);
}
}
void Init(int n, int *a, int *b, int* d) {
for (int i = 0; i < n - 1; i++) {
g[a[i]].push_back({b[i], d[i]});
g[b[i]].push_back({a[i], d[i]});
}
Decompose(0);
for (int i = 0; i < n; i++) mn[i] = 1e18;
}
ll Query(int a, int *x, int b, int *y) {
vector<int> ch;
for (int j = 0; j < a; j++) {
for (auto d : p[x[j]]) {
smin(mn[d[0]], d[1]);
ch.push_back(d[0]);
}
}
ll ans = 1e18;
for (int j = 0; j < b; j++) {
for (auto d : p[y[j]]) {
smin(ans, mn[d[0]] + d[1]);
}
}
for (int u : ch) mn[u] = 1e18;
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
24148 KB |
Output is correct |
2 |
Correct |
281 ms |
32976 KB |
Output is correct |
3 |
Correct |
298 ms |
33356 KB |
Output is correct |
4 |
Correct |
364 ms |
33668 KB |
Output is correct |
5 |
Correct |
327 ms |
33896 KB |
Output is correct |
6 |
Correct |
235 ms |
32332 KB |
Output is correct |
7 |
Correct |
306 ms |
33300 KB |
Output is correct |
8 |
Correct |
318 ms |
33372 KB |
Output is correct |
9 |
Correct |
318 ms |
34036 KB |
Output is correct |
10 |
Correct |
266 ms |
32328 KB |
Output is correct |
11 |
Correct |
301 ms |
33324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23960 KB |
Output is correct |
2 |
Correct |
2239 ms |
188780 KB |
Output is correct |
3 |
Correct |
3283 ms |
276964 KB |
Output is correct |
4 |
Correct |
990 ms |
104232 KB |
Output is correct |
5 |
Correct |
4396 ms |
356132 KB |
Output is correct |
6 |
Correct |
3384 ms |
278160 KB |
Output is correct |
7 |
Correct |
987 ms |
81328 KB |
Output is correct |
8 |
Correct |
455 ms |
59096 KB |
Output is correct |
9 |
Correct |
1167 ms |
94968 KB |
Output is correct |
10 |
Correct |
1048 ms |
82740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
24148 KB |
Output is correct |
2 |
Correct |
281 ms |
32976 KB |
Output is correct |
3 |
Correct |
298 ms |
33356 KB |
Output is correct |
4 |
Correct |
364 ms |
33668 KB |
Output is correct |
5 |
Correct |
327 ms |
33896 KB |
Output is correct |
6 |
Correct |
235 ms |
32332 KB |
Output is correct |
7 |
Correct |
306 ms |
33300 KB |
Output is correct |
8 |
Correct |
318 ms |
33372 KB |
Output is correct |
9 |
Correct |
318 ms |
34036 KB |
Output is correct |
10 |
Correct |
266 ms |
32328 KB |
Output is correct |
11 |
Correct |
301 ms |
33324 KB |
Output is correct |
12 |
Correct |
15 ms |
23960 KB |
Output is correct |
13 |
Correct |
2239 ms |
188780 KB |
Output is correct |
14 |
Correct |
3283 ms |
276964 KB |
Output is correct |
15 |
Correct |
990 ms |
104232 KB |
Output is correct |
16 |
Correct |
4396 ms |
356132 KB |
Output is correct |
17 |
Correct |
3384 ms |
278160 KB |
Output is correct |
18 |
Correct |
987 ms |
81328 KB |
Output is correct |
19 |
Correct |
455 ms |
59096 KB |
Output is correct |
20 |
Correct |
1167 ms |
94968 KB |
Output is correct |
21 |
Correct |
1048 ms |
82740 KB |
Output is correct |
22 |
Correct |
2463 ms |
216348 KB |
Output is correct |
23 |
Correct |
2525 ms |
223580 KB |
Output is correct |
24 |
Correct |
3772 ms |
291388 KB |
Output is correct |
25 |
Correct |
3850 ms |
293628 KB |
Output is correct |
26 |
Correct |
3707 ms |
285600 KB |
Output is correct |
27 |
Correct |
4563 ms |
364144 KB |
Output is correct |
28 |
Correct |
1027 ms |
115476 KB |
Output is correct |
29 |
Correct |
3703 ms |
284496 KB |
Output is correct |
30 |
Correct |
3721 ms |
284004 KB |
Output is correct |
31 |
Correct |
3754 ms |
284896 KB |
Output is correct |
32 |
Correct |
1179 ms |
100200 KB |
Output is correct |
33 |
Correct |
482 ms |
59936 KB |
Output is correct |
34 |
Correct |
791 ms |
74080 KB |
Output is correct |
35 |
Correct |
822 ms |
74732 KB |
Output is correct |
36 |
Correct |
962 ms |
79844 KB |
Output is correct |
37 |
Correct |
924 ms |
79820 KB |
Output is correct |