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>
using namespace std;
#ifdef DEBUG
auto&operator<<(auto &o, pair<auto, auto> p) {o << "(" << p.first << ", " << p.second << ")"; return o;}
auto operator<<(auto &o, auto x)->decltype(x.end(), o) {o<<"{"; for(auto e : x) o<<e<<", "; return o<<"}";}
#define debug(X) cerr << "["#X"]: " << X << '\n';
#else
#define debug(X) ;
#endif
#define ll long long
#define all(v) (v).begin(), (v).end()
#define FOR(i,l,r) for(int i=(l);i<=(r);++i)
#define ROF(i,r,l) for(int i=(r);i>=(l);--i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) int(x.size())
#define fi first
#define se second
#define eb emplace_back
int main () {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, r, q;
cin >> n >> r >> q;
vector<int> h(n);
cin >> h[0];
h[0]--;
vector G(n, vector(0, 0));
FOR(i, 1, n-1) {
int p;
cin >> p >> h[i];
p--;
h[i]--;
G[p].eb(i);
G[i].eb(p);
}
auto merge = [&](vector<int> &a, vector<int> &b) {
REP(i, r) a[i] += b[i];
return;
};
vector ans(r, vector(r, 0));
function<vector<int>(int, int)> dfs = [&](int v, int p) {
vector<int> cnt(r);
for(auto w : G[v]) {
if (w == p) continue;
vector<int> b = dfs(w, v);
merge(cnt, b);
}
REP(i, r) ans[h[v]][i] += cnt[i];
cnt[h[v]]++;
return cnt;
};
dfs(0, -1);
REP(i, q) {
int r1, r2;
cin >> r1 >> r2;
r1--; r2--;
cout << ans[r1][r2] << '\n';
cout.flush();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |