結果

問題 No.1221 木 *= 3
コンテスト
ユーザー ryoku
提出日時 2026-10-10 06:07:12
言語 C++23(gcc16)
(gcc 16.2.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 29 ms / 2,000 ms
+ 987µs
コード長 21,800 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,716 ms
コンパイル使用メモリ 319,928 KB
実行使用メモリ 13,696 KB
最終ジャッジ日時 2026-10-10 06:07:20
合計ジャッジ時間 6,973 ms
ジャッジサーバーID
(参考情報)
judge5_1 / judge4_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

// library begin: "lib/template.hpp"
#ifdef TEMPLATE
#else
#define TEMPLATE
# pragma GCC optimize("O3")
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <climits>
#include <cassert>
#include <functional>
#include <iterator>
#include <utility>
#include <complex>
#include <bitset>
#include <chrono>
#include <random>
#include <limits>
#include <optional>
#include <variant>
#include <any>

#include <array>
#include <bit>
#include <compare>
#include <concepts>
#include <numbers>
#include <ranges>
#include <span>
#include <string_view>
#include <tuple>
#include <type_traits>
#include <version>
using namespace std;
namespace ryoku{}
using uint=unsigned;
using ll=long long;
using ull=unsigned long long;
using ld=long double;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using i128=__int128;
using u128=unsigned __int128;
template<class T>using vc=vector<T>;
template<class T>using vvc=vc<vc<T>>;
template<class T>using vvvc=vvc<vc<T>>;
template<class T>using smpq=priority_queue<T,vector<T>,greater<T>>;
template<class T>using bipq=priority_queue<T>;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define REP(i,j,n) for(ll i=(j);i<(ll)(n);i++)
#define DREP(i,n,m) for(ll i=(n);i>=(m);i--)
#define drep(i,n) for(ll i=((n)-1);i>=0;i--)
#define rall(x) x.rbegin(),x.rend()
#define mp(...) make_pair(__VA_ARGS__)
#define pb push_back
#define fi first
#define se second
#define is insert
#define bg begin()
#define ed end()
#define all(x) x.begin(),x.end()
void scan(int&a) { cin >> a; }
void scan(ll&a) { cin >> a; }
void scan(string&a) { cin >> a; }
void scan(char&a) { cin >> a; }
void scan(uint&a) { cin >> a; }
void scan(ull&a) { cin >> a; }
void scan(bool&a) { cin >> a; }
void scan(ld&a){ cin>> a;}
template<class T> void scan(vector<T>&a) { for(auto&x:a) scan(x); }
void read() {}
template<class Head, class... Tail> void read(Head&head, Tail&... tail) { scan(head); read(tail...); }
#define INT(...) int __VA_ARGS__; read(__VA_ARGS__);
#define LL(...) ll __VA_ARGS__; read(__VA_ARGS__);
#define ULL(...) ull __VA_ARGS__; read(__VA_ARGS__);
#define STR(...) string __VA_ARGS__; read(__VA_ARGS__);
#define VC(type, name, ...) vector<type> name(__VA_ARGS__); read(name);
#define VVC(type, name, size, ...) vector<vector<type>> name(size, vector<type>(__VA_ARGS__)); read(name);
template<class T>void print(T a) { cout << a; }
template<class T> void print(vector<T>a) { for(int i=0;i<(int)a.size();i++){if(i)cout<<" ";print(a[i]);}}
void PRT() { cout <<endl; return ; }
template<class T> void PRT(T a) { print(a); cout <<endl; return; }
template<class Head, class... Tail> void PRT(Head head, Tail ... tail) { print(head); cout << " "; PRT(tail...); return; }
template<class T,class F>
bool chmin(T &x, F y){
    if(x>y){
        x=y;
        return true;
    }
    return false;
}
template<class T, class F>
bool chmax(T &x, F y){
    if(x<y){
        x=y;
        return true;
    }
    return false;
}
template<typename T>
T floor(T a,T b){
    return a/b-(a%b&&((a<0)!=(b<0)));
}
template<typename T>
T ceil(T a,T b){
    return a/b+(a%b&&((a<0)==(b<0)));
}
template<typename T>
T bmod(T x,T y){
    return x-y*floor(x,y);
}
template<typename T>
pair<T,T>divmod(T x,T y){
    T q=floor(x,y);
    return{q,x-q*y};
}
void YesNo(bool b){
    cout<<(b?"Yes":"No")<<endl;
}
void YESNO(bool b){
    cout<<(b?"YES":"NO")<<endl;
}
void AliceBob(bool b){
    cout<<(b?"Alice":"Bob")<<endl;
}
void TakahashiAoki(bool b){
    cout<<(b?"Takahashi":"Aoki")<<endl;
}
void Yes(){
    cout<<"Yes"<<endl;
}
void No(){
    cout<<"No"<<endl;
}
vc<int>stovi(const string&s,const string&S){
    vc<int>v(s.size());
    rep(i,s.size()){
        auto t=S.find(s[i]);
        assert(t!=string::npos);
        v[i]=t;
    }
    return v;
}
template<class T=ll>
T isqrt(T x){
    T F=sqrtl(x);
    while((F+1)*(F+1)<=x)F++;
    while(F*F>x)F--;
    return F;
}
template<class T>
T tri(T x){return x*(x-1)/2;}

//[l,r)
template<class T>T tri(T l,T r){return (r-l)*(l+r-1)/2;}
//n を先頭に持ってくる
template<class T>
vc<T>rot(vc<T>v,int n){
    rotate(v.begin(),v.begin()+n,v.end());
    return v;
}
template<class Container>
auto rle(const Container&cv){
    using T=typename Container::value_type;
    vc<pair<T,int>>res;
    for(const auto&x:cv){
        if(res.empty()||res.back().fi!=x)res.pb({x,1});
        else res.back().se++;
    }
    return res;
}
template<class T>
vc<T>iota(int n){
    vc<T>v(n);rep(i,n)v[i]=i;
    return v;
}
template<class T,int minfirst>
vc<int>argsort(const vc<T>&a){
    auto idx=iota<int>(a.size());
    sort(all(idx),[&](int i,int j){
        return (minfirst?make_pair(a[i],i)<make_pair(a[j],j):make_pair(a[i],i)>make_pair(a[j],j));
    });
    return idx;
}
template<class T>
vvc<T>trans(const vvc<T>&a){
    assert(a.size()&&a[0].size());
    rep(i,a.size())assert(a[i].size()==a[0].size());
    vvc<T>b(a[0].size(),vc<T>(a.size()));
    rep(i,a.size())rep(j,a[0].size())b[j][i]=a[i][j];
    return b;
}
vc<string>trans(const vc<string>&a){
    assert(a.size()&&a[0].size());
    rep(i,a.size())assert(a[i].size()==a[0].size());
    vc<string>b(a[0].size(),string(a.size(),0));
    rep(i,a.size())rep(j,a[0].size())b[j][i]=a[i][j];
    return b;
}
template<class T>
int popcount(T n){
    return __builtin_popcountll(n);
}
template<class T,class L=ll>
L sum(const vc<T>&a){
    return accumulate(all(a),L(0));
}
template<class T>
struct subset_view{
    T s;
    struct iterator{
        T s,x;
        bool done;
        T operator*()const{return x;}
        iterator&operator++(){
            if(x==0)done=true;
            else x=(x-1)&s;
            return*this;
        }
        bool operator!=(const iterator&r)const{return done!=r.done;}
    };
    iterator begin()const{return{s,s,false};}
    iterator end()const{return{s,0,true};}
};
template<class T>
subset_view<T>subset(T s){
    return{s};
}
template<class T>
T max(vc<T>&a){
    return *max_element(all(a));
}
template<class T>
T min(vc<T>&a){
    return *min_element(all(a));
}
template<class T,class L=ll>
vc<L> presum(vc<T> &a){
    vc<L> ret(a.size()+1);
    rep(i,a.size())ret[i+1]=ret[i]+a[i];
    return ret;
}
template<class T, class F>
vc<T> &operator+=(vc<T> &a,F b){
    for (auto&v:a)v += b;
    return a;
}
template<class T, class F>
vc<T> &operator-=(vc<T>&a,F b){
    for (auto&v:a)v-=b;
    return a;
}
template<class T, class F>
vc<T> &operator*=(vc<T>&a,F b){
    for (auto&v:a)v*=b;
    return a;
}
template<class T=ll>
constexpr T pow(T a,T b){
    T res=1;
    while(b){
        if(b&1)res*=a;
        a*=a;
        b/=2;
    }
    return res;
}
constexpr ll ten(ll a){
    return pow<ll>(10,a);
}
template<typename T>constexpr T inf=numeric_limits<T>::max()/2-1;
template<>
constexpr pll inf<pll> ={inf<ll>,inf<ll>};
template<class T>
constexpr T neg_inf=-inf<T>;
template<>
constexpr pll neg_inf<pll> ={-inf<ll>,-inf<ll>};
template<class T>
int tbit(T x){
    using U=make_unsigned_t<T>;
    U y=(U)x;
    return y?(int)bit_width(y)-1:-1;
}
template<class T>
int lbit(T x){
    using U=make_unsigned_t<T>;
    U y=(U)x;
    return y?(int)countr_zero(y):-1;
}
template<class T>
int tbit(T x,int p){
    using U=make_unsigned_t<T>;
    constexpr int W=numeric_limits<U>::digits;
    U y=(U)x;
    if(p<0)return -1;
    if(p>=W-1)return tbit(y);
    return tbit(y&((U(1)<<(p+1))-1));
}
template<class T>
int lbit(T x,int p){
    using U=make_unsigned_t<T>;
    constexpr int W=numeric_limits<U>::digits;
    U y=(U)x;
    if(p<0)return lbit(y);
    if(p>=W)return -1;
    return lbit(y&(~U(0)<<p));
}
istream& operator>>(istream&is,i128&x){
    string s;is>>s;
    x=0;
    int i=0,neg=0;
    if(s[0]=='-')neg=1,i=1;
    for(;i<(int)s.size();i++)x=x*10+s[i]-'0';
    if(neg)x=-x;
    return is;
}
ostream& operator<<(ostream&os,i128 x){
    if(x==0)return os<<0;
    if(x<0)os<<"-";
    u128 y=x<0?-(u128)x:(u128)x;
    string s;
    while(y)s.pb('0'+y%10),y/=10;
    reverse(all(s));
    return os<<s;
}
#ifdef LOCAL
#include"lib/debug/local.hpp"
inline int dbging=1;
#else
#define dbg(...) ((void)0)
#endif

struct template_setup{
    template_setup(){
        #ifdef LOCAL
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
        
        #endif
        cin.tie(0)->sync_with_stdio(0);
        #ifdef LOCAL
        cout<<fixed<<setprecision(6);
        #else
        cout<<fixed<<setprecision(20);
        #endif
    }
};
inline template_setup template_setup;


#endif
// library end: "lib/template.hpp"
// library begin: "lib/graph/base.hpp"
// library include: "lib/template.hpp"
namespace ryoku{
struct unweighted{
    unweighted()=default;
    unweighted(int){}
    operator int()const{return 1;}
};
template<class T=unweighted>
struct edge{
    int from,to,id;
    [[no_unique_address]]T cost;
#ifdef LOCAL
    friend ostream&operator<<(ostream&os,const edge&e){
        return os<<"{from:"<<e.from<<",to:"<<e.to<<",id:"<<e.id<<",cost:"<<e.cost<<"}";
    }
#endif
};
template<bool is_directed,class T=unweighted>
struct static_graph{
    constexpr static bool directed(){return is_directed;}
    using edge=::ryoku::edge<T>;
    using cost_t=T;
private:
    int n;
    mutable bool built=false,inv_built=false;
    vc<edge>edges;
    mutable vc<int>start,inv_start;
    mutable vc<edge>csr,inv_csr;
public:
    static_graph(int n):n(n),start(n+1),inv_start(n+1){assert(n>=0);}
    static_graph(int n,int m):static_graph(n){assert(m>=0);edges.reserve(m);}
    void resize(int size){
        assert(n<=size&&!built);
        assert(!inv_built);
        n=size;
        start.resize(n+1);
        inv_start.resize(n+1);
    }
    void add_edge(const edge&e){
        assert(!built&&!inv_built);
        assert(0<=e.from&&e.from<n&&0<=e.to&&e.to<n);
        edges.pb(e);
    }
    void add_edge(int a,int b,cost_t cost=1,int id=-1){
        assert(!built&&!inv_built);
        assert(0<=a&&a<n&&0<=b&&b<n);
        if(id==-1)id=edges.size();
        edges.pb({a,b,id,cost});
    }
    template<int substract=0>
    void input(int m){
        assert(m>=0);
        rep(i,m){
            INT(a,b);
            a-=substract;b-=substract;
            add_edge(a,b);
        }
        build();
    }
    void build()const{
        if(built)return;
        built=true;
        start.assign(n+1,0);
        for(auto&e:edges){
            ++start[e.from];
            if constexpr(!is_directed)++start[e.to];
        }
        rep(i,n)start[i+1]+=start[i];
        csr.resize(start[n]);
        for(auto it=edges.rbegin();it!=edges.rend();++it){
            auto&e=*it;
            csr[--start[e.from]]=e;
            if constexpr(!is_directed)csr[--start[e.to]]={e.to,e.from,e.id,e.cost};
        }
    }
    void buildinv()const{
        if(inv_built)return;
        inv_start.assign(n+1,0);
        for(auto&e:edges){
            ++inv_start[e.to];
            if constexpr(!is_directed)++inv_start[e.from];
        }
        rep(i,n)inv_start[i+1]+=inv_start[i];
        inv_csr.resize(inv_start[n]);
        for(auto it=edges.rbegin();it!=edges.rend();++it){
            auto&e=*it;
            inv_csr[--inv_start[e.to]]={e.to,e.from,e.id,e.cost};
            if constexpr(!is_directed)inv_csr[--inv_start[e.from]]={e.from,e.to,e.id,e.cost};
        }
        inv_built=true;
    }
    auto operator[](int u){
        build();
        assert(0<=u&&u<n);
        return span<edge>(csr.data()+start[u],start[u+1]-start[u]);
    }

    auto operator[](int u)const{
        build();
        assert(0<=u&&u<n);
        return span<const edge>(csr.data()+start[u],start[u+1]-start[u]);
    }

    auto inv(int u){
        buildinv();
        assert(0<=u&&u<n);
        return span<edge>(inv_csr.data()+inv_start[u],inv_start[u+1]-inv_start[u]);
    }

    auto inv(int u)const{
        buildinv();
        assert(0<=u&&u<n);
        return span<const edge>(inv_csr.data()+inv_start[u],inv_start[u+1]-inv_start[u]);
    }

    const vc<edge>&all_edges()const{return edges;}
    int edge_size()const{return edges.size();}

    edge get_edge(int id)const{
        assert(0<=id&&id<edge_size());
        return edges[id];
    }
    int out_deg(int u)const{
        build();
        assert(0<=u&&u<n);
        return start[u+1]-start[u];
    }
    int in_deg(int u)const{
        buildinv();
        assert(0<=u&&u<n);
        return inv_start[u+1]-inv_start[u];
    }
    int deg(int u)const{return out_deg(u);}
    int size()const{return n;}
    template<class F>
    vvc<F>adj()const{
        vvc<F>res(n,vc<F>(n));
        for(auto&e:edges){
            res[e.from][e.to]=e.cost;
            if constexpr(!is_directed)res[e.to][e.from]=e.cost;
        }
        return res;
    }
    void clear(){
        built=false;
        inv_built=false;
        edges.clear();
        csr.clear();
        inv_csr.clear();
        start.assign(n+1,0);
        inv_start.assign(n+1,0);
    }
    template<class F>
    void sort(int i,F f){
        build();
        assert(0<=i&&i<n);
        std::sort(csr.begin()+start[i],csr.begin()+start[i+1],f);
    }
};

}

#ifdef LOCAL
#include"lib/debug/formatters/graph--base.hpp"
#endif
// library end: "lib/graph/base.hpp"
// library begin: "lib/tree/base.hpp"
// library include: "lib/graph/base.hpp"
// library include: "lib/template.hpp"
// library include: "lib/graph/base.hpp"
namespace ryoku{
template<class T=unweighted>
struct tree{
    using Graph=static_graph<0,T>;
    using edge=typename Graph::edge;
    using cost_t=typename Graph::cost_t;
    mutable Graph g;
    mutable int built_hld=-1;
    int n;
    mutable vc<int>in,out,head,size_,par,depth,ord,cp,cp_root;
    tree(int n):n(n),g(n,n-1){}
    void add_edge(const edge&e){
        g.add_edge(e);
    }
    void add_edge(int a,int b,cost_t cost=1,int id=-1){
        g.add_edge(a,b,cost,id);
    }
    bool is_to_par(auto&e)const{
        return par[e.from]==e.to;
    }
    int size()const{
        return n;
    }
    bool same(int a,int b)const{
        assert(0<=a&&a<n&&0<=b&&b<n);
        build();
        return cp[a]==cp[b];
    }
    auto operator[](int u)const{
        assert(0<=u&&u<n);
        return g[u];
    }
    auto operator[](int u){
        assert(0<=u&&u<n);
        return g[u];
    }
    template<int substract>
    void input(){
        rep(i,n-1){
            INT(a,b);
            a-=substract;
            b-=substract;
            g.add_edge(a,b);
        }
    }
    template<int disjoint=0>
    void build(int root=0)const{
        if(built_hld!=-1)return;
        assert(0<=root&&root<n);
        built_hld=disjoint;
        in.resize(n);out.resize(n);head.resize(n);size_.assign(n,1);par.resize(n);depth.resize(n);ord.assign(n*(disjoint+1),-1);cp.assign(n,-1);cp_root.clear();cp_root.reserve(n);
        int timer=0;
        auto dfs=[&](auto&dfs,int u,int v,int d,int c)->void{
            cp[u]=c;depth[u]=d++;
            auto s=g[u];
            int heavy=-1,par_id=-1,heavy_size=0;
            rep(i,s.size()){
                auto&e=s[i];
                if(e.to==v){par_id=i;continue;}
                par[e.to]=u;
                dfs(dfs,e.to,u,d,c);
                size_[u]+=size_[e.to];
                if(size_[e.to]>heavy_size)heavy_size=size_[e.to],heavy=i;
            }
            if(heavy!=-1&&heavy!=0){
                swap(s[heavy],s[0]);
                if(par_id==0)par_id=heavy;
                else if(par_id==heavy)par_id=0;
            }
            if(par_id!=-1&&par_id+1!=s.size())swap(s[par_id],s[s.size()-1]);
        };
        auto visit=[&](int u){
            int c=cp_root.size();
            cp_root.pb(u);par[u]=-1;head[u]=u;
            dfs(dfs,u,-1,0,c);
        };
        visit(root);
        rep(u,n)if(cp[u]==-1)visit(u);
        auto dfs2=[&](auto&dfs2,int u)->void{
            in[u]=timer;ord[timer++]=u;
            auto s=g[u];
            int ce=s.size()-(par[u]!=-1);
            if(ce){
                head[s[0].to]=head[u];
                dfs2(dfs2,s[0].to);
                REP(i,1,ce)head[s[i].to]=s[i].to,dfs2(dfs2,s[i].to);
            }
            if constexpr(disjoint)out[u]=timer++;
            else out[u]=timer;
        };
        for(auto u:cp_root)dfs2(dfs2,u);
    }
    auto heavy_edge(int u)const{
        assert(0<=u&&u<n);
        build();
        auto s=g[u];
        int sz=s.size(),ce=(par[u]==-1?sz:sz-1);
        if(ce<=0)return span<const edge>{s.data(),s.data()};
        return span<const edge>{s.data(),s.data()+1};
    }
    auto light_edges(int u)const{
        assert(0<=u&&u<n);
        build();
        auto s=g[u];
        int sz=s.size(),ce=(par[u]==-1?sz:sz-1);
        if(ce<=1)return span<const edge>{s.data(),s.data()};
        return span<const edge>{s.data()+1,s.data()+ce};
    }
    int lca(int a,int b)const{
        assert(0<=a&&a<n&&0<=b&&b<n);
        build();
        auto&h=head;
        auto&d=depth;
        auto&p=par;
        if(cp[a]!=cp[b])return -1;
        while(1){
            if(h[a]==h[b]){
                if(d[a]<d[b])return a;
                return b;
            }
            if(d[h[a]]<d[h[b]])swap(a,b);
            a=p[h[a]];
        }
    }
    int dist(int a,int b)const{
        assert(0<=a&&a<n&&0<=b&&b<n);
        build();
        int L=lca(a,b);
        if(L==-1)return -1;
        return depth[a]+depth[b]-2*depth[L];
    }
    int jumpup(int a,int k)const{
        assert(0<=a&&a<n);
        build();
        if(k<0||depth[a]<k)return -1;
        auto&I=in;
        auto&H=head;
        auto&P=par;
        auto&O=ord;
        while(k>0){ 
            int x=I[a]-I[H[a]];
            if(x>=k){
                return O[I[a]-k];
            }
            k-=x+1;
            a=P[H[a]];
        }
        return a;
    }
    int jump(int s,int t,int k)const{
        assert(0<=s&&s<n&&0<=t&&t<n);
        build();
        if(cp[s]!=cp[t]||k<0)return -1;
        int L=lca(s,t);
        int D=depth[s]+depth[t]-2*depth[L];
        if(D<k)return -1;
        if(depth[s]-depth[L]>=k){
            return jumpup(s,k);
        }else{
            return jumpup(t,D-k);
        }
    }   
    int to(int x,int y)const{
        assert(0<=x&&x<n&&0<=y&&y<n&&x!=y);
        build();
        if(cp[x]!=cp[y])return -1;
        if(in[y]<in[x]||out[x]<=in[y])return g[x].size()-1;
        auto s=g[x];
        int ce=s.size()-(par[x]!=-1);
        if(in[s[0].to]<=in[y]&&in[y]<out[s[0].to])return 0;
        int l=1,r=ce;
        while(l<r){
            int m=(l+r)/2;
            if(in[s[m].to]<=in[y])l=m+1;
            else r=m;
        }
        return l-1;
    }
    vc<pair<int,int>>query(int s,int t,bool edge=false)const{
        assert(0<=s&&s<n&&0<=t&&t<n);
        build();
        if(cp[s]!=cp[t])return {};
        auto&h=head;
        auto&d=depth;
        auto&I=in;
        auto&P=par;
        vc<pair<int,int>>rs,rt;
        while(h[s]!=h[t]){
            if(d[h[s]]>d[h[t]]){
                rs.push_back({I[s],I[h[s]]});
                s=P[h[s]];
            }else{
                rt.push_back({I[h[t]],I[t]});
                t=P[h[t]];
            }
        }
        if(s!=t||!edge){
            if(d[s]>d[t]){
                rs.push_back({I[s],I[t]+edge});
            }else{
                rt.push_back({I[s]+edge,I[t]});
            }
        }
        rs.reserve(rs.size()+rt.size());
        for(auto it=rt.rbegin();it!=rt.rend();++it)rs.push_back(*it);
        return rs;
    }
    pair<int,int>get_diameter(int v=0)const{
        assert(n>0);
        assert(0<=v&&v<n);
        auto find_farthest=[&](int from)->int{
            vc<int>d(n),p(n,-2),st(1,from);
            p[from]=-1;
            int far=from;
            while(!st.empty()){
                int u=st.back();
                st.pop_back();
                if(d[far]<d[u])far=u;
                for(auto&e:g[u])if(e.to!=p[u]){
                    p[e.to]=u;
                    d[e.to]=d[u]+1;
                    st.push_back(e.to);
                }
            }
            return far;
        };
        int v1=find_farthest(v);
        int v2=find_farthest(v1);
        return {v1,v2};
    }
    pair<vc<int>,vc<typename Graph::edge>>get_explicit_path(int u,int v)const{
        vc<int>res;
        vc<typename Graph::edge>es;
        auto dfs=[&](auto&dfs,int now,int pre)->int{
            res.pb(now);
            if(now==v){
                return 1;
            }
            for(auto&e:g[now]){
                if(e.to==pre)continue;
                es.pb(e);
                if(dfs(dfs,e.to,now))return 1;
                es.pop_back();
            }
            return 0;
        };
        dfs(dfs,u,-1);
        return{res,es};
    }
};

}

#ifdef LOCAL
#include"lib/debug/formatters/tree--base.hpp"
#endif
// library end: "lib/tree/base.hpp"
// library include: "lib/graph/base.hpp"
using namespace ryoku;
void solve(){
    LL(n);
    VC(ll,a,n);
    VC(ll,b,n);
    tree<unweighted>g(n);g.input<1>();
    auto dfs=[&](auto&dfs,int u,int v)->array<ll,2>{
        array<ll,2>res{};
        res[0]=0;res[1]=a[u];
        for(auto&e:g[u]){
            if(e.to==v)continue;
            auto rdp=dfs(dfs,e.to,u);
            array<ll,2>ndp{-inf<ll>,-inf<ll>};
            rep(use,2){
                rep(pre,2){
                    dbg(res[pre],(1-use)*(1-pre)*(b[u]+b[e.to]),use,pre);
                    chmax(ndp[use],res[use]+rdp[pre]+(1-use)*(1-pre)*(b[u]+b[e.to]));
                }
            }
            res=move(ndp);
        }
        dbg(res);
        return res;
    };
    auto ans=dfs(dfs,0,-1);
    PRT(max(ans[0],ans[1]));
}
signed main(){
    int t=1;
    // cin >> t;
    while(t--)solve();
}
0