#include "bits/stdc++.h"
using namespace std;
#define task ""
#define ll long long
#define endl '\n'
#define fi first
#define se second
#define vall(a) (a).begin(), (a).end()
#define sze(a) (int)a.size()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ep emplace_back
#define pb push_back
#define pf push_front
const ll mod = 1e9 + 7;
const int N = 2e5 + 5;
const ll oo = 1e18;
const int base = 450;
bool START;
int n, r, q, t = 0;
int h[N], p[N], ans[N][450], tin[N], tout[N];
vector<int> arr;
bool nang[N];
vector<int> adj[N], th[N], pos[N];
void euler(int u) {
tin[u] = ++t;
th[h[u]].pb(tin[u]);
pos[h[u]].pb(u);
for (int v : adj[u]) euler(v);
tout[u] = t;
}
void dfs(int u, int hxet, int cnt, int id) {
for (int v : adj[u]) dfs(v, hxet, cnt + (h[u] == hxet), id);
ans[h[u]][id] += cnt;
}
inline void solve() {
euler(1);
for (int i = 1; i <= r; ++i) {
if (sze(pos[i]) > base) {
nang[i] = true;
arr.pb(i);
}
}
for (int i = 0; i < sze(arr); ++i) {
int hh = arr[i];
dfs(1, hh, 0, i);
}
while(q--) {
int r1, r2;
cin >> r1 >> r2;
if (nang[r1]) {
int id = lower_bound(vall(arr), r1) - arr.begin();
cout << ans[r2][id] << endl;
} else {
ll ans = 0;
for (int u : pos[r1]) {
int in = tin[u], out = tout[u];
int l = lower_bound(vall(th[r2]), in) - th[r2].begin();
int r = upper_bound(vall(th[r2]), out) - th[r2].begin();
ans += (r - l);
}
cout << ans << endl;
}
}
}
inline void input() {
cin >> n >> r >> q;
cin >> h[1];
for (int i = 2; i <= n; ++i) {
int p; cin >> p >> h[i];
adj[p].pb(i);
}
return solve();
}
bool END;
int main() {
if(fopen(task ".inp", "r")) {
freopen(task ".inp", "r", stdin);
freopen(task ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
input();
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << 's' << endl;
cerr << "Memory: " << fabs ((&END - &START)) / 1048576.0 << "MB\n";
return 0;
}
Compilation message (stderr)
regions.cpp: In function 'int main()':
regions.cpp:90:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
90 | freopen(task ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
regions.cpp:91:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
91 | freopen(task ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |