QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#500183 | #2993. 林克卡特树 | Nelofus | 100 ✓ | 1792ms | 43756kb | C++23 | 1.8kb | 2024-08-01 00:37:19 | 2024-08-01 00:37:19 |
Judging History
answer
// Code by Heratino & Nelofus
// ???????????
// ???????????
#include <bits/stdc++.h>
using i64 = long long;
//{{{
template<typename Ta, typename Tb>
inline void chkmax(Ta &a, const Tb &b) {if (a < b) a = b;}
template<typename Ta, typename Tb>
inline void chkmin(Ta &a, const Tb &b) {if (a > b) a = b;}
//}}}
constexpr int N = 3e5 + 10;
std::vector<std::array<int, 2>> G[N];
int n, k;
std::array<std::array<i64, 2>, 3> f[N];
std::array<i64, 2> operator + (std::array<i64, 2> a, i64 val) {return {a[0] + val, a[1]};}
std::array<i64, 2> operator + (std::array<i64, 2> a, std::array<i64, 2> b) {return {a[0] + b[0], a[1] + b[1]};}
inline void chkmax(std::array<i64, 2> &a, const std::array<i64, 2> &b) {
if (a[0] < b[0])
a = b;
else if (a[0] == b[0] && a[1] > b[1])
a = b;
}
void dfs(int u, int fa, i64 slope) {
f[u][0] = f[u][1] = f[u][2] = {0, 0};
for (const auto &[v, w] : G[u]) {
if (v == fa)
continue;
dfs(v, u, slope);
chkmax(f[u][2], f[u][2] + f[v][0]);
chkmax(f[u][2], f[u][1] + f[v][1] + (std::array<i64, 2>){w + slope, 1});
chkmax(f[u][1], f[u][1] + f[v][0]);
chkmax(f[u][1], f[u][0] + f[v][1] + w);
f[u][0] = f[u][0] + f[v][0];
}
chkmax(f[u][0], f[u][1] + (std::array<i64, 2>){slope, 1});
chkmax(f[u][0], f[u][2]);
}
bool check(i64 x) {
dfs(1, 0, x);
return f[1][0][1] <= k;
}
int main() {
#ifdef HeratinoNelofus
freopen("input.txt", "r", stdin);
#endif
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> n >> k;
k++;
for (int i = 1; i < n; i++) {
int u, v, w;
std::cin >> u >> v >> w;
G[u].push_back({v, w});
G[v].push_back({u, w});
}
i64 l = -1e9 - 10, r = 1e9 + 10;
while (l < r) {
i64 mid = l + r >> 1;
if (check(mid))
l = mid + 1;
else
r = mid;
}
i64 slope = l - 1;
check(slope);
std::cout << f[1][0][0] - k * slope << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Pretests
Final Tests
Test #1:
score: 5
Accepted
time: 1022ms
memory: 30132kb
input:
200000 0 65350 50045 831081 197523 52910 450640 102223 58430 595075 145187 41777 -548397 119538 69164 -976961 42852 63503 -590523 155411 169713 -523260 156688 13869 -63012 199944 82780 21461 93164 20930 -202990 2189 180064 931389 89237 187565 277289 156421 46988 774727 29142 19573 -953773 44537 1258...
output:
119876064
result:
ok single line: '119876064'
Test #2:
score: 5
Accepted
time: 1657ms
memory: 41692kb
input:
300000 0 197640 17080 162783 269725 187014 -181947 240689 261756 129022 226971 264281 338196 2415 179116 -871598 103266 44170 -454670 63425 143632 660611 205510 137031 -575054 248763 191913 233536 106763 213425 369236 197546 277600 -933947 219363 134954 807154 156605 218354 -516243 23354 181485 -803...
output:
187647882
result:
ok single line: '187647882'
Test #3:
score: 5
Accepted
time: 712ms
memory: 28384kb
input:
200000 1 181791 162820 711757 127748 11452 -795032 80860 71793 525612 195881 135260 -126472 183820 157817 665318 165220 20364 400920 187386 108213 -236737 133696 28527 280508 26024 19170 59816 18044 95464 497705 96297 191435 -191786 130752 20531 -683234 94787 31667 441623 47892 175810 -734631 146477...
output:
133041435
result:
ok single line: '133041435'
Test #4:
score: 5
Accepted
time: 1505ms
memory: 39680kb
input:
300000 1 188256 180568 -883916 297662 188433 62579 72882 3060 802792 173772 156239 -685924 89475 97850 109283 189977 238977 668196 147681 137368 287673 130735 126820 328423 163022 197254 404113 103379 245688 273971 84409 142542 121838 279404 107017 349027 53002 42234 150702 38970 130301 -657900 2262...
output:
243973784
result:
ok single line: '243973784'
Test #5:
score: 5
Accepted
time: 898ms
memory: 27264kb
input:
200000 2 169901 12952 -717273 167468 57457 57238 112530 187631 720251 140886 80961 -211382 169996 116263 747467 133399 190844 -317325 37450 39059 -649790 189065 39630 28609 123082 110715 -212169 27668 161879 -8632 192678 126317 566068 15559 23831 -820002 147769 67130 -565425 96285 77276 11875 137112...
output:
272549445
result:
ok single line: '272549445'
Test #6:
score: 5
Accepted
time: 813ms
memory: 29304kb
input:
200000 2 12078 169076 -942765 44175 99956 231323 133198 8579 171890 18545 179563 -85659 99402 116954 -31337 31702 166261 441837 31558 170873 531500 176395 135683 124560 161927 102775 -976865 35809 136934 299229 191169 34738 -533879 185462 176939 -750013 43633 41368 -926589 71922 149691 -137419 13463...
output:
142193848
result:
ok single line: '142193848'
Test #7:
score: 5
Accepted
time: 1554ms
memory: 41296kb
input:
300000 2 220743 272654 5034 119645 261362 707980 258300 182256 -670779 182916 190543 -319978 153462 244137 -863595 249563 134557 -982616 223363 22292 61412 47052 11723 -305744 91646 202184 -303558 272326 24578 -647471 48668 168529 233438 30267 295657 -410001 213986 81526 -386001 276522 87775 969181 ...
output:
373502704
result:
ok single line: '373502704'
Test #8:
score: 5
Accepted
time: 916ms
memory: 27676kb
input:
200000 100 16537 61788 402424 161077 77028 -919110 117310 153524 -290999 183518 111544 -117636 73244 165828 883254 117689 166229 -406860 102026 40518 78656 159440 72889 -675522 106123 23028 732235 86129 134361 340255 56567 159088 58610 125473 59177 -372900 158520 163684 -349257 108476 165042 875739 ...
output:
1927078979
result:
ok single line: '1927078979'
Test #9:
score: 5
Accepted
time: 898ms
memory: 29816kb
input:
200000 100 159645 190327 -790296 181809 12423 601994 123183 83267 -49442 134128 130575 -143436 164650 149794 429016 47721 105379 296109 18240 27184 762687 38772 128862 596455 113039 3632 -307682 158647 61354 301670 63054 125621 -331135 12548 92826 570965 189760 87332 500426 74656 19969 228158 131009...
output:
1928973825
result:
ok single line: '1928973825'
Test #10:
score: 5
Accepted
time: 1566ms
memory: 38536kb
input:
300000 100 131574 241226 825936 43551 161469 -432167 92729 242024 -852728 201397 89958 -598808 212544 177234 33151 160560 166450 857 270273 200219 -436713 287533 21869 -586096 206427 267598 48749 151663 185902 -336130 124834 57033 -435251 14650 115941 564478 240297 64983 714036 56725 256596 574766 9...
output:
2067331570
result:
ok single line: '2067331570'
Test #11:
score: 5
Accepted
time: 1461ms
memory: 41272kb
input:
300000 100 263059 214437 -956970 266221 5485 -984258 181157 191851 337012 166949 93678 423724 270903 216008 -533514 178527 242172 63569 190242 167484 -620162 74008 278321 -654753 170660 3765 353481 181023 8801 982726 155475 162086 -824228 237659 285329 799133 222437 90885 822446 144544 187010 88694 ...
output:
2084990799
result:
ok single line: '2084990799'
Test #12:
score: 5
Accepted
time: 1347ms
memory: 39612kb
input:
300000 100 25419 209149 -500631 108405 59781 442111 293496 81726 -921827 135966 256780 -174547 209773 193974 -358614 21666 114744 -106858 221058 70979 -209083 295305 246008 -585729 148509 157116 -640851 143114 200242 651047 75896 68199 -893175 297649 209143 422060 213992 183699 199818 201195 275824 ...
output:
1890469382
result:
ok single line: '1890469382'
Test #13:
score: 5
Accepted
time: 742ms
memory: 31956kb
input:
200000 130406 40594 19720 -623122 23706 44660 -874727 198946 155951 446918 18573 173446 -730875 620 170942 760623 90071 160815 -451054 44927 119046 601497 152317 43323 547496 13277 66904 -129002 39821 13557 -337519 97682 195872 473610 135525 112760 -323544 191848 22168 950602 25024 195672 -479997 98...
output:
44325098163
result:
ok single line: '44325098163'
Test #14:
score: 5
Accepted
time: 712ms
memory: 26676kb
input:
200000 112025 19434 26866 -522901 41544 82125 340019 42503 188348 -283235 47014 76883 -752315 192433 97139 -364401 142520 45516 773937 119246 43968 -87291 24207 146679 868137 80687 82213 -813080 36329 84717 -568093 122342 155905 -124629 166598 28449 925294 36746 7420 -767398 186177 79549 -558811 123...
output:
45620696034
result:
ok single line: '45620696034'
Test #15:
score: 5
Accepted
time: 661ms
memory: 30312kb
input:
200000 159073 90997 155660 688588 122016 194855 364001 108373 157814 921909 126532 153527 89711 77452 13721 434419 168396 14935 -742122 25592 91528 -156210 171969 92303 830841 174160 64741 993375 90321 26349 -426120 108013 164044 720212 150049 110076 525431 172048 119753 887134 145459 147158 78386 1...
output:
32509638528
result:
ok single line: '32509638528'
Test #16:
score: 5
Accepted
time: 617ms
memory: 28328kb
input:
200000 138730 34756 87461 -636823 118637 99255 -915828 126441 146865 191707 142168 127467 -20126 81775 88255 -222870 165937 84373 716993 104552 144925 -279669 157496 176688 691692 91940 55387 996047 26473 100735 402521 180263 64826 -845891 193409 27113 -260656 82076 68265 -505721 47211 61310 330882 ...
output:
41824057737
result:
ok single line: '41824057737'
Test #17:
score: 5
Accepted
time: 1507ms
memory: 37924kb
input:
300000 129178 23041 100501 -373992 227874 165730 605092 67462 240962 -713691 109035 104054 -362417 187831 102896 554829 335 127284 -810036 141401 282130 750195 228816 71494 -696039 130283 15637 771038 119508 43490 520169 42895 277561 504582 103769 114888 -343346 73436 73051 491763 196506 25657 13280...
output:
68210040353
result:
ok single line: '68210040353'
Test #18:
score: 5
Accepted
time: 1792ms
memory: 43756kb
input:
300000 152830 264154 103322 476154 41997 134920 -402116 163045 84775 591957 109208 9792 -983414 23856 44103 -745743 90231 82597 -569317 170372 74011 -945586 90507 199076 -328824 134113 2043 -636000 240437 131789 555722 236029 167549 -799563 273537 278524 287973 7746 84943 707424 113967 107808 -81031...
output:
71122489923
result:
ok single line: '71122489923'
Test #19:
score: 5
Accepted
time: 1702ms
memory: 39640kb
input:
300000 220606 73646 165467 211934 287560 202214 503555 133070 247406 930057 135894 259547 953228 71803 157101 -918434 222521 210059 -833619 207233 164303 424927 28849 254325 -553475 115615 159949 44857 71463 13068 -109967 220595 75134 902520 266060 244008 632129 288006 100470 602933 150032 239332 53...
output:
57882988995
result:
ok single line: '57882988995'
Test #20:
score: 5
Accepted
time: 1334ms
memory: 37140kb
input:
300000 177288 96606 159646 -843008 6102 77679 -290722 162415 126260 610363 143376 146328 -745143 64929 265269 605571 183004 109222 754682 59516 295312 -675501 17670 184822 740404 203705 225595 -824744 249255 59274 761581 299854 210063 -551417 231581 65698 808182 181562 142802 143491 146718 135568 -2...
output:
68962524975
result:
ok single line: '68962524975'