#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, timer = 0, in[maxn], out[maxn], r1, r2, ans;
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];
}
}
void dfs2(int u) {
in[u] = timer++;
for(int &v : graph[u])
dfs2(v);
out[u] = timer;
}
bool anc(int u, int v) { return (in[u] <= in[v] && out[u] >= out[v]); }
int32_t main() {
setIO();
cin >> n >> r >> q;
graph.resize(n+1);
vector<int> cnt(r+1);
cin >> home[1];
cnt[home[1]]++;
for(i=2; i<=n; i++) {
int p;
cin >> p >> home[i];
cnt[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--) {
cin >> r1 >> r2;
for(int &x : by_home[r1])
ans += dp[r2][x];
cout << ans << '\n';
cout.flush();
}
return 0;
}
int mx = 0;
for(i=1; i<=n; i++) mx = max(mx, cnt[i]);
if(mx <= 500) {
dfs2(1);
vector<vector<int> > by_home(r+1);
for(i=1; i<=n; i++)
by_home[home[i]].push_back(i);
while(q--) {
cin >> r1 >> r2;
ans = 0;
for(int &u : by_home[r1])
for(int &v : by_home[r2])
ans += anc(u, v);
cout << ans << '\n';
cout.flush();
}
return 0;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
4440 KB |
Output isn't correct |
2 |
Incorrect |
2 ms |
8536 KB |
Output isn't correct |
3 |
Incorrect |
3 ms |
12632 KB |
Output isn't correct |
4 |
Incorrect |
4 ms |
16984 KB |
Output isn't correct |
5 |
Incorrect |
8 ms |
29272 KB |
Output isn't correct |
6 |
Incorrect |
30 ms |
130384 KB |
Output isn't correct |
7 |
Incorrect |
17 ms |
24408 KB |
Output isn't correct |
8 |
Incorrect |
27 ms |
83800 KB |
Output isn't correct |
9 |
Runtime error |
26 ms |
131072 KB |
Execution killed with signal 9 |
10 |
Incorrect |
89 ms |
50692 KB |
Output isn't correct |
11 |
Incorrect |
79 ms |
56664 KB |
Output isn't correct |
12 |
Runtime error |
27 ms |
131072 KB |
Execution killed with signal 9 |
13 |
Incorrect |
190 ms |
94592 KB |
Output isn't correct |
14 |
Incorrect |
131 ms |
48068 KB |
Output isn't correct |
15 |
Runtime error |
30 ms |
131072 KB |
Execution killed with signal 9 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
35 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Incorrect |
1403 ms |
108496 KB |
Output isn't correct |
3 |
Runtime error |
38 ms |
131072 KB |
Execution killed with signal 9 |
4 |
Incorrect |
7 ms |
3928 KB |
Unexpected end of file - int32 expected |
5 |
Incorrect |
7 ms |
4696 KB |
Unexpected end of file - int32 expected |
6 |
Incorrect |
9 ms |
5148 KB |
Unexpected end of file - int32 expected |
7 |
Incorrect |
18 ms |
5924 KB |
Unexpected end of file - int32 expected |
8 |
Incorrect |
24 ms |
8424 KB |
Unexpected end of file - int32 expected |
9 |
Incorrect |
26 ms |
10772 KB |
Unexpected end of file - int32 expected |
10 |
Incorrect |
42 ms |
12128 KB |
Unexpected end of file - int32 expected |
11 |
Incorrect |
37 ms |
10996 KB |
Unexpected end of file - int32 expected |
12 |
Incorrect |
39 ms |
12984 KB |
Unexpected end of file - int32 expected |
13 |
Incorrect |
37 ms |
12828 KB |
Unexpected end of file - int32 expected |
14 |
Incorrect |
37 ms |
12704 KB |
Unexpected end of file - int32 expected |
15 |
Incorrect |
37 ms |
13632 KB |
Unexpected end of file - int32 expected |
16 |
Incorrect |
37 ms |
13848 KB |
Unexpected end of file - int32 expected |
17 |
Incorrect |
42 ms |
14256 KB |
Unexpected end of file - int32 expected |