#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(),x.end()
const int mxN = 2e5+5000;
void io(){
/* #ifndef ONLINE_JUDGE
freopen("input.in","r",stdin);
freopen("output.out","w",stdout);
#endif*/
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
}
vector<int> adj[mxN];
int region[mxN],dp[mxN][505],answers[505][505];
void dfs(int u){
for(auto it:adj[u]){
dfs(it);
for(int i=0;i<=500;++i){
dp[u][i]+=dp[it][i];
}
++dp[u][region[it]];
}
}
int main(){
io();
int n,r,q,u,man,reg;cin>>n>>r>>q;
cin>>region[1];
for(int i=2;i<=n;++i){
cin>>u>>region[i];
adj[u].pb(i);
}
memset(dp,0,sizeof(dp));
dfs(1);
for(int i=1;i<=n;++i){
for(int j=1;j<=r;++j){
answers[region[i]][j]+=dp[i][j];
}
}
while(q--){
cin>>man>>reg;
cout<<answers[man][reg]<<endl;
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |