QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#465843 | #9110. Zayin and Tree | grass8cow# | AC ✓ | 211ms | 37688kb | C++17 | 1000b | 2024-07-07 11:17:56 | 2024-07-07 11:17:56 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int dp[N][4],n,a[N];
#define pb push_back
vector<int>g[N];
int ans;
void ad(int &x,int y){if(x>y)x=y;}
void dfs(int x,int f){
for(int i=0;i<2;i++)for(int j=0;j<2;j++)dp[x][i|(j<<1)]=(i?a[x]:0)+(j?-a[x]:0)+1;
for(int v:g[x])if(v!=f){
dfs(v,x);
for(int i=0;i<4;i++)ans=min(ans,dp[x][i]+dp[v][3^i]);
for(int i=1;i>=0;i--)for(int j=1;j>=0;j--)for(int i_=1;i_>=0;i_--)
for(int j_=1;j_>=0;j_--){
if(i&&i_)continue;if(j&&j_)continue;
ad(dp[x][i|i_|((j|j_)<<1)],dp[v][i|(j<<1)]+(i_?a[x]:0)+(j_?-a[x]:0)+1);
}
}
}
void sol(){
ans=1;
scanf("%d",&n);for(int i=1;i<=n;i++)g[i].clear();
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1,u,v;i<n;i++){
scanf("%d%d",&u,&v);
g[u].pb(v),g[v].pb(u);
}
dfs(1,0);printf("%d\n",ans);
}
int main(){
int T;scanf("%d",&T);while(T--)sol();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 211ms
memory: 37688kb
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