#include <bits/stdc++.h>
using namespace std;
int n, r, q, p, h[200001], r1, r2, timer, tin[200001], tout[200001];
vector<int> adj[200001];
vector<int> v1[200001], v2[25001];
void dfs(int u, int p) {
tin[u] = timer++;
for (auto v: adj[u]) if (v != p) dfs(v, u);
tout[u] = timer;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> r >> q;
cin >> h[1];
v1[h[1]].push_back(1);
for (int i = 2; i <= n; i++) {
cin >> p >> h[i];
adj[p].push_back(i);
v1[h[i]].push_back(i);
}
dfs(1, 0);
for (int i = 1; i <= n; i++) v2[h[i]].push_back(tin[i]);
for (int i = 1; i <= r; i++) sort(v2[i].begin(), v2[i].end());
for (int i = 0; i < q; i++) {
int ans = 0;
cin >> r1 >> r2;
for (auto e: v1[r1]) ans += lower_bound(v2[r2].begin(), v2[r2].end(), tout[e]) - lower_bound(v2[r2].begin(), v2[r2].end(), tin[e]);
cout << ans << endl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |