#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
namespace
{
vector<vector<pair<int, long>>> adj, gr;
vector<int> sub;
vector<bool> done;
vector<long> dp;
} // namespace
int subtree(int u, int p)
{
sub[u] = 1;
for (auto [v, d] : adj[u])
{
if (v != p && !done[v])
sub[u] += subtree(v, u);
}
return sub[u];
}
int get_centroid(int sz, int u, int p)
{
for (auto [v, d] : adj[u])
{
if (v != p && !done[v])
{
if (sub[v] * 2 > sz)
return get_centroid(sz, v, u);
}
}
return u;
}
void dfs(int r, int u, int p, long dist)
{
gr[u].emplace_back(r, dist);
for (auto [v, d] : adj[u])
{
if (v != p && !done[v])
dfs(r, v, u, dist + d);
}
}
void rec(int u)
{
int cen = get_centroid(subtree(u, -1), u, -1);
dfs(cen, cen, -1, 0);
done[cen] = 1;
for (auto [v, d] : adj[cen])
{
if (!done[v])
rec(v);
}
}
void upd(int u)
{
for (auto [r, dist] : gr[u])
dp[r] = min(dp[r], dist);
}
long qry(int u)
{
long x = LONG_MAX;
for (auto [r, dist] : gr[u])
{
if (dp[r] != LONG_MAX)
x = min(x, dist + dp[r]);
}
return x;
}
void reset(int u)
{
for (auto [r, dist] : gr[u])
dp[r] = LONG_MAX;
}
void Init(int N, int A[], int B[], int D[])
{
adj.assign(N, vector<pair<int, long>>());
for (int i = 0; i < N - 1; i++)
{
adj[A[i]].emplace_back(B[i], D[i]);
adj[B[i]].emplace_back(A[i], D[i]);
}
sub.assign(N, 0);
done.assign(N, 0);
gr.assign(N, vector<pair<int, long>>());
rec(0);
dp.assign(N, LONG_MAX);
}
long long Query(int S, int X[], int T, int Y[])
{
for (int i = 0; i < S; i++)
upd(X[i]);
long cost = LONG_MAX;
for (int i = 0; i < T; i++)
cost = min(cost, qry(Y[i]));
for (int i = 0; i < S; i++)
reset(X[i]);
return cost;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
852 KB |
Output is correct |
2 |
Correct |
390 ms |
19096 KB |
Output is correct |
3 |
Correct |
355 ms |
19664 KB |
Output is correct |
4 |
Correct |
361 ms |
19400 KB |
Output is correct |
5 |
Correct |
388 ms |
19844 KB |
Output is correct |
6 |
Correct |
280 ms |
18476 KB |
Output is correct |
7 |
Correct |
403 ms |
19556 KB |
Output is correct |
8 |
Correct |
389 ms |
19596 KB |
Output is correct |
9 |
Correct |
409 ms |
19988 KB |
Output is correct |
10 |
Correct |
280 ms |
18412 KB |
Output is correct |
11 |
Correct |
441 ms |
19552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
508 KB |
Output is correct |
2 |
Correct |
2361 ms |
212808 KB |
Output is correct |
3 |
Correct |
3557 ms |
284428 KB |
Output is correct |
4 |
Correct |
875 ms |
107648 KB |
Output is correct |
5 |
Correct |
4266 ms |
366220 KB |
Output is correct |
6 |
Correct |
3589 ms |
286080 KB |
Output is correct |
7 |
Correct |
1043 ms |
64100 KB |
Output is correct |
8 |
Correct |
449 ms |
40976 KB |
Output is correct |
9 |
Correct |
1173 ms |
78084 KB |
Output is correct |
10 |
Correct |
1057 ms |
65484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
852 KB |
Output is correct |
2 |
Correct |
390 ms |
19096 KB |
Output is correct |
3 |
Correct |
355 ms |
19664 KB |
Output is correct |
4 |
Correct |
361 ms |
19400 KB |
Output is correct |
5 |
Correct |
388 ms |
19844 KB |
Output is correct |
6 |
Correct |
280 ms |
18476 KB |
Output is correct |
7 |
Correct |
403 ms |
19556 KB |
Output is correct |
8 |
Correct |
389 ms |
19596 KB |
Output is correct |
9 |
Correct |
409 ms |
19988 KB |
Output is correct |
10 |
Correct |
280 ms |
18412 KB |
Output is correct |
11 |
Correct |
441 ms |
19552 KB |
Output is correct |
12 |
Correct |
3 ms |
508 KB |
Output is correct |
13 |
Correct |
2361 ms |
212808 KB |
Output is correct |
14 |
Correct |
3557 ms |
284428 KB |
Output is correct |
15 |
Correct |
875 ms |
107648 KB |
Output is correct |
16 |
Correct |
4266 ms |
366220 KB |
Output is correct |
17 |
Correct |
3589 ms |
286080 KB |
Output is correct |
18 |
Correct |
1043 ms |
64100 KB |
Output is correct |
19 |
Correct |
449 ms |
40976 KB |
Output is correct |
20 |
Correct |
1173 ms |
78084 KB |
Output is correct |
21 |
Correct |
1057 ms |
65484 KB |
Output is correct |
22 |
Correct |
2742 ms |
219164 KB |
Output is correct |
23 |
Correct |
2709 ms |
224288 KB |
Output is correct |
24 |
Correct |
4070 ms |
292144 KB |
Output is correct |
25 |
Correct |
4035 ms |
296004 KB |
Output is correct |
26 |
Correct |
3662 ms |
292988 KB |
Output is correct |
27 |
Correct |
4505 ms |
368072 KB |
Output is correct |
28 |
Correct |
1152 ms |
118124 KB |
Output is correct |
29 |
Correct |
3816 ms |
292432 KB |
Output is correct |
30 |
Correct |
3700 ms |
291900 KB |
Output is correct |
31 |
Correct |
3707 ms |
292372 KB |
Output is correct |
32 |
Correct |
1237 ms |
78504 KB |
Output is correct |
33 |
Correct |
508 ms |
41456 KB |
Output is correct |
34 |
Correct |
843 ms |
56376 KB |
Output is correct |
35 |
Correct |
843 ms |
57304 KB |
Output is correct |
36 |
Correct |
1085 ms |
62356 KB |
Output is correct |
37 |
Correct |
1050 ms |
62480 KB |
Output is correct |