#include "bits/stdc++.h"
using namespace std;
#define ll long long
const int areTests = 0, N = 2e5 + 20;

int n, p[N], q, u, d, dp[N], st[N], ed[N], tym, a[N];
vector <int> adj[N], idx[N];

void dfs(int node, int edges) {
	st[node] = ++tym;
	dp[node] = edges;
	idx[edges].push_back(tym);
	for(int child: adj[node]) {
		dfs(child, edges + 1);
	}
	ed[node] = tym;
}

void run_test(int testcase) {
	cin >> n;
	for(int i = 2; i <= n; i++) {
		cin >> p[i];
		adj[p[i]].push_back(i);
	}

	dfs(1, 0);
	for(int i = 0; i <= n; i++) {
		sort(idx[i].begin(), idx[i].end());
	}

	cin >> q;
	while(q--) {
		cin >> u >> d;

		if(dp[u] > d) {
			cout << "0";
		} else {
			cout << upper_bound(idx[d].begin(), idx[d].end(), ed[u]) - lower_bound(idx[d].begin(), idx[d].end(), st[u]);
		}
		cout << "\n";
	}
}

int main() {
	ios::sync_with_stdio(0);
	#ifndef DUSH1729
	cin.tie(0);
	#endif
	cout << fixed << setprecision(10);

	int t = 1;
	if(areTests) cin >> t;
	for(int i = 1; i <= t; i++) {
		run_test(i);
	}
}
#include<bits/stdc++.h>
using namespace std;

#define ff first
#define ss second
#define PI acos(-1)
#define pb push_back
#define int long long
#define ld long double
#define sp fixed<<setprecision
#define bp __builtin_popcountll
#define all(x) x.begin(),x.end()
#define pii pair<long long,long long>
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

const int M = (1e9)+7;
const int N = 200005;

vector<int> adj[N];
int vis[N],a[N],b[N];
int curr = 0, need = 0;

void dfs(int c)
{
    curr += a[c];
    need += b[c];
    vis[c]++;
    for(int ne:adj[c])if(!vis[ne])dfs(ne);
}

signed main()
{
    FAST
    int tc=1;
    //cin>>tc;
    for(int ti=1;ti<=tc;ti++)
    {
        int n,m;
        cin>>n>>m;
        for(int i=1;i<=n;i++)cin>>a[i];
        for(int i=1;i<=n;i++)cin>>b[i];
        for(int i=0;i<m;i++)
        {
            int x,y;
            cin>>x>>y;
            adj[x].pb(y);
            adj[y].pb(x);
        }
        int ok = 1;
        for(int i=1;i<=n;i++)
        {
            if(vis[i])continue;
            need = 0; curr = 0;
            dfs(i);
            ok &= (need == curr);
        }
        cout<<(ok ? "Yes" : "No")<<endl;
    }
    return 0;
}
