Submission #1332594

#TimeUsernameProblemLanguageResultExecution timeMemory
1332594JuanJLHarmonija (COCI25_harmonija)C++20
68 / 110
2595 ms23568 KiB
#include <bits/stdc++.h>

#define fst first
#define snd second
#define pb push_back
#define SZ(x) (int)x.size()
#define ALL(x) x.begin(),x.end()
#define forn(i,a,b) for(int i = a; i<b; i++)
#define mset(a,v) memset(a,v,sizeof(a))
#define FIN ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;
typedef long long ll;

const int MAXN = 200000+5;

#define INF 99999999999999999;
ll n,q;
ll red[MAXN];
ll blue[MAXN];

vector<ll> adj[MAXN];
ll dp[MAXN][10];
ll cen;

// diferencia red-blue
/*
0=-2
1=-1
2=0
3=1
4=2
*/
vector<ll> dif = {-2,-1,0,1,2};

void expand(ll nd, ll p){
    //cout<<"expand to "<<nd<<'\n';

    if(p==-1){
        dp[nd][3]=max(dp[nd][3] , red[nd]);
        dp[nd][1]=max(dp[nd][1] , blue[nd]);
    }else{
        forn(t, 0, 5){
            if(t-1>=0) dp[nd][t]=max( dp[nd][t] , dp[p][t-1]+red[nd] );
            if(t+1<5)  dp[nd][t]=max( dp[nd][t] , dp[p][t+1]+blue[nd] );
            //cout<<"dp "<<t<<" "<<dp[nd][t]<<'\n';
        }
    } 

    for(auto i:adj[nd]) if(i!=p){
        expand(i,nd);
    }
}

int main(){
    cin>>n>>q;
    forn(i,0,n) cin>>red[i];
    forn(i,0,n) cin>>blue[i];
    
    ll u,v; 
    forn(i,0,n-1){
        cin>>u>>v; u--; v--;
        adj[u].pb(v);
        adj[v].pb(u);
    }

    forn(i,0,q){
        cin>>u>>v; u--; v--;
        forn(j,0,MAXN) forn(k,0,10) dp[j][k]=-INF;
        
        expand(u,-1);

        ll res = -INF;
        forn(t,0,5) res=max(res,dp[v][t]);
        cout<<res<<'\n';
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...