#include #include #include #include #include // 任意長整数型 using Bint = boost::multiprecision::cpp_int; // 仮数部が10進数で1024桁の浮動小数点数型(TLEしたら小さくする) using Real = boost::multiprecision::number>; using Rat = boost::rational; using namespace std; using ll = long long; using pll = pair; #define rep(i, s, t) for (ll i = s; i < (ll)(t); i++) #define rrep(i, s, t) for (ll i = (ll)(t) - 1; i >= (ll)(s); i--) #define all(x) begin(x), end(x) #define TT template TT using vec = vector; TT using vvec = vec>; TT using vvvec = vec>; TT using minheap = priority_queue, greater>; TT using maxheap = priority_queue; template bool chmin(T1 &x, T2 y) { return x > y ? (x = y, true) : false; } template bool chmax(T1 &x, T2 y) { return x < y ? (x = y, true) : false; } TT auto prev_itr(vec &A, T x) { // must sorted auto res = lower_bound(all(A), x); if (res == A.begin()) return A.end(); else return --res; } TT auto next_itr(vec &A, T x) { // must sorted return upper_bound(all(A), x) - A.begin(); } struct io_setup { io_setup() { ios::sync_with_stdio(false); std::cin.tie(nullptr); cout << fixed << setprecision(15); } } io_setup; int main() { ll n; cin >> n; vec> g(n); rep(i, 1, n) { int p; cin >> p; p--; g[i].push_back(p); g[p].push_back(i); } vec col(n, 0); rep(i, 1, n) { char c; cin >> c; if (c == '#') col[i] = 1; //変える必要がある。 } ll k; cin >> k; vec U(k), V(k); rep(i, 0, k) { cin >> U[i] >> V[i]; U[i]--, V[i]--; } vec> rev(n); rep(i, 0, k) { rev[U[i]].insert(i); rev[V[i]].insert(i); } vec base(n, -1); auto path_xor = [&](int v, int id1, int id2) { // id2を変更する。 int nl, nr; if (U[id1] == v) nl = V[id1]; else nl = U[id1]; if (U[id2] == v) nr = V[id2]; else nr = U[id2]; rev[U[id2]].erase(id2); rev[V[id2]].erase(id2); U[id2] = nl; V[id2] = nr; if (nl == nr) return; rev[U[id2]].insert(id2); rev[V[id2]].insert(id2); return; }; set base_used; auto dfs = [&](auto f, int v, int p = -1) -> void { for(int to : g[v]) if(to != p) { f(f, to, v); } int use = -1; for(int id : rev[v]) if(base_used.count(id) == 0) use = id; if(use == -1) return; base_used.insert(use); vec tar; for(int id : rev[v]) if(id != use) tar.push_back(id); sort(all(tar)); tar.erase(unique(all(tar)), tar.end()); for(int id : tar) path_xor(v, use, id); /* vec era; for(int di : rev[v]) era.push_back(di); for(int di : era) path_xor(v, use, di); */ base[v] = use; return; }; dfs(dfs, 0); vec imos(n, 0); set already; rep(i, 0, n) if(base[i] != -1 && col[i] == 1 && !already.count(base[i])) { int id = base[i]; already.insert(id); imos[U[id]]++; imos[V[id]]++; } auto dfs2 = [&](auto f, int v, int p = -1) -> bool { for(int to : g[v]) if(to != p) { if(f(f, to, v) == false) { return false; } } if(p != -1) imos[p] += imos[v]; if((imos[v] % 2) != col[v]) { return false; } else return true; }; if(dfs2(dfs2, 0)) cout << "Yes" << endl; else cout << "No" << endl; }