QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#636259 | #2993. 林克卡特树 | wzj33300 | 100 ✓ | 2981ms | 63156kb | C++23 | 5.7kb | 2024-10-12 23:05:38 | 2024-10-12 23:05:39 |
Judging History
answer
/**
* created : 12.10.2024 22:19:00
* author : wzj33300
*/
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include <algo/debug.h>
#else
#define debug(...) 42
#define assert(...) 42
#endif
using ll = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
using db = long double; // or double, if TL is tight
using str = string; // yay python!
// pairs
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;
#define mp make_pair
#define fi first
#define se second
// ^ lol this makes everything look weird but I'll try it
template <class T>
using vc = vector<T>;
template <class T, size_t SZ>
using AR = array<T, SZ>;
using vi = vc<int>;
using vb = vc<bool>;
using vl = vc<ll>;
using vd = vc<db>;
using vs = vc<str>;
using vpi = vc<pi>;
using vpl = vc<pl>;
using vpd = vc<pd>;
// vectors
// oops size(x), rbegin(x), rend(x) need C++17
#define sz(x) int((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) x.rbegin(), x.rend()
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define pb push_back
#define eb emplace_back
#define ft front()
#define bk back()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i < (n); ++i)
#define rep1n(i, n) for (int i = 1; i <= (n); ++i)
#define repr(i, n) for (int i = (n) - 1; i >= 0; --i)
#define rep_subset(t, s) \
for (ll t = (s); t >= 0; t = (t == 0 ? -1 : (t - 1) & (s)))
#define lb lower_bound
#define ub upper_bound
template <class T>
int lwb(vc<T> &a, const T &b) {
return int(lb(all(a), b) - bg(a));
}
template <class T>
int upb(vc<T> &a, const T &b) {
return int(ub(all(a), b) - bg(a));
}
// const int MOD = 998244353; // 1e9+7;
const int Big = 1e9; // not too close to INT_MAX
const ll BIG = 1e18; // not too close to LLONG_MAX
const db PI = acos((db)-1);
const int dx[4]{1, 0, -1, 0}, dy[4]{0, 1, 0, -1}; // for every grid problem!!
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;
int pct(int x) { return __builtin_popcount(x); }
int pct(u32 x) { return __builtin_popcount(x); }
int pct(ll x) { return __builtin_popcountll(x); }
int pct(u64 x) { return __builtin_popcountll(x); }
int popcnt_mod_2(int x) { return __builtin_parity(x); }
int popcnt_mod_2(u32 x) { return __builtin_parity(x); }
int popcnt_mod_2(ll x) { return __builtin_parityll(x); }
int popcnt_mod_2(u64 x) { return __builtin_parityll(x); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)
int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)
int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
template <typename T>
T floor(T a, T b) {
return a / b - (a % b && (a ^ b) < 0);
}
template <typename T>
T ceil(T x, T y) {
return floor(x + y - 1, y);
}
template <typename T>
T bmod(T x, T y) {
return x - y * floor(x, y);
}
template <typename T>
pair<T, T> divmod(T x, T y) {
T q = floor(x, y);
return {q, x - q * y};
}
template <class T>
bool ckmin(T &a, const T &b) {
return b < a ? a = b, 1 : 0;
} // set a = min(a,b)
template <class T>
bool ckmax(T &a, const T &b) {
return a < b ? a = b, 1 : 0;
} // set a = max(a,b)
template <class T, class U>
T fstTrue(T lo, T hi, U f) {
++hi;
assert(lo <= hi); // assuming f is increasing
while (lo < hi) { // find first index such that f is true
T mid = lo + (hi - lo) / 2;
f(mid) ? hi = mid : lo = mid + 1;
}
return lo;
}
template <class T, class U>
T lstTrue(T lo, T hi, U f) {
--lo;
assert(lo <= hi); // assuming f is decreasing
while (lo < hi) { // find first index such that f is true
T mid = lo + (hi - lo + 1) / 2;
f(mid) ? lo = mid : hi = mid - 1;
}
return lo;
}
// signed main() {
int main() {
// freopen(".in", "r",stdin);
// freopen(".out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
std::cin >> n >> m;
++m;
vc<vpi> e(n);
for (int i = 1; i < n; i++) {
int u, v, w;
std::cin >> u >> v >> w;
--u, --v;
e[u].eb(v, w), e[v].eb(u, w);
}
/*
wqs ?? k ???
*/
vc f(n, AR<pl, 3>{});
auto check = [&](ll add_val) {
vc(n, AR<pl, 3>{}).swap(f);
auto dfs = [&](auto &&dfs, int x, int fa) -> void {
auto &fu = f[x];
fu.fill({-BIG, -BIG});
fu[0] = {0, 0};
fu[1] = {add_val, 1};
for (auto [v, w] : e[x]) {
if (v == fa) continue;
dfs(dfs, v, x);
auto &fv = f[v];
AR<pl, 3> nf;
nf.fill({-BIG, -BIG});
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
pl now(fu[i].first + fv[j].first, fu[i].second + fv[j].second);
ckmax(nf[i], now);
}
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
/*
i+j=2 -1
i+j=1 0
i+j=0 1
*/
pl now(fu[i].first + fv[j].first + w + add_val * (1 - (i + j)), fu[i].second + fv[j].second + 1 - (i + j));
ckmax(nf[i + 1], now);
}
}
fu = nf;
}
};
dfs(dfs, 0, -1);
debug(f);
return std::max_element(all(f[0]))->second >= m;
};
ll d = fstTrue(-1'000'000'000'000LL, 1'000'000'000'000LL, check);
debug(d);
check(d);
std::cout << std::max_element(all(f[0]))->first - d * m;
return 0;
}
详细
Pretests
Final Tests
Test #1:
score: 5
Accepted
time: 1911ms
memory: 38340kb
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: 2981ms
memory: 59820kb
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: 1667ms
memory: 35180kb
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: 2717ms
memory: 55940kb
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: 1772ms
memory: 36660kb
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: 1473ms
memory: 36900kb
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: 2675ms
memory: 59056kb
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: 1780ms
memory: 37484kb
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: 1585ms
memory: 37748kb
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: 2646ms
memory: 54212kb
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: 2822ms
memory: 58792kb
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: 2792ms
memory: 55848kb
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: 1863ms
memory: 41344kb
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: 1606ms
memory: 35820kb
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: 1707ms
memory: 38568kb
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: 1542ms
memory: 35332kb
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: 2557ms
memory: 53172kb
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: 2836ms
memory: 63156kb
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: 2674ms
memory: 56092kb
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: 2427ms
memory: 51916kb
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'