#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; namespace { using Integer = long long; //__int128; template istream& operator >> (istream& is, vector& vec){for(T& val: vec) is >> val; return is;} template istream& operator , (istream& is, T& val){ return is >> val;} template ostream& operator << (ostream& os, const vector& vec){for(int i=0; i ostream& operator , (ostream& os, const T& val){ return os << " " << val;} template void print(const H& head){ cout << head; } template void print(const H& head, const T& ... tail){ cout << head << " "; print(tail...); } template void println(const T& ... values){ print(values...); cout << endl; } template void eprint(const H& head){ cerr << head; } template void eprint(const H& head, const T& ... tail){ cerr << head << " "; eprint(tail...); } template void eprintln(const T& ... values){ eprint(values...); cerr << endl; } string operator "" _s (const char* str, size_t size){ return move(string(str)); } constexpr Integer my_pow(Integer x, Integer k, Integer z=1){return k==0 ? z : k==1 ? z*x : (k&1) ? my_pow(x*x,k>>1,z*x) : my_pow(x*x,k>>1,z);} constexpr Integer my_pow_mod(Integer x, Integer k, Integer M, Integer z=1){return k==0 ? z%M : k==1 ? z*x%M : (k&1) ? my_pow_mod(x*x%M,k>>1,M,z*x%M) : my_pow_mod(x*x%M,k>>1,M,z);} constexpr unsigned long long operator "" _ten (unsigned long long value){ return my_pow(10,value); } inline int k_bit(Integer x, int k){return (x>>k)&1;} //0-indexed mt19937 mt(chrono::duration_cast(chrono::steady_clock::now().time_since_epoch()).count()); template string join(const vector& v, const string& sep){ stringstream ss; for(int i=0; i0) ss << sep; ss << v[i]; } return ss.str(); } } void dfs1(vector>& G, vector>& p, vector& d, int pos, int last){ p[pos][0] = last; d[pos] = d[last]+1; for(auto& next : G[pos]){ if(next == last) continue; dfs1(G,p,d, next,pos); } } int dfs2(vector>& par, vector& kado, vector& max_kado, int pos){ if(max_kado[pos] != -1) return max_kado[pos]; if(kado[pos] == 0){ return max_kado[pos] = 0; } int k = 0; while((1<c) || (a>b&&b>& par, vector& d, int u, int v){ if(d[u] < d[v]) swap(u,v); int dd = d[u] - d[v]; for(int k=0; dd; k++, dd>>=1){ if(dd&1){ u = par[u][k]; } } if(u==v){ return u; } for(int k=par[u].size()-1; k>=0; k--){ if(par[u][k] != par[v][k]){ u = par[u][k]; v = par[v][k]; } } return par[u][0]; } int main(){ int n; scanf("%d", &n); vector a(n); for(int i=0; i> G(n); for(int i=0; i> par(n, vector(log_n)); vector depth(n,-1); vector kado(n,0); dfs1(G,par,depth, 0,0); for(int i=0; i max_kado(n, -1); for(int i=0; i>=1){ if(dd_&1){ qv = par[qv][k]; } } if( max_kado[u] > dd-2 && is_kadomatsu(a[pu], a[u], a[v]) && is_kadomatsu(a[u], a[v], a[qv]) ){ puts("YES"); }else{ puts("NO"); } }else{ if(max_kado[u] <= depth[u] - depth[p] - 2){ puts("NO"); continue; } if(max_kado[v] <= depth[v] - depth[p] - 2){ puts("NO"); continue; } int qu = par[u][0]; int qv = par[v][0]; int pu = u; int pv = v; for(int k=0,dd_=depth[u]-depth[p]-1; dd_; k++, dd_>>=1){ if(dd_&1){ pu = par[pu][k]; } } for(int k=0,dd_=depth[v]-depth[p]-1; dd_; k++, dd_>>=1){ if(dd_&1){ pv = par[pv][k]; } } if( is_kadomatsu(a[qu],a[u],a[v]) && is_kadomatsu(a[u],a[v],a[qv]) && is_kadomatsu(a[pu],a[p],a[pv]) ){ puts("YES"); }else{ puts("NO"); } } } return 0; }