#include <bits/stdc++.h>
#define X first
#define Y second
typedef long long ll;
using namespace std;
const int MOD = 1e9 + 7;
const ll INF = 1e18;
int n, r, q, cv, q1, q2;
int sq;
vector<int> v[200002];
unordered_map<int, ll> m[25002];
int w[200002];
int kol[200002];
int kol2[200002];
vector<int> g[200002];
int t;
int d1[200002];
int d2[200002];
void dfs(int x) {
for (int i = 0; i < v[x].size(); i ++) {
int sus = v[x][i];
dfs(sus);
kol[x] += kol[sus];
}
//cout << "mapa od " << x << ", " << t << " += " << kol[x] << endl;
m[w[x]][t] += kol[x];
}
void rek(int x, int y) {
y += kol2[x];
m[t][w[x]] += y;
for (int i = 0; i < v[x].size(); i ++) {
rek(v[x][i], y);
}
}
int tren;
void ord(int x) {
tren ++;
d1[x] = tren;
for (int i = 0; i < v[x].size(); i ++) {
ord(v[x][i]);
}
d2[x] = tren;
//cout << x << ": " << d1[x] << " " << d2[x] << endl;
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> r >> q;
sq = sqrt(n);
cin >> w[1];
g[w[1]].push_back(1);
for (int i = 2; i <= n; i ++) {
cin >> cv >> w[i];
g[w[i]].push_back(i);
v[cv].push_back(i);
}
ord(1);
for (int i = 1; i <= r; i ++) {
if (g[i].size() > sq) {
memset(kol, 0, sizeof kol);
memset(kol2, 0, sizeof kol2);
t = i;
for (int j = 0; j < g[i].size(); j ++) {
kol[g[i][j]] = 1;
kol2[g[i][j]] = 1;
}
dfs(1);
rek(1, 0);
}
}
for (int i = 0; i < q; i ++) {
cin >> q1 >> q2;
if (g[q1].size() > sq && g[q2].size() > sq) {
m[q1][q2] /= 2;
}
if (g[q1].size() <= sq && g[q2].size() <= sq) {
assert(0);
}
cout << max(0LL, m[q1][q2]) << endl;
}
return 0;
}
Compilation message
regions.cpp: In function 'void dfs(int)':
regions.cpp:23:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < v[x].size(); i ++) {
~~^~~~~~~~~~~~~
regions.cpp: In function 'void rek(int, int)':
regions.cpp:36:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < v[x].size(); i ++) {
~~^~~~~~~~~~~~~
regions.cpp: In function 'void ord(int)':
regions.cpp:45:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < v[x].size(); i ++) {
~~^~~~~~~~~~~~~
regions.cpp: In function 'int main()':
regions.cpp:69:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (g[i].size() > sq) {
~~~~~~~~~~~~^~~~
regions.cpp:73:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < g[i].size(); j ++) {
~~^~~~~~~~~~~~~
regions.cpp:85:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (g[q1].size() > sq && g[q2].size() > sq) {
~~~~~~~~~~~~~^~~~
regions.cpp:85:41: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (g[q1].size() > sq && g[q2].size() > sq) {
~~~~~~~~~~~~~^~~~
regions.cpp:89:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (g[q1].size() <= sq && g[q2].size() <= sq) {
~~~~~~~~~~~~~^~~~~
regions.cpp:89:42: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (g[q1].size() <= sq && g[q2].size() <= sq) {
~~~~~~~~~~~~~^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
29 ms |
25592 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Runtime error |
26 ms |
22260 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Runtime error |
26 ms |
22392 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
4 |
Runtime error |
26 ms |
22264 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
5 |
Runtime error |
26 ms |
22300 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
6 |
Runtime error |
26 ms |
22392 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
7 |
Runtime error |
26 ms |
22392 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
8 |
Runtime error |
32 ms |
22648 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
9 |
Runtime error |
38 ms |
23144 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
10 |
Runtime error |
29 ms |
23288 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
11 |
Runtime error |
32 ms |
23800 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
12 |
Runtime error |
35 ms |
24628 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
13 |
Runtime error |
35 ms |
23996 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
14 |
Runtime error |
55 ms |
28408 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
15 |
Runtime error |
51 ms |
33320 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
265 ms |
34936 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Runtime error |
187 ms |
31992 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Runtime error |
156 ms |
37880 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
4 |
Runtime error |
47 ms |
25080 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
5 |
Runtime error |
49 ms |
27640 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
6 |
Runtime error |
935 ms |
90684 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
7 |
Runtime error |
1314 ms |
118792 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
8 |
Runtime error |
2402 ms |
131072 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
9 |
Runtime error |
106 ms |
38420 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
10 |
Runtime error |
2398 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
11 |
Runtime error |
126 ms |
37624 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
12 |
Runtime error |
318 ms |
50040 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
13 |
Runtime error |
253 ms |
50360 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
14 |
Runtime error |
2116 ms |
111752 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
15 |
Runtime error |
261 ms |
61356 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
16 |
Runtime error |
225 ms |
72556 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
17 |
Runtime error |
1524 ms |
131072 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |