QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#694229 | #6437. Paimon's Tree | RegistrationError# | WA | 371ms | 7868kb | C++20 | 1.9kb | 2024-10-31 17:30:57 | 2024-10-31 17:30:58 |
Judging History
answer
#include <bits/stdc++.h>
#define all(a) begin(a), end(a)
#define sz(a) (int) (a).size()
#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){
//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";
if (i) sz[i]+=sz[i-1];
}
for (int i=0;i<=n;i++) for (int j=0;j<=n;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 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();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3796kb
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: 371ms
memory: 7868kb
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 73rd numbers differ - expected: '4448483475', found: '4510407000'