QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#292786#6437. Paimon's TreeDoqeAC ✓2350ms31376kbC++141.7kb2023-12-28 13:20:442023-12-28 13:20:44

Judging History

你现在查看的是最新测评结果

  • [2023-12-28 13:20:44]
  • 评测
  • 测评结果:AC
  • 用时:2350ms
  • 内存:31376kb
  • [2023-12-28 13:20:44]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=152;
long long F[N][N][N];
int n,s[N];
set<vector<int>>S;
vector<int>to[N];
int sta[N],MD,sz[N];
void ini(int u,int f){sz[u]=1;for(int v:to[u])if(v!=f)ini(v,u),sz[u]+=sz[v];}
void dfs(int u,int f,int top)
{
    if(f&&to[u].size()==1)sta[top]=0,S.insert(vector<int>(sta+1,sta+top+1));
    for(int v:to[u])if(v!=f)sta[top]=sz[u]-sz[v]-1,dfs(v,u,top+1);
}
int a[N];
__inline void ckmax(int&A,int B){(A<B)?(A=B):0;}
__inline void ckmax(long long&A,long long B){(A<B)?(A=B):0;}
int main()
{
    cin.tie(0)->sync_with_stdio(0);
    int T;cin>>T;
    while(T--)
    {
        cin>>n;++n;MD=0;
        long long ans=0;
        for(int i=0;i<n-1;++i)cin>>a[i];
        for(int i=1,u,v;i<n;++i)cin>>u>>v,to[u].push_back(v),to[v].push_back(u);
        for(int i=1;i<=n;++i)if(to[i].size()==1)ini(i,0),dfs(i,0,1);
        for(const vector<int>&A:S)
        {
            MD=A.size();
            for(int i=0;i<n;++i)for(int l=0;l<MD;++l)for(int r=0;r<MD;++r)F[i][l][r]=0;
            for(int i=0;i<MD;++i)s[i]=(i?s[i-1]:0)+A[i];
            for(int i=0;i<n-1;++i)
                for(int l=0;l<MD;++l)
                    for(int r=l;r<MD;++r)
                        if(s[r]-(l?s[l-1]:0)+(r-l)>=i)
                        {
                            ckmax(F[i+1][l][r],F[i][l][r]);
                            if(l)ckmax(F[i+1][l-1][r],F[i][l][r]+a[i]);
                            if(r+1<MD)ckmax(F[i+1][l][r+1],F[i][l][r]+a[i]);
                        }
            ckmax(ans,F[n-1][0][MD-1]);
        }
        S.clear();
        cout<<ans<<endl;
        for(int i=1;i<=n;++i)to[i].clear();
    }
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3876kb

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: 130ms
memory: 5840kb

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: 71ms
memory: 7796kb

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: 414ms
memory: 29180kb

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: 7ms
memory: 28236kb

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: 678ms
memory: 30592kb

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: 33ms
memory: 30900kb

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: 28ms
memory: 30484kb

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: 2350ms
memory: 29940kb

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: 545ms
memory: 31376kb

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: 365ms
memory: 29116kb

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