QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#708985 | #6437. Paimon's Tree | Others | WA | 70ms | 5808kb | C++14 | 2.1kb | 2024-11-04 10:30:45 | 2024-11-04 10:30:46 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define eps 1e-12
#define db long double
#define ull unsigned long long
#define wr(x,ch) write(x),putchar(ch)
#define lb(x) ((x)&-(x))
using namespace std;
typedef pair<ll,ll> pai;
#define x first
#define y second
ll read() {
ll x=0;
bool f=0;char c=getchar();
while(!isdigit(c)) f|=c=='-',c=getchar();
while(isdigit(c)) x=(x<<3)+(x<<1)+(c^48),c=getchar();
return f?-x:x;
}
void write(ll x) {
if(x<0) putchar('-'),x=-x;
if(x>=10) write(x/10);
putchar(x%10^48);
return ;
}
const int N=155;
int dp[N][N][N],siz[N],dep[N],n,u,v,ans,a[N],s[N],fa[N],sta[N],top;
vector<int> G[N];
void dfs(int p) {
siz[p]=1;
for(const auto &lxl:G[p])
if(lxl!=fa[p])
fa[lxl]=p,dfs(lxl),siz[p]+=siz[lxl];
return ;
}
int calc(int x,int y) {
if(fa[x]==y) return siz[x];
else return n-siz[y];
}
bool getr(int p,int fa,int t) {
sta[++top]=p;
s[top]=calc(sta[top],sta[top-1]);
if(t==p) return 1;
for(const auto &lxl:G[p])
if(lxl!=fa) {
if(getr(lxl,p,t)) return 1;
}
top--;
return 0;
}
void solve() {
for(int i=1;i<top;i++) s[i]-=s[i+1]-1;
for(int i=2;i<=top;i++) s[i]+=s[i-1];
for(int i=1;i<=top;i++)
for(int j=i;j<=top;j++)
for(int k=0;k<n;k++) dp[i][j][k]=0;
for(int len=2;len<=top;len++) {
for(int l=1,r=len;r<=top;l++,r++) {
for(int t=1;t<n;t++) {
dp[l][r][t]=dp[l][r][t-1];
if(t<=s[r-1]-s[l-1]+(r-l)+1) dp[l][r][t]=max(dp[l][r][t],dp[l][r-1][t-1]+a[t]);
if(t<=s[r]-s[l]+(r-l)+1) dp[l][r][t]=max(dp[l][r][t],dp[l+1][r][t-1]+a[t]);
}
}
}
ans=max(ans,dp[1][top][n-1]);
return ;
}
void Solve() {
n=1+read();
for(int i=1;i<=n;i++) G[i].clear();
for(int i=1;i<n;i++) a[i]=read();
for(int i=1;i<n;i++) u=read(),v=read(),G[u].push_back(v),G[v].push_back(u);
dfs(1);
ans=0;
for(int i=1;i<=n;i++) {
for(int j=i+1;j<=n;j++) {
if(G[i].size()==1&&G[j].size()==1) {
top=0;
getr(i,0,j);
solve();
}
}
}
wr(ans,'\n');
return ;
}
signed main() {
int t=read();
while(t--) Solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5692kb
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: 70ms
memory: 5808kb
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'