QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#604180 | #4159. 消耗战 | lwm7708 | 100 ✓ | 678ms | 53512kb | C++17 | 4.4kb | 2024-10-02 00:34:37 | 2024-10-02 00:34:38 |
Judging History
answer
#include <algorithm>
#include <array>
#include <cstdint>
#include <ios>
#include <iostream>
#include <utility>
#include <vector>
template <typename F>
class y_combinator {
private:
F f;
public:
explicit y_combinator(F&& f) : f(f) {}
template <typename... Args>
decltype(auto) operator()(Args&&... args) const {
return f(*this, std::forward<Args>(args)...);
}
};
template <typename F>
y_combinator(F) -> y_combinator<F>;
void solve() {
using edge_t = std::array<std::int32_t, 2>;
std::int32_t n;
std::cin >> n;
std::vector<std::vector<edge_t>> adj(n);
std::int64_t mx = 0;
for (std::int32_t i = 0; i < n - 1; ++i) {
std::int32_t u;
std::int32_t v;
std::int32_t w;
std::cin >> u >> v >> w;
--u;
--v;
adj[u].push_back(edge_t({v, w}));
adj[v].push_back(edge_t({u, w}));
mx += w;
}
std::int32_t m;
std::cin >> m;
std::vector<std::int32_t> deps(n);
std::vector<std::int32_t> dfn(n);
std::vector<std::int64_t> dp(n);
std::vector<bool> in(n);
const std::int32_t lvls = 32 - __builtin_clz(n - 1);
std::vector<std::int64_t> mns(n, mx + 1);
std::int32_t tm = 0;
std::vector<std::vector<std::int32_t>> v_adj(n);
std::vector ancs(lvls, std::vector(n, std::int32_t(-1)));
y_combinator(
[&](auto self, std::int32_t node, std::int32_t par) -> void {
ancs[0][node] = par;
deps[node] = par != -1 ? deps[par] + 1 : 0;
dfn[node] = tm;
++tm;
for (const auto& [nbr, w] : adj[node]) {
if (nbr != par) {
mns[nbr] = std::min<std::int64_t>(mns[node], w);
self(nbr, node);
}
}
}
)(0, -1);
for (std::int32_t i = 1; i < lvls; ++i) {
for (std::int32_t j = 0; j < n; ++j) {
const std::int32_t anc = ancs[i - 1][j];
ancs[i][j] = anc != -1 ? ancs[i - 1][anc] : -1;
}
}
for (std::int32_t i = 0; i < m; ++i) {
std::int32_t k;
std::cin >> k;
std::vector<std::int32_t> h(k + 1);
for (std::int32_t j = 0; j < k; ++j) {
std::cin >> h[j];
--h[j];
}
for (std::int32_t j = 0; j < k; ++j) {
in[h[j]] = true;
}
h.reserve((k + 1) * 2 - 1);
const auto cmp = [&](std::int32_t node_1, std::int32_t node_2) -> bool {
return dfn[node_1] < dfn[node_2];
};
const auto lca = [&](std::int32_t node_1, std::int32_t node_2) -> std::int32_t {
for (std::int32_t i = lvls - 1; i >= 0; --i) {
if (deps[node_1] - deps[node_2] >= 1 << i) {
node_1 = ancs[i][node_1];
}
if (deps[node_2] - deps[node_1] >= 1 << i) {
node_2 = ancs[i][node_2];
}
}
if (node_1 == node_2) {
return node_1;
}
for (std::int32_t i = lvls - 1; i >= 0; --i) {
if (ancs[i][node_1] != ancs[i][node_2]) {
node_1 = ancs[i][node_1];
node_2 = ancs[i][node_2];
}
}
return ancs[0][node_1];
};
std::sort(std::begin(h), std::end(h), cmp);
for (std::int32_t j = 0; j < k; ++j) {
h.push_back(lca(h[j], h[j + 1]));
}
std::sort(std::begin(h), std::end(h), cmp);
h.erase(std::unique(std::begin(h), std::end(h)), std::end(h));
const std::int32_t sz = std::size(h);
for (std::int32_t j = 1; j < sz; ++j) {
v_adj[lca(h[j - 1], h[j])].push_back(h[j]);
}
for (std::int32_t j = sz - 1; j >= 0; --j) {
dp[h[j]] = mns[h[j]];
if (!in[h[j]]) {
std::int64_t c_cst = 0;
for (auto x : v_adj[h[j]]) {
c_cst += dp[x];
}
dp[h[j]] = std::min(dp[h[j]], c_cst);
}
}
std::cout << dp[0] << '\n';
for (auto x : h) {
in[x] = false;
v_adj[x].clear();
}
}
}
int main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Pretests
Final Tests
Test #1:
score: 10
Accepted
time: 1ms
memory: 3632kb
input:
100 1 2 3872 2 3 2287 2 4 2427 2 5 7759 1 6 2695 5 7 9472 6 8 1299 1 9 4637 8 10 2386 4 11 4623 10 12 5610 5 13 891 2 14 143 5 15 3363 14 16 3490 1 17 6588 14 18 2877 13 19 68 3 20 1702 17 21 700 21 22 5439 11 23 7487 22 24 2996 13 25 8201 23 26 7510 23 27 2657 26 28 5831 5 29 9863 16 30 8873 2 31 9...
output:
7267 3444 3872 727 6191 10128 7267 6191 3872 3747 5992 3011 143 2427 3872 10508 4892 5171 5171 569 6567 6567 3498 5171 6567 10828 5491 3872 3872 4572 5740 9650 8988 4192 3872 3039 1997 700 10460 2190 6887 5171 1139 9398 3726 8290 1087 6060 4693 6567 10460 7267 874 5171 4572 6046 6887 3872 3363 4161 ...
result:
ok 100 lines
Test #2:
score: 10
Accepted
time: 79ms
memory: 3776kb
input:
1000 1 2 537 1 3 2059 1 4 8586 3 5 5724 3 6 8019 1 7 6286 1 8 3653 2 9 3684 4 10 6654 4 11 4488 4 12 7127 11 13 2066 11 14 7876 5 15 6664 8 16 8598 14 17 5744 15 18 3807 1 19 1351 19 20 662 13 21 3559 13 22 7508 2 23 3181 7 24 2384 21 25 3849 13 26 618 11 27 5810 24 28 4856 7 29 6843 21 30 4627 5 31...
output:
2728 2910 3824 2431 4459 7429 4827 1519 2431 3166 4339 4016 3042 5086 4125 2803 1502 2587 1920 1567 769 684 999 2575 2909 1677 1756 1155 792 2066 4020 4506 2222 363 1280 3672 3325 2728 662 1280 900 379 2106 4016 2129 4634 707 2721 2929 1849 4315 1280 848 2685 2059 2603 1406 618 4432 2544 1908 2596 1...
result:
ok 166666 lines
Test #3:
score: 10
Accepted
time: 90ms
memory: 3980kb
input:
1000 1 2 2564 1 3 1499 1 4 9826 2 5 5515 1 6 8014 2 7 638 1 8 3916 6 9 1976 3 10 8803 5 11 5933 10 12 3738 11 13 3448 10 14 9249 10 15 867 3 16 1237 8 17 3322 5 18 1794 1 19 7298 9 20 9216 9 21 3952 8 22 5296 1 23 9836 18 24 9160 2 25 6225 7 26 4424 5 27 3818 9 28 2152 4 29 6383 27 30 1505 13 31 413...
output:
2564 113 2485 501 2799 2757 5691 1499 2211 3844 4190 2901 1530 726 2564 5229 5536 2813 2743 857 2776 638 1587 2169 3519 10299 3143 1195 1961 2564 2137 2564 5275 2564 2711 1107 1555 8301 829 1772 6884 3671 2137 3770 2564 2564 1680 3801 313 3113 1496 2933 3671 2564 1910 5554 4063 2477 3083 4063 3221 3...
result:
ok 250000 lines
Test #4:
score: 10
Accepted
time: 399ms
memory: 48768kb
input:
250000 1 2 2268 1 3 3908 3 4 8278 4 5 4693 4 6 3931 5 7 2207 4 8 9819 7 9 2678 1 10 2206 3 11 433 2 12 7081 4 13 179 2 14 3770 13 15 1328 4 16 1167 10 17 3542 2 18 6968 9 19 5150 12 20 692 14 21 6096 4 22 4619 22 23 3564 23 24 801 6 25 4668 25 26 3824 18 27 3190 8 28 3064 17 29 1159 20 30 4957 23 31...
output:
11933 20150 13672 10226 9037 12030 14820 12212 11170 8490 14559 12157 12598 20332 12625 19691 13141 11319 13118 10386 11224 12343 10561 13672 13006 8748 13672 13672 11381 9626 8403 13672 10155 19619 13068 11782 11067 15861 9402 13672 13672 12118 15496 9158 12625 11842 19339 11284 8710 13653 9368 136...
result:
ok 5000 lines
Test #5:
score: 10
Accepted
time: 371ms
memory: 48744kb
input:
250000 1 2 6901 1 3 6877 3 4 4119 4 5 8363 1 6 9998 3 7 6589 3 8 9451 4 9 2328 1 10 1612 7 11 6154 8 12 1583 3 13 6664 7 14 924 8 15 9077 15 16 9191 11 17 1997 7 18 1879 13 19 9237 2 20 1222 19 21 6336 7 22 8576 12 23 8169 16 24 4638 1 25 8765 19 26 4484 11 27 6850 9 28 2985 20 29 657 29 30 4817 9 3...
output:
22470 16304 14462 11066 13700 13826 15177 11114 8911 14623 12886 16744 12731 8553 19666 13797 14211 12747 10918 11989 12280 10424 18039 8931 15555 17003 12569 17133 14638 11317 11180 10622 14142 10475 9214 22966 8192 11779 13180 10206 12513 10714 14555 13952 16398 18370 19955 20313 17191 10625 11110...
result:
ok 16666 lines
Test #6:
score: 10
Accepted
time: 678ms
memory: 47188kb
input:
250000 38777 108274 87589 108274 160854 77640 160854 60552 78805 60552 200039 58017 200039 84497 69330 84497 124673 34582 124673 58879 30083 58879 212671 64495 212671 36027 38890 36027 101232 26550 101232 191826 77803 191826 86159 43383 86159 10737 29218 10737 169665 79625 169665 215204 9275 215204 ...
output:
694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 437 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 694 ...
result:
ok 9891 lines
Test #7:
score: 10
Accepted
time: 586ms
memory: 47124kb
input:
250000 28712 5893 24269 5893 234660 76106 234660 68804 34449 68804 92158 10190 92158 30430 94629 30430 147323 97917 147323 3900 76522 3900 163904 33184 163904 230315 6988 230315 84092 49596 84092 228686 22214 228686 34943 64054 34943 156088 9160 156088 40788 8540 40788 214979 48213 214979 90403 7986...
output:
531 531 531 531 531 531 531 531 531 361 531 531 531 531 531 531 531 531 531 531 531 481 531 531 531 531 531 531 438 531 531 531 531 531 531 531 531 531 531 531 531 531 531 531 531 531 531 531 531 531 531 531 531 531 531 531 531 531 531 531 531 531 531 531 531 531 531 531 531 531 531 531 531 531 531 ...
result:
ok 62334 lines
Test #8:
score: 10
Accepted
time: 348ms
memory: 48684kb
input:
250000 1 2 3705 2 3 3084 3 4 8957 3 5 5735 1 6 1597 6 7 3523 5 8 1769 7 9 8021 8 10 8743 5 11 3445 9 12 2611 4 13 5966 11 14 4466 4 15 830 7 16 6111 7 17 9587 11 18 2402 2 19 178 15 20 5660 16 21 5340 9 22 4499 7 23 65 14 24 1874 16 25 3921 17 26 1203 22 27 8893 27 28 9215 4 29 4481 1 30 5282 14 31 ...
output:
5913 5302 4681 4647 6272 6725 8785 4681 3437 4347 4681 4851 4681 4184 4681 5302 4849 6116 1930 5393 5616 5959 4840 5385 4681 4681 4681 4681 5381 4853 5033 4049 4082 5302 5497 4681 2817 4332 6277 8364 3349 7342 4737 4176 4859 5854 4646 5912 6167 4859 5555 7050 5097 4711 8206 9405 4112 5075 5521 4681 ...
result:
ok 50000 lines
Test #9:
score: 10
Accepted
time: 343ms
memory: 53512kb
input:
250000 1 156736 100000 156736 146447 100000 146447 238841 100000 1 91578 100000 1 181978 100000 1 248425 100000 1 134840 100000 134840 230020 100000 230020 245935 100000 1 218596 100000 218596 139329 100000 1 130973 100000 130973 155892 100000 1 44564 100000 44564 40236 100000 1 123733 100000 123733...
output:
11877300000 12066100000 1128400000 3397000000 78000000 57600000 231800000 201200000 51600000 7300000 100000 600000 300000 100000
result:
ok 14 lines
Test #10:
score: 10
Accepted
time: 0ms
memory: 3604kb
input:
10 1 2 6432 2 3 9694 3 4 9714 4 5 8890 4 6 9170 1 7 6361 1 8 1627 2 9 9824 7 10 9947 5 3 4 7 2 5 7 2 10 6 3 5 3 7 4 8 10 3 3 2 10 2 6 8
output:
12793 12793 14420 12793 8059
result:
ok 5 lines