#include <bits/stdc++.h>
using namespace std;
vector<int> adj[300005], reg[25005], regst[25005];
int st[300005], en[300005], timer = 0;
void dfs(int i, int p) {
st[i] = timer;
timer++;
for (int j : adj[i]) {
if (j == p) {
continue;
}
dfs(j, i);
}
en[i] = timer;
}
void solve() {
int n, r, q;
cin >> n >> r >> q;
for (int i = 1; i <= n; i++) {
int s, h;
if (i == 1) {
cin >> h;
reg[h].push_back(i);
} else {
cin >> s >> h;
reg[h].push_back(i);
adj[s].push_back(i);
adj[i].push_back(s);
}
}
dfs(1, -1);
for (int i = 1; i <= r; i++) {
for (int j : reg[i]) {
regst[i].push_back(st[j]);
}
sort(regst[i].begin(), regst[i].end());
}
while(q--) {
int a, b, ans = 0;
cin >> a >> b;
for (int i : reg[a]) {
int l, r;
l = upper_bound(regst[b].begin(), regst[b].end(), st[i]) - regst[b].begin();
r = lower_bound(regst[b].begin(), regst[b].end(), en[i]) - regst[b].begin();
ans += r-l;
}
cout << ans << endl;
}
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
t = 1;
while(t--) {
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10840 KB |
Output is correct |
2 |
Correct |
2 ms |
10840 KB |
Output is correct |
3 |
Correct |
2 ms |
10840 KB |
Output is correct |
4 |
Correct |
4 ms |
10684 KB |
Output is correct |
5 |
Correct |
5 ms |
10840 KB |
Output is correct |
6 |
Correct |
12 ms |
10840 KB |
Output is correct |
7 |
Correct |
17 ms |
10840 KB |
Output is correct |
8 |
Correct |
15 ms |
10840 KB |
Output is correct |
9 |
Correct |
22 ms |
11096 KB |
Output is correct |
10 |
Correct |
45 ms |
11096 KB |
Output is correct |
11 |
Correct |
75 ms |
11352 KB |
Output is correct |
12 |
Correct |
90 ms |
11864 KB |
Output is correct |
13 |
Correct |
118 ms |
11864 KB |
Output is correct |
14 |
Correct |
191 ms |
12120 KB |
Output is correct |
15 |
Correct |
198 ms |
14424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
8002 ms |
14680 KB |
Time limit exceeded |
2 |
Execution timed out |
8002 ms |
14424 KB |
Time limit exceeded |
3 |
Execution timed out |
8029 ms |
16276 KB |
Time limit exceeded |
4 |
Correct |
184 ms |
12376 KB |
Output is correct |
5 |
Correct |
266 ms |
13908 KB |
Output is correct |
6 |
Correct |
1135 ms |
13400 KB |
Output is correct |
7 |
Correct |
1397 ms |
14424 KB |
Output is correct |
8 |
Correct |
959 ms |
18628 KB |
Output is correct |
9 |
Correct |
1909 ms |
19024 KB |
Output is correct |
10 |
Correct |
3480 ms |
23376 KB |
Output is correct |
11 |
Correct |
3394 ms |
20604 KB |
Output is correct |
12 |
Correct |
4274 ms |
19288 KB |
Output is correct |
13 |
Correct |
4712 ms |
20148 KB |
Output is correct |
14 |
Correct |
6467 ms |
20408 KB |
Output is correct |
15 |
Correct |
7194 ms |
23632 KB |
Output is correct |
16 |
Correct |
5221 ms |
29008 KB |
Output is correct |
17 |
Correct |
7910 ms |
27728 KB |
Output is correct |