#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 2e5 + 5;
void setIO() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
int n, r, q, dp[501][maxn], i;
short home[maxn];
vector<vector<int> > graph;
void dfs(int u) {
dp[home[u]][u]++;
for(int &v : graph[u]) {
dfs(v);
for(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(i=2; i<=n; i++) {
int p;
cin >> p >> home[i];
graph[p].push_back(i);
}
if(r <= 500) {
dfs(1);
vector<vector<int> > by_home(r+1);
for(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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
6488 KB |
Output is correct |
3 |
Correct |
3 ms |
14680 KB |
Output is correct |
4 |
Correct |
6 ms |
20824 KB |
Output is correct |
5 |
Correct |
7 ms |
21080 KB |
Output is correct |
6 |
Correct |
30 ms |
130376 KB |
Output is correct |
7 |
Correct |
21 ms |
26456 KB |
Output is correct |
8 |
Correct |
30 ms |
77912 KB |
Output is correct |
9 |
Runtime error |
26 ms |
131072 KB |
Execution killed with signal 9 |
10 |
Correct |
90 ms |
50608 KB |
Output is correct |
11 |
Correct |
91 ms |
54704 KB |
Output is correct |
12 |
Runtime error |
30 ms |
131072 KB |
Execution killed with signal 9 |
13 |
Correct |
228 ms |
90816 KB |
Output is correct |
14 |
Correct |
133 ms |
49580 KB |
Output is correct |
15 |
Runtime error |
31 ms |
131072 KB |
Execution killed with signal 9 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
37 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Correct |
1322 ms |
104576 KB |
Output is correct |
3 |
Runtime error |
38 ms |
131072 KB |
Execution killed with signal 9 |
4 |
Incorrect |
7 ms |
4172 KB |
Unexpected end of file - int32 expected |
5 |
Incorrect |
7 ms |
4568 KB |
Unexpected end of file - int32 expected |
6 |
Incorrect |
9 ms |
4976 KB |
Unexpected end of file - int32 expected |
7 |
Incorrect |
17 ms |
5972 KB |
Unexpected end of file - int32 expected |
8 |
Incorrect |
17 ms |
8120 KB |
Unexpected end of file - int32 expected |
9 |
Incorrect |
27 ms |
10316 KB |
Unexpected end of file - int32 expected |
10 |
Incorrect |
35 ms |
12088 KB |
Unexpected end of file - int32 expected |
11 |
Incorrect |
43 ms |
11224 KB |
Unexpected end of file - int32 expected |
12 |
Incorrect |
35 ms |
12808 KB |
Unexpected end of file - int32 expected |
13 |
Incorrect |
35 ms |
12264 KB |
Unexpected end of file - int32 expected |
14 |
Incorrect |
37 ms |
12504 KB |
Unexpected end of file - int32 expected |
15 |
Incorrect |
37 ms |
13528 KB |
Unexpected end of file - int32 expected |
16 |
Incorrect |
37 ms |
13436 KB |
Unexpected end of file - int32 expected |
17 |
Incorrect |
38 ms |
13536 KB |
Unexpected end of file - int32 expected |