QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#292742#6437. Paimon's TreeDoqeWA 43ms3848kbC++142.2kb2023-12-28 12:31:062023-12-28 12:31:07

Judging History

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

  • [2023-12-28 12:31:07]
  • 评测
  • 测评结果:WA
  • 用时:43ms
  • 内存:3848kb
  • [2023-12-28 12:31:06]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=152;
long long F[N][N][N];
int n,s[N];
vector<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)
    {
        if(MD<top)MD=top,S.clear();sta[top]=0;
        if(MD==top)S.push_back(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,U=0;
    cin>>T;U=T>10;
    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);
        // cerr<<"ED\n";
        for(const vector<int>&A:S)
        {
            // cerr<<A.size()<<": ";
            // for(auto k:A)cerr<<k<<"_";cerr<<endl;
            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<MD;++i)F[0][i][i]=0;
            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+1)>=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]);
                            ckmax(F[i+1][l][r+1],F[i][l][r]+a[i]);
                        }
            // for(int i=0;i<n;++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+1)>=i)
            //                 cerr<<"F "<<i<<" "<<l<<"~"<<r<<" : "<<F[i][l][r]<<endl;
            ckmax(ans,F[n-1][0][MD-1]);
        }
        S.clear();
        for(int i=1;i<=n;++i)to[i].clear();
        cout<<ans<<endl;
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3484kb

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: -100
Wrong Answer
time: 43ms
memory: 3848kb

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:

wrong answer 48th numbers differ - expected: '5317528311', found: '5514819848'