#include "factories.h"
#include <bits/stdc++.h>
template <class T>
using Vec = std::vector<T>;
using ll = long long;
constexpr ll INF = std::numeric_limits<ll>::max() / 2;
template <class F>
struct RecLambda: private F {
explicit RecLambda(F&& f): F(std::forward<F>(f)) { }
template <class... Args>
decltype(auto) operator () (Args&&... args) const {
return F::operator()(*this, std::forward<Args>(args)...);
}
};
int N;
Vec<Vec<std::pair<int, ll>>> graph;
Vec<int> in, depth, col;
Vec<ll> length;
std::array<Vec<int>, 20> parent;
void Init(int V, int A[], int B[], int D[]) {
N = V;
graph.resize(V);
in.resize(V);
depth.resize(V);
col.resize(V);
length.resize(V);
for (auto& v: parent) {
v.resize(V);
}
for (int i = 0; i < V - 1; ++i) {
graph[A[i]].emplace_back(B[i], D[i]);
graph[B[i]].emplace_back(A[i], D[i]);
}
int time = 0;
RecLambda([&](auto&& dfs, const int u, const int p, const int d, const ll l) -> void {
in[u] = time++;
depth[u] = d;
length[u] = l;
parent[0][u] = p;
for (const auto [v, c]: graph[u]) {
if (v != p) {
dfs(v, u, d + 1, l + c);
}
}
})(0, 0, 0, 0);
for (int i = 0; i < 19; ++i) {
for (int j = 0; j < V; ++j) {
parent[i + 1][j] = parent[i][parent[i][j]];
}
}
}
int lca(int u, int v) {
if (depth[u] < depth[v]) {
std::swap(u, v);
}
const auto dif = depth[u] - depth[v];
for (int i = 0; i < 20; ++i) {
if (dif >> i & 1) {
u = parent[i][u];
}
}
if (u == v) {
return u;
}
for (int i = 19; i >= 0; --i) {
if (parent[i][u] != parent[i][v]) {
u = parent[i][u];
v = parent[i][v];
}
}
return parent[0][u];
}
struct Node {
int uid;
int idx;
int pidx;
int chcnt;
};
long long Query(int S, int X[], int T, int Y[]) {
for (int i = 0; i < S; ++i) {
col[X[i]] = 1;
}
for (int i = 0; i < T; ++i) {
col[Y[i]] = 2;
}
Vec<Node> node;
const auto add_node = [&](const int u) {
const auto idx = (int) node.size();
node.push_back(Node { u, idx, idx, 0 });
return idx;
};
Vec<int> vs;
vs.reserve(S + T);
for (int i = 0; i < S; ++i) {
vs.push_back(X[i]);
}
for (int i = 0; i < T; ++i) {
vs.push_back(Y[i]);
}
std::sort(vs.begin(), vs.end(), [&](const int u, const int v) {
return in[u] < in[v];
});
std::stack<int> stack;
for (const auto u: vs) {
if (stack.empty()) {
stack.push(add_node(u));
continue;
}
const auto v = node[stack.top()].uid;
const auto w = lca(u, v);
int poped = -1;
while (!stack.empty() and depth[node[stack.top()].uid] > depth[w]) {
poped = stack.top();
stack.pop();
}
if (stack.empty() or node[stack.top()].uid != w) {
const auto i = add_node(w);
if (!stack.empty()) {
node[i].pidx = stack.top();
}
stack.push(i);
}
const auto pi = stack.top();
if (poped != -1) {
node[poped].pidx = pi;
}
const auto i = add_node(u);
node[i].pidx = pi;
stack.push(i);
}
const auto size = (int) node.size();
for (int i = 0; i < size; ++i) {
if (node[i].pidx != i) {
node[node[i].pidx].chcnt += 1;
}
}
std::queue<int> que;
for (int i = 0; i < size; ++i) {
if (node[i].chcnt == 0) {
que.push(i);
}
}
Vec<ll> black(size, INF), white(size, INF);
ll ans = INF;
while (!que.empty()) {
const auto i = que.front();
que.pop();
if (col[node[i].uid] == 1) {
black[i] = 0;
}
if (col[node[i].uid] == 2) {
white[i] = 0;
}
ans = std::min(ans, black[i] + white[i]);
const auto pi = node[i].pidx;
if (pi != i) {
const auto len = length[node[i].uid] - length[node[pi].uid];
black[pi] = std::min(black[pi], black[i] + len);
white[pi] = std::min(white[pi], white[i] + len);
node[pi].chcnt -= 1;
if (node[pi].chcnt == 0) {
que.push(pi);
}
}
}
for (int i = 0; i < S; ++i) {
col[X[i]] = 0;
}
for (int i = 0; i < T; ++i) {
col[Y[i]] = 0;
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
716 KB |
Output is correct |
2 |
Correct |
748 ms |
18852 KB |
Output is correct |
3 |
Correct |
800 ms |
18940 KB |
Output is correct |
4 |
Correct |
762 ms |
18952 KB |
Output is correct |
5 |
Correct |
593 ms |
19152 KB |
Output is correct |
6 |
Correct |
678 ms |
18800 KB |
Output is correct |
7 |
Correct |
790 ms |
18704 KB |
Output is correct |
8 |
Correct |
788 ms |
18944 KB |
Output is correct |
9 |
Correct |
585 ms |
19304 KB |
Output is correct |
10 |
Correct |
706 ms |
18876 KB |
Output is correct |
11 |
Correct |
791 ms |
18820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
460 KB |
Output is correct |
2 |
Correct |
2091 ms |
99904 KB |
Output is correct |
3 |
Correct |
2754 ms |
103952 KB |
Output is correct |
4 |
Correct |
1623 ms |
115444 KB |
Output is correct |
5 |
Correct |
2400 ms |
152444 KB |
Output is correct |
6 |
Correct |
3042 ms |
123236 KB |
Output is correct |
7 |
Correct |
2386 ms |
43012 KB |
Output is correct |
8 |
Correct |
1488 ms |
42632 KB |
Output is correct |
9 |
Correct |
1506 ms |
47520 KB |
Output is correct |
10 |
Correct |
2116 ms |
44244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
716 KB |
Output is correct |
2 |
Correct |
748 ms |
18852 KB |
Output is correct |
3 |
Correct |
800 ms |
18940 KB |
Output is correct |
4 |
Correct |
762 ms |
18952 KB |
Output is correct |
5 |
Correct |
593 ms |
19152 KB |
Output is correct |
6 |
Correct |
678 ms |
18800 KB |
Output is correct |
7 |
Correct |
790 ms |
18704 KB |
Output is correct |
8 |
Correct |
788 ms |
18944 KB |
Output is correct |
9 |
Correct |
585 ms |
19304 KB |
Output is correct |
10 |
Correct |
706 ms |
18876 KB |
Output is correct |
11 |
Correct |
791 ms |
18820 KB |
Output is correct |
12 |
Correct |
3 ms |
460 KB |
Output is correct |
13 |
Correct |
2091 ms |
99904 KB |
Output is correct |
14 |
Correct |
2754 ms |
103952 KB |
Output is correct |
15 |
Correct |
1623 ms |
115444 KB |
Output is correct |
16 |
Correct |
2400 ms |
152444 KB |
Output is correct |
17 |
Correct |
3042 ms |
123236 KB |
Output is correct |
18 |
Correct |
2386 ms |
43012 KB |
Output is correct |
19 |
Correct |
1488 ms |
42632 KB |
Output is correct |
20 |
Correct |
1506 ms |
47520 KB |
Output is correct |
21 |
Correct |
2116 ms |
44244 KB |
Output is correct |
22 |
Correct |
3088 ms |
111088 KB |
Output is correct |
23 |
Correct |
3233 ms |
131408 KB |
Output is correct |
24 |
Correct |
3163 ms |
116720 KB |
Output is correct |
25 |
Correct |
3492 ms |
119512 KB |
Output is correct |
26 |
Correct |
4111 ms |
111764 KB |
Output is correct |
27 |
Correct |
2471 ms |
151232 KB |
Output is correct |
28 |
Correct |
2462 ms |
105656 KB |
Output is correct |
29 |
Correct |
4363 ms |
105368 KB |
Output is correct |
30 |
Correct |
4460 ms |
104680 KB |
Output is correct |
31 |
Correct |
4380 ms |
105400 KB |
Output is correct |
32 |
Correct |
1130 ms |
50620 KB |
Output is correct |
33 |
Correct |
1462 ms |
45280 KB |
Output is correct |
34 |
Correct |
1806 ms |
40548 KB |
Output is correct |
35 |
Correct |
1921 ms |
40420 KB |
Output is correct |
36 |
Correct |
1934 ms |
41308 KB |
Output is correct |
37 |
Correct |
1953 ms |
41220 KB |
Output is correct |