QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#718902 | #6437. Paimon's Tree | qwqUwU_ | TL | 6ms | 31888kb | C++14 | 1.5kb | 2024-11-06 21:44:25 | 2024-11-06 21:44:26 |
Judging History
answer
#include<bits/stdc++.h>
#define pb push_back
#define P make_pair
#define fi first
#define se second
#define bit(s,x) (((s)>>(x))&1)
#define pnp(s) __builtin_popcountll(s)
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
inline ll read(){
ll x=0,f=1,c=getchar();
while(c<'0'||c>'9')f=(c=='-'?-1:1),c=getchar();
while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
return x*f;
}
const int N=153;
int n,a[N];
ll f[N][N][N];
vector<int>G[N];
int rt;
int sz[N][N],F[N][N];
inline void dfs(int u,int fa){
F[rt][u]=fa; sz[rt][u]=1;
for(int v:G[u])if(v!=fa)dfs(v,u),sz[rt][u]+=sz[rt][v];
}
inline void ck(ll &x,ll y){if(y>x)x=y;}
inline void solve(){
n=read();rep(i,1,n)a[i]=read();
rep(i,1,n+1)G[i].clear();
rep(i,1,n){
int u=read(),v=read();
G[u].pb(v),G[v].pb(u);
}
rep(i,1,n+1) dfs(rt=i,0);
memset(f,-1,sizeof f);
rep(i,1,n+1)f[0][i][i]=0;
rep(i,0,n-1)rep(u,1,n+1)rep(v,1,n+1)if(f[i][u][v]!=-1){
ck(f[i+1][u][v],f[i][u][v]);
for(int w:G[u])if(w!=F[v][u]&&i<=n-sz[v][w])ck(f[i+1][w][v],f[i][u][v]+a[i+1]);
if(u==v)continue;
for(int w:G[v])if(w!=F[u][v]&&i<=n-sz[u][w])ck(f[i+1][u][w],f[i][u][v]+a[i+1]);
}
ll ans=0;
rep(u,1,n+1)rep(v,1,n+1)ans=max(ans,f[n][u][v]);
printf("%lld\n",ans);
}
int main() {
//freopen("data.in", "r", stdin);
// freopen("myans.out","w",stdout);
int T=read();while(T--)solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 6ms
memory: 31888kb
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
Time Limit Exceeded
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...