QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#292754 | #6437. Paimon's Tree | Doqe | WA | 35ms | 3884kb | C++14 | 3.0kb | 2023-12-28 12:43:27 | 2023-12-28 12:43:28 |
Judging History
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)
{
if(MD<top)MD=top,S.clear();sta[top]=0;
if(MD==top)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,U=0,C=0;
cin>>T;U=T>10;
while(T--)
{
++C;
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)>=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]);
}
// int l=0,r=MD-1;
// for(int i=n-1;i;--i)
// {
// cerr<<i<<": "<<l<<" "<<r<<" "<<s[r]-(l?s[l-1]:0)+(r-l)<<endl;
// if(F[i][l][r]==F[i-1][l][r])cout<<"NO KEY\n";
// else if(F[i][l][r]==F[i-1][l+1][r]+a[i-1])cout<<"-> "<<l<<" "<<r<<endl,++l;
// else if(F[i][l][r]==F[i-1][l][r-1]+a[i-1])cout<<"-> "<<l<<" "<<r<<endl,--r;
// else cout<<"???",assert(0);
// }
// 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)>=i)
// cerr<<"F "<<i<<" "<<l<<"~"<<r<<" : "<<F[i][l][r]<<endl;
ckmax(ans,F[n-1][0][MD-1]);
}
S.clear();
cout<<ans<<endl;
// if(!U)cout<<ans<<endl;
// else if(C==48)
// {
// for(int i=1;i<=n;++i)
// for(int j:to[i])cout<<i<<" "<<j<<endl;
// for(int i=0;i<n-1;++i)cout<<a[i]<<"_";cout<<endl;
// }
for(int i=1;i<=n;++i)to[i].clear();
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3492kb
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: 35ms
memory: 3884kb
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 361st numbers differ - expected: '6138525060', found: '5993096216'