This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define pb push_back
#define eb emplace_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define uniq(x) x.erase(unique(all(x)), x.end())
#define rall(x) x.rbegin(), x.rend()
//#define int long long
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 2e5 + 5;
const double eps = 1e-9;
void setIO() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
int n, r, q, home[maxn+1], dp[501][maxn];
vector<vector<int> > graph;
void dfs(int u, int p) {
dp[home[u]][u]++;
for(int &v : graph[u]) {
if(v == p) continue;
dfs(v, u);
for(int i=1; i<=r; i++)
dp[i][u] += dp[i][v];
}
}
int32_t main() {
setIO();
cin >> n >> r >> q;
graph.resize(n+1);
cin >> home[1];
for(int i=2; i<=n; i++) {
int p;
cin >> p >> home[i];
graph[i].push_back(p);
graph[p].push_back(i);
}
if(r <= 500) {
dfs(1, 0);
vector<vector<int> > by_home(r+1);
for(int i=1; i<=n; i++) by_home[home[i]].push_back(i);
while(q--) {
int r1, r2, ans = 0;
cin >> r1 >> r2;
for(int &x : by_home[r1])
ans += dp[r2][x];
cout << ans << '\n';
cout.flush();
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |