#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
vector <vector <int>> adj;
vector <int> col;
vector <map <int, int>> freq;
vector <vector <int>> regions;
void dfs(int s, int p) {
freq[s][col[s]] = 1;
for (auto i : adj[s]) {
if (i == p)
continue;
dfs(i, s);
for (auto j : freq[i])
freq[s][j.first] += j.second;
}
}
int main() {
int n, r, q;
cin >> n >> r >> q;
col = vector <int> (n+1);
adj.resize(n+1);
regions.resize(r+1);
freq.resize(n+1);
cin >> col[1];
regions[col[1]].push_back(1);
for (int i = 2; i <= n; i++) {
int p, c;
cin >> p >> c;
adj[i].push_back(p);
adj[p].push_back(i);
col[i] = c;
regions[c].push_back(i);
}
dfs(1, 1);
while (q--) {
int r1, r2;
cin >> r1 >> r2;
int ans = 0;
for (auto i : regions[r1]) {
ans += freq[i][r2];
}
cout << ans << endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
436 KB |
Output is correct |
3 |
Correct |
1 ms |
708 KB |
Output is correct |
4 |
Correct |
2 ms |
484 KB |
Output is correct |
5 |
Correct |
4 ms |
924 KB |
Output is correct |
6 |
Correct |
23 ms |
7720 KB |
Output is correct |
7 |
Correct |
21 ms |
2932 KB |
Output is correct |
8 |
Correct |
30 ms |
5840 KB |
Output is correct |
9 |
Correct |
197 ms |
68940 KB |
Output is correct |
10 |
Correct |
115 ms |
20692 KB |
Output is correct |
11 |
Correct |
321 ms |
54720 KB |
Output is correct |
12 |
Runtime error |
205 ms |
131072 KB |
Execution killed with signal 9 |
13 |
Correct |
391 ms |
53412 KB |
Output is correct |
14 |
Correct |
923 ms |
109472 KB |
Output is correct |
15 |
Runtime error |
201 ms |
131072 KB |
Execution killed with signal 9 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
226 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Runtime error |
350 ms |
131072 KB |
Execution killed with signal 9 |
3 |
Runtime error |
215 ms |
131072 KB |
Execution killed with signal 9 |
4 |
Runtime error |
227 ms |
131072 KB |
Execution killed with signal 9 |
5 |
Runtime error |
219 ms |
131072 KB |
Execution killed with signal 9 |
6 |
Runtime error |
269 ms |
131072 KB |
Execution killed with signal 9 |
7 |
Runtime error |
243 ms |
131072 KB |
Execution killed with signal 9 |
8 |
Runtime error |
237 ms |
131072 KB |
Execution killed with signal 9 |
9 |
Runtime error |
260 ms |
131072 KB |
Execution killed with signal 9 |
10 |
Runtime error |
256 ms |
131072 KB |
Execution killed with signal 9 |
11 |
Runtime error |
290 ms |
131072 KB |
Execution killed with signal 9 |
12 |
Runtime error |
275 ms |
131072 KB |
Execution killed with signal 9 |
13 |
Runtime error |
272 ms |
131072 KB |
Execution killed with signal 9 |
14 |
Runtime error |
291 ms |
131072 KB |
Execution killed with signal 9 |
15 |
Runtime error |
275 ms |
131072 KB |
Execution killed with signal 9 |
16 |
Runtime error |
256 ms |
131072 KB |
Execution killed with signal 9 |
17 |
Runtime error |
273 ms |
131072 KB |
Execution killed with signal 9 |