#include using namespace std; #define modulo 1000000007 #define mod(mod_x) ((((long long)mod_x+modulo))%modulo) #define Inf 1000000001 struct subtree{ int mini=Inf,maxi=-Inf; int NGid=-1; bool ok=true; }; template struct rerooting{ F0 func0; F1 func1; T init_value; vector v; vector ans; vector visited; rerooting(vector> &E,F0 f0,F1 f1,T iv):func0(f0),func1(f1){ init_value = iv; v.resize(E.size()); ans.resize(E.size()); visited.resize(E.size(),false); for(int i=0;i> &E,int now,int p){ v[now] = init_value; for(int i=0;i> &E,int now,int p,T pv){ vector S(E[now].size(),init_value); if(S.size()>1){ for(int i=S.size()-2;i>=0;i--){ int to = E[now][i+1]; T x = v[to]; if(to==p)x = pv; S[i] = func0(x,S[i+1]); } } T temp = init_value; for(int i=0;i> E(N,vector()); for(int i=0;i rr(E,f0,f1,subtree()); for(int i=0;i