QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#629485 | #6437. Paimon's Tree | eggegg185 | AC ✓ | 1160ms | 119068kb | C++14 | 2.6kb | 2024-10-11 12:24:45 | 2024-10-11 12:24:45 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n; vector<int> vecs[155];
int dp[155][155][155][2][2],a[155],sws[155][155],dep[155][155];
inline void foo(int& x,int y) {x = (x>y)?(x):(y);} //for convince
void dfs(int now,int pa,int c) {
dep[c][now] = dep[c][pa]+1; sws[c][now] = 1;
for(auto v:vecs[now]) {
if(v == pa) continue; dfs(v,now,c);
sws[c][now] += sws[c][v];
}
}
void solve() { //damn, multitest???!
cin >> n; n++; for(int i = 1; i < n; i++) {cin >> a[i]; vecs[i].clear();} vecs[n].clear();
for(int i = 1; i < n; i++) {
int u,v; cin >> u >> v;
vecs[u].push_back(v); vecs[v].push_back(u);
}
for(int i = 1; i <= n; i++) dfs(i,0,i);
for(int k = 0; k < n; k++) for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) {
dp[k][i][j][0][0] = dp[k][i][j][0][1] = dp[k][i][j][1][0] = dp[k][i][j][1][1] = -0x3f3f3f3f3f3f3f3f;
}
for(int i = 1; i <= n; i++) {
for(auto v:vecs[i]) {
dp[0][i][v][1][0] = dp[0][v][i][0][1] = 0;
for(auto w:vecs[i]) if(v != w) dp[0][w][v][0][0] = 0;
}
}
for(int k = 0; k < n-1; k++) {
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
foo(dp[k+1][i][j][1][1],dp[k][i][j][1][1]); //ci && cj
//!ci && cj
foo(dp[k+1][i][j][1][1],dp[k][i][j][0][1]+a[k+1]);
if(k < n-1-sws[j][i]) foo(dp[k+1][i][j][0][1],dp[k][i][j][0][1]);
for(auto v:vecs[i]) {
if(dep[v][j] < dep[i][j]) continue;
foo(dp[k+1][v][j][0][1],dp[k][i][j][0][1]+a[k+1]);
}
//ci && !cj
foo(dp[k+1][i][j][1][1],dp[k][i][j][1][0]+a[k+1]);
if(k < n-1-sws[i][j]) foo(dp[k+1][i][j][1][0],dp[k][i][j][1][0]);
for(auto v:vecs[j]) {
if(dep[v][i] < dep[j][i]) continue;
foo(dp[k+1][i][v][1][0],dp[k][i][j][1][0]+a[k+1]);
}
//!ci && !cj
foo(dp[k+1][i][j][1][0],dp[k][i][j][0][0]+a[k+1]);
foo(dp[k+1][i][j][0][1],dp[k][i][j][0][0]+a[k+1]);
if(k < n-1-sws[i][j]-sws[j][i]) foo(dp[k+1][i][j][0][0],dp[k][i][j][0][0]);
for(auto v:vecs[i]) {
if(dep[v][j] < dep[i][j]) continue;
foo(dp[k+1][v][j][0][0],dp[k][i][j][0][0]+a[k+1]);
}
for(auto v:vecs[j]) {
if(dep[v][i] < dep[j][i]) continue;
foo(dp[k+1][i][v][0][0],dp[k][i][j][0][0]+a[k+1]);
}
}
}
}
int ans = 0;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) for(int ci = 0; ci < 2; ci++)
for(int cj = 0; cj < 2; cj++) ans = max(ans,dp[n-1][i][j][ci][cj]);
}
printf("%lld\n",ans);
for(int i = 1; i <= n; i++) vecs[i].clear();
}
signed main() {
//freopen("length.in","r",stdin);
//freopen("length.out","w",stdout);
cin.tie(0)->sync_with_stdio(false);
int T; cin >> T; while(T--) solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 7984kb
input:
2 5 1 7 3 5 4 1 3 2 3 3 4 4 5 4 6 1 1000000000 1 2
output:
16 1000000000
result:
ok 2 number(s): "16 1000000000"
Test #2:
score: 0
Accepted
time: 286ms
memory: 18428kb
input:
5000 19 481199252 336470888 634074578 642802746 740396295 773386884 579721198 396628655 503722503 971207868 202647942 2087506 268792718 46761498 443917727 16843338 125908043 691952768 717268783 9 4 4 18 12 9 10 9 4 1 6 12 2 18 9 13 6 14 4 8 2 3 10 17 2 20 19 20 5 1 12 15 15 16 4 7 17 11 4 240982681 ...
output:
5750811120 1896999359 4208559611 4140156713 5361004844 1875024569 3690026656 3702623113 3412485417 7807375141 5341435147 2355946110 3090496776 5626636202 4729664767 2207592767 572868833 4759005973 2944749369 2538044586 3083947956 5757497518 1421427135 3971435093 1197051728 396588615 251138097 221986...
result:
ok 5000 numbers
Test #3:
score: 0
Accepted
time: 429ms
memory: 18448kb
input:
5000 19 54748096 75475634 804928248 476927808 284875072 503158867 627937890 322595515 786026685 645468307 669240390 939887597 588586447 973764525 521365644 710156469 985188306 860350786 11308832 4 13 10 4 4 11 6 4 4 19 4 1 4 15 4 8 4 2 4 20 4 18 4 12 5 4 4 3 17 4 7 4 16 4 4 14 9 4 13 622212643 57402...
output:
1958952831 1556504637 1811593532 1843992975 1412884109 1670235363 1366463682 1730307362 1641481010 774899289 1844540747 1841737518 1901514823 896915645 1811367031 1942958831 1510851341 1868235486 1717710959 1746435719 1775669008 1545978811 1260512063 1901314686 1887583955 1612937227 1663411828 24160...
result:
ok 5000 numbers
Test #4:
score: 0
Accepted
time: 715ms
memory: 118216kb
input:
10 146 923264237 374288891 535590429 751244358 124321145 232930851 266089174 543529670 773363571 319728747 580543238 582720391 468188689 490702144 598813561 138628383 284660056 733781508 155605777 931759705 245485733 723534730 257812292 794937524 596788519 188451996 981010588 14483682 59267682 95946...
output:
16105116749 14057223647 17705414317 14384453079 15404734809 15790061661 16231602795 19233686195 18857573113 11187760320
result:
ok 10 numbers
Test #5:
score: 0
Accepted
time: 756ms
memory: 118960kb
input:
10 142 496813081 673102149 561219907 730593611 814024114 812959730 314305867 469496529 350635050 699021890 342102981 815487777 787982418 857896659 526518374 421876106 438907614 902179526 449645826 783856158 865633510 238642240 774653971 962475573 467098727 196513513 561435449 333165290 951567552 726...
output:
1977223339 1953394635 1988595590 1980797827 1983374568 1983162616 1992209378 1976533200 1982262321 1960170345
result:
ok 10 numbers
Test #6:
score: 0
Accepted
time: 701ms
memory: 117940kb
input:
10 134 365329221 412106895 291882089 564718673 358502890 837699009 657489855 690430685 632939232 373282330 398630021 753287868 667584659 79866982 603966291 850348020 738379364 480642952 593942770 930919906 485781288 903492853 141752547 984789430 897217447 909607734 846893014 211655411 843867422 7894...
output:
54921227449 58412405010 62668998860 60013031127 59126475793 61723803281 60669846784 64603047323 64554268427 60829821341
result:
ok 10 numbers
Test #7:
score: 0
Accepted
time: 768ms
memory: 119068kb
input:
10 150 643910770 5887448 757703054 544067926 902981667 712695184 295641139 911364840 620276118 902318577 865222469 250896470 987378388 742028793 681414208 133595743 597659626 649040970 33207011 223207847 960704874 418600362 658594226 417168695 767527655 622701955 867509363 235369723 31134588 7022106...
output:
67038541760 60389299543 58072836329 65133495854 63822929049 57486908835 58850978806 67419523894 59616918455 58277647975
result:
ok 10 numbers
Test #8:
score: 0
Accepted
time: 634ms
memory: 116632kb
input:
10 147 72235422 449924898 783332532 378192988 592684636 147499872 343857831 837331700 197547597 576579017 776525316 188696560 12204822 669031820 758862125 826908873 897131377 817438988 737312468 370271596 580852652 638740575 585501313 439482552 637837864 335796176 447934224 259084035 778210267 46972...
output:
76820910063 67648954926 71427358468 76169641177 64552726972 68783255604 69293078661 72806596538 74951915651 77616665583
result:
ok 10 numbers
Test #9:
score: 0
Accepted
time: 815ms
memory: 117388kb
input:
10 148 25 18 53 30 32 44 30 62 54 92 69 38 36 12 95 5 5 49 61 99 76 58 61 61 7 96 57 2 47 5 98 89 32 8 15 53 8 79 39 83 52 62 6 52 76 9 81 25 37 61 34 9 69 39 9 62 9 95 45 20 20 23 72 76 89 98 46 18 89 86 90 57 22 90 616721187 961978145 919995685 593080507 682726140 504661700 847323800 681816611 768...
output:
46039132286 47348735144 49971770686 44899592918 40878796202 46757933697 47265470205 45710301491 47425113722 47012233388
result:
ok 10 numbers
Test #10:
score: 0
Accepted
time: 1160ms
memory: 118076kb
input:
5000 19 514300407 782710197 539624191 631858791 976609486 752268030 30225807 279200011 467188665 630132600 594612100 769329445 916633496 258196658 913757959 538628510 55883389 859267729 615840950 4 6 4 13 16 4 4 1 19 4 15 4 10 4 4 3 14 4 7 4 4 12 4 20 8 4 2 4 4 11 17 4 4 9 4 5 18 4 14 764136687 1842...
output:
1893242982 1736357333 1014806839 1863428366 1966435774 1918990218 1739753908 1946580996 1861052511 131731019 1880511758 1981855954 1723656459 1861986784 1797533442 1951806347 1825740616 1984078932 1629741508 1863542580 1882941663 1636957045 517967696 1768015316 1928534569 1796454921 1679623025 14820...
result:
ok 5000 numbers
Test #11:
score: 0
Accepted
time: 700ms
memory: 118384kb
input:
10 143 821955558 367726249 233055154 541810622 149799292 162777358 9876091 757696856 516870441 847843655 872492634 307022494 260602767 682939057 460413579 74686486 209722407 924713885 170098274 805044860 698032060 216281038 480767427 46766398 992837869 893877503 294452136 563587327 212669103 9857882...
output:
14350701440 12306227726 14860078357 14999671004 12079179804 15028800831 19265283513 13993544698 14442300020 13394247064
result:
ok 10 numbers