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;
#define fast ios::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
#define f first
#define s second
#define MOD 1000000007
#define LOGN 20
#define MAXN 200005
#define pb push_back
vector<vector<int>> graph;
int ans[505][505], cnt[505][505];
int region[MAXN];
void dfs(int node, int parent) {
for (int i = 0; i <= 500; i++) {
cnt[node][i] = cnt[parent][i];
ans[i][region[node]] += cnt[node][i];
}
cnt[node][region[node]]++;
for (auto u : graph[node]) {
dfs(u, node);
}
}
int main() {
fast
int N, R, Q, S, r1, r2;
cin >> N >> R >> Q >> region[1];
graph = vector<vector<int>>(N+1, vector<int>());
for (int i = 2; i <= N; i++) {
cin >> S >> region[i];
graph[S].pb(i);
}
if (R <= 500) {
dfs(1, 1);
while (Q--) {
cin >> r1 >> r2;
cout << ans[r1][r2] << endl;
}
} else {
while (Q--) {
int x, y;
cin >> x >> y;
cout << x << " " << y << endl;
}
}
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |