#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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2548 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
2 ms |
2392 KB |
Output is correct |
5 |
Correct |
4 ms |
2392 KB |
Output is correct |
6 |
Correct |
8 ms |
2392 KB |
Output is correct |
7 |
Correct |
13 ms |
2392 KB |
Output is correct |
8 |
Correct |
17 ms |
2648 KB |
Output is correct |
9 |
Incorrect |
2 ms |
2648 KB |
Unexpected end of file - int32 expected |
10 |
Incorrect |
3 ms |
2904 KB |
Unexpected end of file - int32 expected |
11 |
Incorrect |
3 ms |
3416 KB |
Unexpected end of file - int32 expected |
12 |
Incorrect |
5 ms |
3536 KB |
Unexpected end of file - int32 expected |
13 |
Incorrect |
5 ms |
3392 KB |
Unexpected end of file - int32 expected |
14 |
Incorrect |
5 ms |
3928 KB |
Unexpected end of file - int32 expected |
15 |
Incorrect |
8 ms |
4696 KB |
Unexpected end of file - int32 expected |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
18 ms |
6488 KB |
Unexpected end of file - int32 expected |
2 |
Incorrect |
15 ms |
6116 KB |
Unexpected end of file - int32 expected |
3 |
Incorrect |
19 ms |
7624 KB |
Unexpected end of file - int32 expected |
4 |
Incorrect |
7 ms |
3992 KB |
Unexpected end of file - int32 expected |
5 |
Incorrect |
6 ms |
4696 KB |
Unexpected end of file - int32 expected |
6 |
Incorrect |
10 ms |
4952 KB |
Unexpected end of file - int32 expected |
7 |
Incorrect |
12 ms |
5940 KB |
Unexpected end of file - int32 expected |
8 |
Incorrect |
19 ms |
8040 KB |
Unexpected end of file - int32 expected |
9 |
Incorrect |
37 ms |
10772 KB |
Unexpected end of file - int32 expected |
10 |
Incorrect |
33 ms |
12196 KB |
Unexpected end of file - int32 expected |
11 |
Incorrect |
40 ms |
11072 KB |
Unexpected end of file - int32 expected |
12 |
Incorrect |
37 ms |
13244 KB |
Unexpected end of file - int32 expected |
13 |
Incorrect |
44 ms |
12592 KB |
Unexpected end of file - int32 expected |
14 |
Incorrect |
38 ms |
12992 KB |
Unexpected end of file - int32 expected |
15 |
Incorrect |
43 ms |
13868 KB |
Unexpected end of file - int32 expected |
16 |
Incorrect |
35 ms |
13652 KB |
Unexpected end of file - int32 expected |
17 |
Incorrect |
36 ms |
13816 KB |
Unexpected end of file - int32 expected |