#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, i, in[maxn], out[maxn], timer = 0, ans = 0;
short home[maxn], r1, r2;
vector<vector<int> > graph, dp;
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])
dfs(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 <= 300) {
dp.resize(r+1, vector<int>(n+1));
dfs(1);
vector<vector<int> > by_home(r+1);
for(i=1; i<=n; i++)
by_home[home[i]].push_back(i);
while(q--) {
ans = 0;
cin >> r1 >> r2;
for(int &x : by_home[r1])
ans += dp[r2][x];
cout << ans << endl;
}
return 0;
}
if(*max_element(cnt.begin(), cnt.end()) <= 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--) {
ans = 0;
cin >> r1 >> r2;
for(int &u : by_home[r1])
for(int &v : by_home[r2]) ans += anc(u, v);
cout << ans << endl;
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
2 ms |
344 KB |
Output is correct |
5 |
Correct |
3 ms |
344 KB |
Output is correct |
6 |
Correct |
9 ms |
1112 KB |
Output is correct |
7 |
Correct |
12 ms |
1368 KB |
Output is correct |
8 |
Correct |
16 ms |
2136 KB |
Output is correct |
9 |
Correct |
33 ms |
6744 KB |
Output is correct |
10 |
Runtime error |
3 ms |
1624 KB |
Execution killed with signal 11 |
11 |
Correct |
71 ms |
17240 KB |
Output is correct |
12 |
Runtime error |
5 ms |
3160 KB |
Execution killed with signal 11 |
13 |
Runtime error |
7 ms |
2480 KB |
Execution killed with signal 11 |
14 |
Correct |
169 ms |
25760 KB |
Output is correct |
15 |
Correct |
244 ms |
43148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1089 ms |
65144 KB |
Output is correct |
2 |
Correct |
1529 ms |
74584 KB |
Output is correct |
3 |
Correct |
1710 ms |
99228 KB |
Output is correct |
4 |
Runtime error |
7 ms |
3672 KB |
Execution killed with signal 11 |
5 |
Runtime error |
9 ms |
4892 KB |
Execution killed with signal 11 |
6 |
Runtime error |
15 ms |
5992 KB |
Execution killed with signal 11 |
7 |
Runtime error |
21 ms |
7768 KB |
Execution killed with signal 11 |
8 |
Runtime error |
23 ms |
12120 KB |
Execution killed with signal 11 |
9 |
Runtime error |
41 ms |
17508 KB |
Execution killed with signal 11 |
10 |
Runtime error |
38 ms |
20636 KB |
Execution killed with signal 11 |
11 |
Runtime error |
46 ms |
18292 KB |
Execution killed with signal 11 |
12 |
Incorrect |
42 ms |
11128 KB |
Unexpected end of file - int32 expected |
13 |
Incorrect |
38 ms |
10532 KB |
Unexpected end of file - int32 expected |
14 |
Incorrect |
39 ms |
10560 KB |
Unexpected end of file - int32 expected |
15 |
Incorrect |
47 ms |
12164 KB |
Unexpected end of file - int32 expected |
16 |
Incorrect |
42 ms |
11996 KB |
Unexpected end of file - int32 expected |
17 |
Incorrect |
45 ms |
12020 KB |
Unexpected end of file - int32 expected |