#include<bits/stdc++.h>
//#include<bits/extc++.h>
#define fi first
#define se second
//#define int long long
using namespace std;
//using namespace __gnu_pbds;
using db=double;
using ll=int64_t;
using sll=__int128;
using lb=long double;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n,r,q; vector<vector<int>>adj; vector<vector<int>>h; vector<int>a;
int timer=0; vector<int>in; vector<int>out; vector<vector<int>>v;
void dfs(int node, int fa){
timer++; in[node]=timer; v[a[node]].push_back(timer);
for(auto x:adj[node]){
if(x!=fa)dfs(x,node);
}
out[node]=timer;
}
int32_t main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); srand(time(0));
cin>>n>>r>>q; adj.resize(n+1); h.resize(r+1); a.resize(n+1);
for(int i=1; i<=n; i++){
if(i!=1){
int fa; cin>>fa; adj[i].push_back(fa); adj[fa].push_back(i);
}
int s; cin>>s; h[s].push_back(i); a[i]=s;
}
in.resize(n+1); out.resize(n+1); v.resize(r+1); dfs(1,0);
while(q--){
int r1,r2; cin>>r1>>r2; int ans=0;
for(auto x:h[r1]){
int end=upper_bound(v[r2].begin(),v[r2].end(),out[x])-v[r2].begin();
int st=lower_bound(v[r2].begin(),v[r2].end(),in[x])-v[r2].begin(); ans+=end-st;
}
cout<<ans<<endl;
}
}
/*
Overhead the albatross hangs motionless upon the air
And deep beneath the rolling waves in labyrinths of coral caves
The echo of a distant time comes willowing across the sand
And everything is green and submarine
And no one showed us to the land
And no one knows the wheres or whys
But something stirs and something tries
And starts to climb towards the light
*/
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |