QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#232836 | #7103. Red Black Tree | neal2022 | AC ✓ | 890ms | 26212kb | C++23 | 4.0kb | 2023-10-30 23:31:06 | 2023-10-30 23:31:07 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct HeavyLight {
int root = 0, n = 0;
vector<int> parent, deep, hson, top, sz, dfn;
HeavyLight(vector<vector<int>> &g, int _root)
: root(_root), n(int(g.size())), parent(n), deep(n), hson(n, -1), top(n), sz(n), dfn(n, -1) {
int cur = 0;
function<int(int, int, int)> dfs = [&](int node, int fa, int dep) {
deep[node] = dep, sz[node] = 1, parent[node] = fa;
for (auto &ne : g[node]) {
if (ne == fa) continue;
sz[node] += dfs(ne, node, dep + 1);
if (hson[node] == -1 || sz[ne] > sz[hson[node]]) hson[node] = ne;
}
return sz[node];
};
function<void(int, int)> dfs2 = [&](int node, int t) {
top[node] = t, dfn[node] = cur++;
if (hson[node] == -1) return;
dfs2(hson[node], t);
for (auto &ne : g[node]) {
if (ne == parent[node] || ne == hson[node]) continue;
dfs2(ne, ne);
}
};
dfs(root, -1, 0), dfs2(root, root);
}
int lca(int x, int y) const {
while (top[x] != top[y]) {
if (deep[top[x]] < deep[top[y]]) swap(x, y);
x = parent[top[x]];
}
return deep[x] < deep[y] ? x : y;
}
vector<array<int, 2>> get_dfn_path(int x, int y) const {
array<vector<array<int, 2>>, 2> path;
bool front = true;
while (top[x] != top[y]) {
if (deep[top[x]] > deep[top[y]]) swap(x, y), front = !front;
path[front].push_back({dfn[top[y]], dfn[y] + 1});
y = parent[top[y]];
}
if (deep[x] > deep[y]) swap(x, y), front = !front;
path[front].push_back({dfn[x], dfn[y] + 1});
reverse(path[1].begin(), path[1].end());
for (const auto &[left, right] : path[1]) path[0].push_back({right, left});
return path[0];
}
// Node query_seg(int u, int v, const SegTree &seg) const {
// auto node = Node();
// for (const auto &[left, right] : get_dfn_path(u, v)) {
// if (left > right) {
// node = pull(node, rev(seg.query(right, left)));
// } else {
// node = pull(node, seg.query(left, right));
// }
// }
// return node;
// }
};
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int T;
cin >> T;
while (T--) {
int n, m, q;
cin >> n >> m >> q;
vector<int> is_red(n);
for (int i = 0, x; i < m; i++) {
cin >> x, x--, is_red[x] = 1;
}
vector<vector<array<int, 2>>> g(n);
vector<vector<int>> g_for_hld(n);
for (int i = 0, u, v, w; i < n - 1; i++) {
cin >> u >> v >> w, u--, v--;
g[u].push_back({v, w}), g[v].push_back({u, w});
g_for_hld[u].push_back(v), g_for_hld[v].push_back(u);
}
vector<ll> cost(n);
vector<int> last_red(n);
function<void(int, int)> get_cost = [&](int node, int fa) {
for (auto &[ne, w] : g[node]) {
if (ne == fa) continue;
if (!is_red[ne]) {
cost[ne] = cost[node] + w;
last_red[ne] = last_red[node];
} else {
last_red[ne] = ne;
}
get_cost(ne, node);
}
};
get_cost(0, -1);
auto hld = HeavyLight(g_for_hld, 0);
while (q--) {
int k;
cin >> k;
vector<array<ll, 2>> a(k);
for (auto &[c, x] : a) {
cin >> x, x--;
c = cost[x];
}
sort(a.begin(), a.end(), greater<>());
if (a.size() == 1) {
cout << "0\n";
continue;
}
ll res = a[1][0];
ll node = -1, top = -1;
for (int i = 0; i < k; i++) {
if (node == -1) {
node = a[i][1];
top = last_red[a[i][1]];
} else if (last_red[a[i][1]] != top) {
break;
} else {
node = hld.lca(int(node), int(a[i][1]));
}
const auto minus = cost[node];
auto cur = a[0][0] - minus;
if (i != k - 1) {
cur = max(cur, a[i + 1][0]);
}
res = min(res, cur);
}
cout << res << "\n";
}
}
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3452kb
input:
2 12 2 4 1 9 1 2 1 2 3 4 3 4 3 3 5 2 2 6 2 6 7 1 6 8 2 2 9 5 9 10 2 9 11 3 1 12 10 3 3 7 8 4 4 5 7 8 4 7 8 10 11 3 4 5 12 3 2 3 1 2 1 2 1 1 3 1 1 1 2 1 2 3 1 2 3
output:
4 5 3 8 0 0 0
result:
ok 7 lines
Test #2:
score: 0
Accepted
time: 890ms
memory: 26212kb
input:
522 26 1 3 1 1 4 276455 18 6 49344056 18 25 58172365 19 9 12014251 2 1 15079181 17 1 50011746 8 9 2413085 23 24 23767115 22 2 26151339 26 21 50183935 17 14 16892041 9 26 53389093 1 20 62299200 24 18 56114328 11 2 50160143 6 26 14430542 16 7 32574577 3 16 59227555 3 15 8795685 4 12 5801074 5 20 57457...
output:
148616264 148616264 0 319801028 319801028 255904892 317070839 1265145897 1265145897 1072765445 667742619 455103436 285643094 285643094 285643094 317919339 0 785245841 691421476 605409472 479058444 371688030 303203698 493383271 919185207 910180170 919185207 121535083 181713164 181713164 181713164 181...
result:
ok 577632 lines
Extra Test:
score: 0
Extra Test Passed