QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#670948#6437. Paimon's Treelouhao088WA 8ms3680kbC++232.0kb2024-10-24 09:00:582024-10-24 09:01:01

Judging History

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

  • [2024-10-24 09:01:01]
  • 评测
  • 测评结果:WA
  • 用时:8ms
  • 内存:3680kb
  • [2024-10-24 09:00:58]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int maxn=150+5,M=3e5+5;
#define pb push_back
inline int read(){
    char ch=getchar();int x=0;bool f=0;
    for(;!isdigit(ch);ch=getchar())if(ch=='-')f=1;
    for(;isdigit(ch);ch=getchar())x=(x<<1)+(x<<3)+(ch^48);
    if(f==1){x=-x;}return x;
}
int T,n,m,x,y,a[maxn];
long long f[maxn][maxn],g[maxn][maxn];
int sz[maxn],mx,F,flag[maxn];
int s[maxn],dis[maxn];
int D[maxn],cnt=0;
vector<int>e[maxn];
void dfs(int x,int fa){
    dis[x]=dis[fa]+1;
    if(dis[mx]<dis[x])mx=x;
    for(auto i:e[x])if(i^fa){
        dfs(i,x);
    }
}
void getd(int x,int fa){
    if(x==mx){
        F=1;D[++cnt]=x;flag[x]=1;
        return;
    }
    for(auto i:e[x])if(i!=fa&&!F){
        getd(i,x);
    }
    if(F)D[++cnt]=x,flag[x]=1;
}
void getsz(int x,int fa){
    sz[x]++;
    for(auto i:e[x])if(i!=fa&&!flag[i]){
        getsz(i,x);sz[x]+=sz[i];
    }
}
int o=0;
void solve(){
    n=read();
    o++;
    for(int i=1;i<=n;i++)a[i]=read();
    n++;
    for(int i=1;i<n;i++){
        x=read(),y=read(),e[x].pb(y),e[y].pb(x);
    }//return ;
    dfs(1,0);dis[mx]=0;int z=mx;
    dfs(z,0);F=0;cnt=0;
    getd(z,0);//return;
    for(int i=0;i<=cnt;i++)getsz(D[i],0);
    for(int j=1;j<=cnt;j++)s[j]=s[j-1]+sz[D[j]]-1;
    for(int i=1;i<=cnt;i++)
        for(int j=i;j<=cnt;j++)f[i][j]=0;
    for(int i=1;i<n;i++){
        for(int j=1;j<=cnt;j++){
            for(int k=j;k<=cnt;k++){
                g[j][k]=0;
                if(s[k]-s[j-1]>=i-(k-j))g[j][k]=max(f[j][k],g[j][k]);
                if(j!=k)g[j][k]=max(g[j][k],f[j+1][k]+a[i]);
                if(j!=k)g[j][k]=max(g[j][k],f[j][k-1]+a[i]);
                //cout<<g[j][k]<<" "<<j<<" "<<k<<endl;
            }
        }
        for(int j=1;j<=cnt;j++)
            for(int k=j;k<=cnt;k++)
                f[j][k]=g[j][k];
    }
    cout<<f[1][cnt]<<endl;
    mx=0;F=0;
    for(int i=1;i<=n;i++)e[i].clear(),dis[i]=0,flag[i]=0,sz[i]=0;
}
signed main(){
    T=read();
    while(T--){
        solve();
    }
    return 0;
}

詳細信息

Test #1:

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

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

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 46th numbers differ - expected: '4845762928', found: '4505741853'