#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
2 ms |
208 KB |
Output is correct |
4 |
Correct |
2 ms |
208 KB |
Output is correct |
5 |
Correct |
5 ms |
336 KB |
Output is correct |
6 |
Correct |
18 ms |
1488 KB |
Output is correct |
7 |
Correct |
21 ms |
464 KB |
Output is correct |
8 |
Correct |
21 ms |
768 KB |
Output is correct |
9 |
Correct |
48 ms |
7504 KB |
Output is correct |
10 |
Correct |
53 ms |
1872 KB |
Output is correct |
11 |
Correct |
70 ms |
2204 KB |
Output is correct |
12 |
Correct |
111 ms |
10672 KB |
Output is correct |
13 |
Correct |
113 ms |
2272 KB |
Output is correct |
14 |
Correct |
66 ms |
2500 KB |
Output is correct |
15 |
Correct |
137 ms |
46104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
521 ms |
18556 KB |
Output is correct |
2 |
Correct |
598 ms |
7248 KB |
Output is correct |
3 |
Correct |
865 ms |
46356 KB |
Output is correct |
4 |
Correct |
493 ms |
91484 KB |
Output is correct |
5 |
Runtime error |
68 ms |
131072 KB |
Execution killed with signal 9 |
6 |
Runtime error |
61 ms |
131072 KB |
Execution killed with signal 9 |
7 |
Runtime error |
62 ms |
131072 KB |
Execution killed with signal 9 |
8 |
Runtime error |
66 ms |
131072 KB |
Execution killed with signal 9 |
9 |
Runtime error |
88 ms |
131072 KB |
Execution killed with signal 9 |
10 |
Runtime error |
78 ms |
131072 KB |
Execution killed with signal 9 |
11 |
Runtime error |
95 ms |
131072 KB |
Execution killed with signal 9 |
12 |
Runtime error |
80 ms |
131072 KB |
Execution killed with signal 9 |
13 |
Runtime error |
83 ms |
131072 KB |
Execution killed with signal 9 |
14 |
Runtime error |
93 ms |
131072 KB |
Execution killed with signal 9 |
15 |
Runtime error |
92 ms |
131072 KB |
Execution killed with signal 9 |
16 |
Runtime error |
98 ms |
131072 KB |
Execution killed with signal 9 |
17 |
Runtime error |
95 ms |
131072 KB |
Execution killed with signal 9 |