QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#504513 | #9110. Zayin and Tree | Afterlife# | AC ✓ | 347ms | 21676kb | C++20 | 1.4kb | 2024-08-04 13:37:12 | 2024-08-04 13:37:12 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define N 1000100
typedef long long ll;
int n,a[N],ans;
vector<int> G[N];
int siz[N],maxp[N],rt,vis[N];
void get_root(int u,int fa,int total){
siz[u]=1;
maxp[u]=0;
for(auto v:G[u]){
if(v==fa||vis[v])continue;
get_root(v,u,total);
siz[u]+=siz[v];
maxp[u]=max(maxp[u],siz[v]);
}
maxp[u]=max(maxp[u],total-siz[u]);
if(!rt||maxp[u]<maxp[rt]){
rt=u;
}
}
int t1,t2;
void dfs2(int u,int fa,int dep){
t1=max(t1,a[u]-dep);
t2=max(t2,-a[u]-dep);
for(auto v:G[u]){
if(v==fa||vis[v])continue;
dfs2(v,u,dep+1);
}
}
void dfs(int u){
vis[u]=1;
t1=-1e9,t2=-1e9;
dfs2(u,0,0);
ans=max(ans,t1+t2-1);
for(auto v:G[u]){
if(vis[v])continue;
rt=0;
get_root(v,u,siz[v]);
dfs(rt);
}
}
void Solve(){
cin>>n;
for(int i=1;i<=n;++i){
G[i].clear();
vis[i]=0;
}
for(int i=1;i<=n;++i){
cin>>a[i];
}
for(int i=1;i<n;++i){
int u,v;
cin>>u>>v;
G[u].push_back(v);
G[v].push_back(u);
}
rt=0;
ans=-1e9;
get_root(1,0,n);
dfs(rt);
cout<<-ans<<'\n';
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin>>T;
while(T--)Solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 347ms
memory: 21676kb
input:
3009 5 4 5 3 4 2 1 2 2 3 3 4 3 5 5 4 4 1 1 2 1 2 2 3 3 4 3 5 10 5 8 1 0 8 7 5 2 0 4 2 4 3 8 3 9 1 2 1 3 3 6 4 5 5 7 6 10 10 6 8 8 4 8 0 6 6 0 2 7 10 1 7 2 9 2 3 3 4 1 5 1 6 6 8 1 2 10 9 0 4 0 4 6 0 2 0 0 1 5 1 3 1 7 2 6 1 2 1 9 1 4 5 8 7 10 10 8 8 1 2 7 4 8 6 0 8 1 6 1 7 1 5 7 9 1 3 1 2 2 10 3 4 1 8...
output:
0 -1 -6 -6 -7 -6 -7 -4 -3 -7 -5 -6 -5 -4 -6 -3 -4 -7 -4 -4 -6 -6 -6 -5 -4 -5 -6 -6 -7 -7 -5 -7 -6 -6 -7 -6 -5 -5 -4 -6 -6 -5 -6 -6 -6 -6 -3 -6 -3 -6 -4 -6 -7 -6 -7 -6 -6 -5 -7 -6 -4 -7 -3 -5 -5 -6 -4 -5 -7 -6 -5 -5 -4 -3 -5 -3 -4 -2 -6 -5 -7 -4 -5 -5 -7 -7 -4 -6 -5 -4 -6 -5 -5 -6 -3 -6 -7 -7 -7 -6 -...
result:
ok 3009 lines