QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#694392#6437. Paimon's TreeRegistrationError#TL 426ms26292kbC++201.9kb2024-10-31 17:51:242024-10-31 17:51:31

Judging History

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

  • [2024-10-31 17:51:31]
  • 评测
  • 测评结果:TL
  • 用时:426ms
  • 内存:26292kb
  • [2024-10-31 17:51:24]
  • 提交

answer

#include <bits/stdc++.h>
#define all(a) begin(a), end(a)
#define int long long
using namespace std;
const int N=160;
int n,a[N],dp[N][N][N],u,v,inch[N],ans,sz[N];
vector<int> adj[N],stk;
void calcsz(int u,int p,int grp){
    sz[grp]++;
    for (int i:adj[u]) if (!inch[i]&&i!=p) calcsz(i,u,grp);
}
void dfs(int u,int dep){
    stk.push_back(u);
    inch[u]=dep;
    if (adj[u].size()==1&&dep>1&&stk[0]<stk.back()){
        //for (int i:stk) cout<<i<<"-";
        //cout<<"::\n";
        for (int i=0;i<dep;i++){
            sz[i]=-1;
            calcsz(stk[i],stk[i],i);
            //cout<<stk[i]<<": "<<sz[i]<<"\n";
        }
        for (int i=0;i<=dep;i++) for (int j=0;j<=dep;j++) for (int k=0;k<=n;k++) dp[i][j][k]=-1e15;
        for (int i=0;i<dep;i++) for (int k=0;k<=sz[i];k++) dp[i][i][k]=0;
        for (int i=1;i<dep;i++) sz[i]+=sz[i-1];
        for (int len=1;len<dep;len++) for (int j=0;j+len<dep;j++) for (int k=1;k<n;k++) {
            dp[j][j+len][k]=max(dp[j+1][j+len][k-1],dp[j][j+len-1][k-1])+a[k];
            int v=sz[j+len]-(j?sz[j-1]:0)+len;
            /*if (stk[0]==2&&stk.back()==6){
                cout<<j<<" - "<<j+len<<" w "<<k<<": "<<v<<", "<<dp[j][j+len][k]<<"\n";
            }*/
            if (k<=v) dp[j][j+len][k]=max(dp[j][j+len][k],dp[j][j+len][k-1]);
        }
        ans=max(ans,dp[0][dep-1][n-1]);
        //cout<<dp[0][dep-1][n-1]<<"!\n";
    }
    for (int i:adj[u]) if (!inch[i]) dfs(i,dep+1);
    stk.pop_back();
    inch[u]=0;
}
void solve() {
    cin>>n;
    n++;
    for (int i=1;i<n;i++) cin>>a[i];
    for (int i=1;i<=n;i++) adj[i].clear();
    for (int i=1;i<n;i++){
        cin>>u>>v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    ans=-1e15;
    for (int i=1;i<=n;i++) if (adj[i].size()==1) dfs(i,1);
    cout<<ans<<"\n";
}

int32_t main() {
    cin.tie(0)->sync_with_stdio(false);

    int t = 1; cin >> t;
    while (t--) {
        solve();
    }
}

詳細信息

Test #1:

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

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

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

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

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

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

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: -100
Time Limit Exceeded

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:


result: