QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#121495 | #2065. Cyclic Distance | black_trees | WA | 63ms | 21940kb | C++20 | 1.6kb | 2023-07-08 12:58:26 | 2023-07-08 12:58:29 |
Judging History
answer
// author : black_trees
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <utility>
#include <iostream>
#include <algorithm>
#define endl '\n'
using namespace std;
using i64 = long long;
const int si = 2e5 + 10;
int n, k;
int l[si], r[si];
std::vector<std::pair<int, int>> G[si];
std::priority_queue<int> pq_l[si], pq_r[si];
void Merge(std::priority_queue<int> &a,
std::priority_queue<int> &b, int &x, int &y) {
if(a.size() < b.size()) swap(a, b), swap(x, y);
for(; !b.empty(); b.pop()) a.push(b.top() + y - x);
}
void dfs(int u, int fa) {
pq_l[u].push(0);
for(auto &[v, w]: G[u]) {
if(v == fa) continue;
dfs(v, u), l[v] -= w; r[v] -= w;
if(k & 1 && !pq_r[v].empty()){
int t = pq_r[v].top();
pq_r[v].pop(), pq_r[v].push(t + w);
}
Merge(pq_l[u], pq_l[v], l[u], l[v]);
for(; pq_l[u].size() > (k / 2); pq_l[u].pop())
pq_r[u].push(-l[u] - pq_l[u].top() - r[u]);
Merge(pq_r[u], pq_r[v], r[u], r[v]);
}
}
int main() {
cin.tie(0) -> sync_with_stdio(false);
cin.exceptions(cin.failbit | cin.badbit);
int T; cin >> T;
while(T--) {
cin >> n >> k;
for(int i = 1; i <= n; ++i)
G[i].clear(), l[i] = r[i] = 0;
for(int i = 1; i <= n - 1; ++i) {
int u, v, w;
cin >> u >> v >> w;
G[u].push_back(make_pair(v, w));
G[v].push_back(make_pair(u, w));
}
dfs(1, 0);
int ans = 0, i = 0;
for(i = 0; !pq_l[1].empty(); ++i)
ans -= pq_l[1].top() + l[1], pq_l[1].pop();
for(; !pq_r[1].empty(); ++i) {
if(i < k)
ans += pq_r[1].top() + r[1];
pq_r[1].pop();
}
cout << ans * 2 << endl;
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 20928kb
input:
1 5 3 1 2 4 1 3 1 1 4 8 4 5 9
output:
44
result:
ok single line: '44'
Test #2:
score: 0
Accepted
time: 28ms
memory: 21940kb
input:
57206 3 2 1 2 574927 2 3 406566 2 2 1 2 308806 4 3 1 2 312588 2 3 500141 2 4 53602 3 3 1 2 797183 2 3 944061 5 3 1 2 624010 2 3 488613 3 4 734514 4 5 497493 5 4 1 2 540278 2 3 488655 2 4 228989 2 5 653896 2 2 1 2 569117 4 2 1 2 821764 2 3 499471 1 4 549060 2 2 1 2 991159 2 2 1 2 482941 5 4 1 2 30462...
output:
1962986 617612 1732662 3482488 4689260 3823636 1138234 3740590 1982318 965882 3418504 5026562 1623414 1885106 1952142 3050630 1691896 3102076 2380932 3076270 5697196 7258020 879020 2500212 3613854 1358950 1182198 2273662 2331560 1681964 8917452 2373066 3163042 3104226 3642898 3162310 5058442 3669186...
result:
ok 57206 lines
Test #3:
score: 0
Accepted
time: 18ms
memory: 21480kb
input:
57087 3 3 1 2 34132 1 3 188096 2 2 1 2 996527 2 2 1 2 475736 5 3 1 2 329834 2 3 339687 1 4 954113 4 5 224354 2 2 1 2 641444 2 2 1 2 114059 5 3 1 2 635722 1 3 552627 1 4 721758 3 5 396156 4 3 1 2 655099 2 3 963393 1 4 953969 5 2 1 2 369719 1 3 22087 1 4 531252 3 5 449025 4 3 1 2 788498 1 3 220292 1 4...
output:
444456 1993054 951472 3695976 1282888 228118 4612526 5144922 2004728 3309502 2626844 3053048 3939444 3790784 2617770 38866 3033250 5707738 511666 403846 1923106 3331606 3447180 2329518 5656124 33582 2283312 3454982 110590 3125394 4517486 4522330 2352316 3966810 3463746 5181112 3089346 1260326 466418...
result:
ok 57087 lines
Test #4:
score: 0
Accepted
time: 30ms
memory: 21372kb
input:
33344 9 6 1 2 562996 1 3 312637 3 4 591016 1 5 811983 2 6 896220 3 7 854379 2 8 861166 1 9 672337 8 6 1 2 53530 1 3 712638 1 4 539356 1 5 179377 3 6 456495 2 7 730760 4 8 934379 3 3 1 2 87024 1 3 328551 3 3 1 2 664600 1 3 519786 5 4 1 2 374521 1 3 484148 2 4 501378 1 5 280691 10 3 1 2 676949 1 3 639...
output:
12876734 9717058 831150 2368772 4030518 7963678 2135868 739728 11584454 1670128 3432160 5573124 1293282 3608364 8574290 6242670 10860048 4726106 5661430 9713590 5160212 5958260 14418122 1913782 1393854 5129544 9369494 11601220 4751232 1623938 8259790 3591252 5112182 4761950 5284034 13000192 4895040 ...
result:
ok 33344 lines
Test #5:
score: 0
Accepted
time: 30ms
memory: 20704kb
input:
33337 8 2 1 2 22201 2 3 94167 2 4 978398 4 5 452870 5 6 59368 5 7 804913 7 8 977938 3 3 1 2 784938 1 3 333822 8 4 1 2 737256 2 3 276599 2 4 338826 2 5 260533 2 6 520885 1 7 971939 1 8 613926 8 2 1 2 405702 1 3 514900 2 4 861432 2 5 715573 2 6 269555 5 7 528278 6 8 537996 6 2 1 2 984398 2 3 629 1 4 3...
output:
6616572 2237520 7840176 4328906 4093536 11035562 2053254 17920138 4892406 11437574 7585262 5318412 12387008 1823170 5912732 2056136 4049368 3780958 3965658 1661392 3447138 7906552 12728830 12419926 4593330 817758 2300052 12252582 10429848 629344 8615656 8922918 3351270 6102888 3501718 11662020 15460...
result:
ok 33337 lines
Test #6:
score: 0
Accepted
time: 43ms
memory: 21184kb
input:
18261 6 6 1 2 401169 1 3 865631 3 4 470224 1 5 136374 3 6 696999 3 2 1 2 216465 2 3 99004 9 3 1 2 360514 1 3 110584 3 4 170236 1 5 969734 4 6 929592 3 7 907150 4 8 418707 4 9 357462 4 4 1 2 951855 2 3 70272 2 4 113663 17 9 1 2 352392 1 3 254146 2 4 362317 1 5 589664 3 6 284236 6 7 978987 2 8 122649 ...
output:
8603318 630938 6174592 2271580 32450770 9765552 9941290 17849770 6762442 9904414 59511294 4686354 5194544 44718814 20540916 7002622 29096312 1815140 9151006 20865960 2859444 7971376 15607738 16938982 9282678 15770664 10404176 2332096 3515930 50580870 9444474 7316680 3747306 14809566 32347198 5442322...
result:
ok 18261 lines
Test #7:
score: 0
Accepted
time: 43ms
memory: 20704kb
input:
18082 12 8 1 2 893078 2 3 422969 1 4 633414 4 5 744557 3 6 860147 3 7 385978 5 8 399366 3 9 431676 4 10 181291 9 11 486224 10 12 444565 13 12 1 2 449428 1 3 484947 3 4 581713 3 5 159778 3 6 337685 4 7 565917 4 8 136883 7 9 963963 9 10 457061 9 11 818966 4 12 588294 1 13 275051 11 8 1 2 742103 1 3 98...
output:
26178344 26647644 17726444 51096468 51024750 4318098 10947660 9534678 3065408 11342084 8694638 19155222 849062 7555504 8993018 3193064 4338758 519680 4516496 18892576 2929566 12021588 29857614 40051924 1688342 24734948 10330762 14820592 11122894 15774626 17385606 20569180 5715160 3895974 7010784 271...
result:
ok 18082 lines
Test #8:
score: 0
Accepted
time: 38ms
memory: 20640kb
input:
7777 27 19 1 2 930838 1 3 462030 1 4 982798 2 5 829904 3 6 593202 5 7 941278 5 8 694251 3 9 720130 4 10 604740 4 11 550251 5 12 409519 3 13 23594 12 14 54526 2 15 511926 1 16 48491 1 17 765416 12 18 819984 9 19 325056 7 20 175920 11 21 269086 16 22 641837 13 23 1737 21 24 948879 15 25 349036 3 26 13...
output:
71370146 30838976 80456148 32228866 5055546 25592662 95173582 13955400 17980860 19738002 78673788 101336458 125780830 1081414 105831712 11260058 85123024 74738088 91760570 127445888 56551920 26076342 50784456 43425188 9465296 64841258 21733114 12894954 66549458 57289112 46556192 46428776 79922806 15...
result:
ok 7777 lines
Test #9:
score: 0
Accepted
time: 49ms
memory: 21812kb
input:
3973 72 55 1 2 918907 2 3 400804 2 4 72269 2 5 254465 3 6 176834 4 7 487004 4 8 469111 5 9 299565 5 10 455772 8 11 575420 3 12 538035 7 13 501415 11 14 583573 1 15 879841 11 16 16749 16 17 48301 17 18 5050 6 19 739687 10 20 264146 19 21 95867 14 22 436314 18 23 109932 17 24 472782 5 25 809039 19 26 ...
output:
201634460 8298172 194453968 102456878 21539194 126399884 235528270 117959738 23543328 41025942 8304998 31545014 17344164 41189444 25956190 46294310 13019524 71670116 120980628 48791074 150100762 116919430 244037610 218464668 133368300 255723622 106123038 244888064 19329892 66580624 74085138 26108538...
result:
ok 3973 lines
Test #10:
score: 0
Accepted
time: 43ms
memory: 21572kb
input:
1977 164 159 1 2 789785 2 3 953798 2 4 694582 2 5 546152 1 6 977613 4 7 100774 1 8 699051 6 9 456494 4 10 736064 8 11 451475 2 12 212640 12 13 472011 2 14 473796 12 15 986991 8 16 723782 6 17 209086 2 18 619112 15 19 740890 19 20 114446 4 21 470217 7 22 718655 9 23 989557 14 24 575144 24 25 897325 1...
output:
728347768 385840768 77551442 592321810 379244468 70306600 40298184 752175314 115213140 20514164 76134366 99658306 453129018 233705740 297458016 274605942 332890648 11997344 319596032 85455912 55983850 11837114 356411436 56917200 180309026 69088440 113716684 159826434 571011208 528906534 606746358 46...
result:
ok 1977 lines
Test #11:
score: -100
Wrong Answer
time: 63ms
memory: 21732kb
input:
818 216 206 1 2 369713 2 3 421291 3 4 140720 3 5 453918 2 6 347245 3 7 292355 4 8 804550 2 9 511603 5 10 576941 6 11 79641 2 12 493352 6 13 192308 12 14 854864 4 15 144922 7 16 522578 7 17 532656 15 18 685489 2 19 809906 14 20 599938 20 21 527857 10 22 822574 4 23 885328 13 24 111539 8 25 292999 10 ...
output:
867774932 -1997972502 233491416 339174870 4380454 316249010 1649235398 150692978 859452362 1387824632 566645160 1671550174 374729794 38076864 256942076 89728496 111087726 819481720 353274342 158202878 -1787283314 659900622 594449324 108828820 220100714 806438160 288755872 450110446 1306416876 288016...
result:
wrong answer 2nd lines differ - expected: '2296994794', found: '-1997972502'