QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#575919 | #6437. Paimon's Tree | Harry27182 | WA | 97ms | 7828kb | C++14 | 2.0kb | 2024-09-19 17:23:03 | 2024-09-19 17:23:04 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
using namespace std;
int dep[155],f[155],dp[155][155][155],T,n,h[155];
int u,v,w[155],vis[155],deg[155],cnt,a[155],sum[155];
struct edge{int v,nxt;}e[305];
void add(int u,int v){e[++cnt]={v,h[u]};h[u]=cnt;}
void dfs(int u,int fa)
{
dep[u]=dep[fa]+1;f[u]=fa;
for(int i=h[u];i;i=e[i].nxt)
{
int v=e[i].v;
if(v==fa)continue;
dfs(v,u);
}
}
int lca(int u,int v)
{
while(u!=v)
{
if(dep[u]<dep[v])swap(u,v);
u=f[u];
}
return u;
}
int dfs2(int u,int fa)
{
int res=1;
for(int i=h[u];i;i=e[i].nxt)
{
int v=e[i].v;
if(v==fa||vis[v])continue;
res+=dfs2(v,u);
}
return res;
}
signed main()
{
cin.tie(0)->sync_with_stdio(0);
cin>>T;
while(T--)
{
cin>>n;n++;int ans=0;cnt=0;
for(int i=1;i<n;i++)cin>>w[i];
for(int i=1;i<=n;i++)h[i]=0,deg[i]=0;
for(int i=1;i<n;i++)
{
cin>>u>>v;deg[u]++;deg[v]++;
add(u,v);add(v,u);
}
dfs(1,0);
for(int u=1;u<=n;u++)
{
if(deg[u]>1)continue;
for(int v=u+1;v<=n;v++)
{
if(deg[v]>1)continue;
int p=lca(u,v),tot=0;
for(int i=u;i!=p;i=f[i])a[++tot]=i;
a[++tot]=p;int pos=tot;
for(int i=v;i!=p;i=f[i])a[++tot]=i;
reverse(a+pos+1,a+tot+1);
for(int i=1;i<=n;i++)vis[i]=0;
for(int i=1;i<=tot;i++)vis[a[i]]=1;
for(int i=1;i<=tot;i++)sum[i]=dfs2(a[i],0)-1;
for(int i=1;i<=tot;i++)sum[i]+=sum[i-1];
for(int i=1;i<=tot;i++)for(int j=i;j<=tot;j++)for(int k=0;k<n;k++)dp[i][j][k]=0;
for(int len=1;len<tot;len++)
{
for(int i=1;i+len-1<=tot;i++)
{
int j=i+len-1;
for(int k=0;k<n;k++)
{
if(k+1<n&&sum[j]-sum[i-1]+j-i>=k+1)dp[i][j][k+1]=max(dp[i][j][k+1],dp[i][j][k]);
if(i>1)dp[i-1][j][k+1]=max(dp[i-1][j][k+1],dp[i][j][k]+w[k+1]);
if(j<tot)dp[i][j+1][k+1]=max(dp[i][j+1][k+1],dp[i][j][k]+w[k+1]);
}
}
}
ans=max(ans,dp[1][tot][n-1]);
}
}
cout<<ans<<'\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3656kb
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: 97ms
memory: 7828kb
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 4743710325 1875024569 3690026656 3702623113 3412485417 7807375141 4874373848 2136473141 3090496776 5255698485 4550445099 2207592767 572868833 4644501974 2709451916 2538044586 2924577294 5225343016 1421427135 3971435093 1197051728 396588615 251138097 215311...
result:
wrong answer 5th numbers differ - expected: '5361004844', found: '4743710325'