#include <bits/stdc++.h>
using namespace std;
using idata = vector<int>;
using iset = set<int>;
using igrid = vector<idata>;
using di = pair<int, int>;
using vd = vector<di>;
const int MAXK = 20;
struct Tree {
igrid roads;
idata preordre, postordre, parents, depth;
igrid eulertour;
igrid parents_2k;
void dfs (int node, int parent, int _depth, int &iOrdre) {
preordre[node] = iOrdre ++;
parents [node] = parent;
depth [node] = _depth;
for (int next : roads[node]) if (next != parent) {
eulertour[node].push_back(iOrdre ++);
dfs(next, node, _depth + 1, iOrdre);
}
postordre[node] = iOrdre ++;
}
Tree (int N, igrid &roads) : roads(roads) {
eulertour.resize(N);
postordre.resize(N);
preordre .resize(N);
parents .resize(N);
depth .resize(N);
int iOrdre = 0;
dfs (0, -1, 0, iOrdre);
parents_2k.resize(N, idata(MAXK, -1));
for (int i = 0; i < N; i ++) parents_2k[i][0] = parents[i];
for (int k = 0; k + 1 < MAXK; k ++) {
for (int i = 0; i < N; i ++) {
if (parents_2k[i][k] == -1) continue ;
parents_2k[i][k + 1] = parents_2k[parents_2k[i][k]][k];
}
}
}
int jump (int node, int k) {
for (int i = 0; i < MAXK; i ++)
if ((1 << i) & k)
node = parents_2k[node][i];
return node;
}
int lca (int a, int b) {
if (depth[a] > depth[b]) swap(a, b);
b = jump(b, depth[b] - depth[a]);
if (a == b) return a;
for (int i = MAXK - 1; i >= 0; i --) {
if (parents_2k[a][i] == parents_2k[b][i]) continue ;
a = parents_2k[a][i];
b = parents_2k[b][i];
}
if (a == b) return a;
return parents[a];
}
};
struct VirtualTree {
int size = 1;
map<int, int> id_to_vid;
VirtualTree (Tree &tree, iset virtual_set) {
vd order_positions;
for (int u : virtual_set) order_positions.push_back({ tree.preordre[u], u });
sort(order_positions.begin(), order_positions.end());
for (int i = 0; i + 1 < order_positions.size(); i ++){
int u = order_positions[i ].second;
int v = order_positions[i + 1].second;
int l = tree.lca(u, v);
virtual_set.insert(l);
}
vd dist_computations;
for (int u : virtual_set) {
dist_computations.push_back({ tree.preordre [u], u });
for (int h : tree.eulertour[u])
dist_computations.push_back({ h, u });
dist_computations.push_back({ tree.postordre[u], u });
}
sort(dist_computations.begin(), dist_computations.end());
set<pair<int, int>> pot_roads;
for (int i = 0; i + 1 < dist_computations.size(); i ++) {
int u = dist_computations[i] .second;
int v = dist_computations[i + 1].second;
if (u == v) continue ;
pot_roads.insert({ min(u, v), max(u, v) });
}
int iNode = 0;
for (int u : virtual_set)
id_to_vid.insert({ u, iNode ++ });
for (const auto &road : pot_roads) {
int u = road.first; int v = road.second;
int dist = abs(tree.depth[u] - tree.depth[v]);
size += dist;
}
}
};
int main () {
int N, M, Q;
cin >> N >> M >> Q;
igrid roads(N);
for (int i = 1; i < N; i ++) {
int a, b;
cin >> a >> b;
a --; b --;
roads[a].push_back(b);
roads[b].push_back(a);
}
Tree tree(N, roads);
idata R(M);
for (int i = 0; i < M; i ++) {
cin >> R[i];
R[i] --;
}
for (int q = 0; q < Q; q ++) {
int l, r;
cin >> l >> r;
l --; r --;
iset SV;
for (int u = l; u <= r; u ++) SV.insert(R[u]);
VirtualTree vTree(tree, SV);
cout << vTree.size << "\n";
}
}
Compilation message
tourism.cpp: In constructor 'VirtualTree::VirtualTree(Tree&, iset)':
tourism.cpp:93:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | for (int i = 0; i + 1 < order_positions.size(); i ++){
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:113:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
113 | for (int i = 0; i + 1 < dist_computations.size(); i ++) {
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
11 ms |
384 KB |
Output is correct |
5 |
Correct |
10 ms |
340 KB |
Output is correct |
6 |
Correct |
9 ms |
340 KB |
Output is correct |
7 |
Correct |
12 ms |
340 KB |
Output is correct |
8 |
Correct |
9 ms |
340 KB |
Output is correct |
9 |
Correct |
24 ms |
340 KB |
Output is correct |
10 |
Correct |
26 ms |
340 KB |
Output is correct |
11 |
Correct |
21 ms |
340 KB |
Output is correct |
12 |
Correct |
45 ms |
340 KB |
Output is correct |
13 |
Correct |
40 ms |
432 KB |
Output is correct |
14 |
Correct |
43 ms |
448 KB |
Output is correct |
15 |
Correct |
16 ms |
432 KB |
Output is correct |
16 |
Correct |
15 ms |
340 KB |
Output is correct |
17 |
Correct |
16 ms |
448 KB |
Output is correct |
18 |
Correct |
21 ms |
444 KB |
Output is correct |
19 |
Correct |
19 ms |
340 KB |
Output is correct |
20 |
Correct |
17 ms |
428 KB |
Output is correct |
21 |
Correct |
22 ms |
340 KB |
Output is correct |
22 |
Correct |
20 ms |
452 KB |
Output is correct |
23 |
Correct |
20 ms |
432 KB |
Output is correct |
24 |
Correct |
20 ms |
340 KB |
Output is correct |
25 |
Correct |
23 ms |
432 KB |
Output is correct |
26 |
Correct |
20 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
300 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
11 ms |
384 KB |
Output is correct |
5 |
Correct |
10 ms |
340 KB |
Output is correct |
6 |
Correct |
9 ms |
340 KB |
Output is correct |
7 |
Correct |
12 ms |
340 KB |
Output is correct |
8 |
Correct |
9 ms |
340 KB |
Output is correct |
9 |
Correct |
24 ms |
340 KB |
Output is correct |
10 |
Correct |
26 ms |
340 KB |
Output is correct |
11 |
Correct |
21 ms |
340 KB |
Output is correct |
12 |
Correct |
45 ms |
340 KB |
Output is correct |
13 |
Correct |
40 ms |
432 KB |
Output is correct |
14 |
Correct |
43 ms |
448 KB |
Output is correct |
15 |
Correct |
16 ms |
432 KB |
Output is correct |
16 |
Correct |
15 ms |
340 KB |
Output is correct |
17 |
Correct |
16 ms |
448 KB |
Output is correct |
18 |
Correct |
21 ms |
444 KB |
Output is correct |
19 |
Correct |
19 ms |
340 KB |
Output is correct |
20 |
Correct |
17 ms |
428 KB |
Output is correct |
21 |
Correct |
22 ms |
340 KB |
Output is correct |
22 |
Correct |
20 ms |
452 KB |
Output is correct |
23 |
Correct |
20 ms |
432 KB |
Output is correct |
24 |
Correct |
20 ms |
340 KB |
Output is correct |
25 |
Correct |
23 ms |
432 KB |
Output is correct |
26 |
Correct |
20 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
300 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
308 KB |
Output is correct |
30 |
Correct |
510 ms |
1100 KB |
Output is correct |
31 |
Correct |
682 ms |
984 KB |
Output is correct |
32 |
Correct |
1029 ms |
1288 KB |
Output is correct |
33 |
Correct |
1028 ms |
1240 KB |
Output is correct |
34 |
Correct |
1008 ms |
1256 KB |
Output is correct |
35 |
Correct |
2350 ms |
1384 KB |
Output is correct |
36 |
Correct |
2270 ms |
1392 KB |
Output is correct |
37 |
Correct |
2217 ms |
1304 KB |
Output is correct |
38 |
Correct |
775 ms |
1288 KB |
Output is correct |
39 |
Correct |
762 ms |
1292 KB |
Output is correct |
40 |
Correct |
749 ms |
1288 KB |
Output is correct |
41 |
Correct |
1942 ms |
1348 KB |
Output is correct |
42 |
Correct |
1927 ms |
1356 KB |
Output is correct |
43 |
Correct |
1905 ms |
1316 KB |
Output is correct |
44 |
Correct |
765 ms |
1376 KB |
Output is correct |
45 |
Correct |
797 ms |
1368 KB |
Output is correct |
46 |
Correct |
803 ms |
1384 KB |
Output is correct |
47 |
Correct |
1930 ms |
1300 KB |
Output is correct |
48 |
Correct |
1902 ms |
1236 KB |
Output is correct |
49 |
Correct |
1923 ms |
1288 KB |
Output is correct |
50 |
Correct |
904 ms |
1272 KB |
Output is correct |
51 |
Correct |
925 ms |
1208 KB |
Output is correct |
52 |
Correct |
923 ms |
1316 KB |
Output is correct |
53 |
Correct |
927 ms |
1336 KB |
Output is correct |
54 |
Correct |
920 ms |
1216 KB |
Output is correct |
55 |
Correct |
918 ms |
1216 KB |
Output is correct |
56 |
Correct |
9 ms |
312 KB |
Output is correct |
57 |
Correct |
5 ms |
820 KB |
Output is correct |
58 |
Correct |
6 ms |
852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
304 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
9 ms |
212 KB |
Output is correct |
4 |
Execution timed out |
5086 ms |
30820 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
128 ms |
16944 KB |
Output is correct |
3 |
Correct |
201 ms |
18728 KB |
Output is correct |
4 |
Correct |
160 ms |
20984 KB |
Output is correct |
5 |
Correct |
216 ms |
45832 KB |
Output is correct |
6 |
Correct |
252 ms |
39132 KB |
Output is correct |
7 |
Correct |
273 ms |
33948 KB |
Output is correct |
8 |
Correct |
287 ms |
31996 KB |
Output is correct |
9 |
Correct |
266 ms |
31248 KB |
Output is correct |
10 |
Correct |
282 ms |
30964 KB |
Output is correct |
11 |
Correct |
260 ms |
30796 KB |
Output is correct |
12 |
Correct |
261 ms |
30796 KB |
Output is correct |
13 |
Correct |
264 ms |
30836 KB |
Output is correct |
14 |
Correct |
308 ms |
31224 KB |
Output is correct |
15 |
Correct |
296 ms |
32128 KB |
Output is correct |
16 |
Correct |
252 ms |
40440 KB |
Output is correct |
17 |
Correct |
262 ms |
40304 KB |
Output is correct |
18 |
Correct |
251 ms |
40304 KB |
Output is correct |
19 |
Correct |
204 ms |
46112 KB |
Output is correct |
20 |
Correct |
232 ms |
41364 KB |
Output is correct |
21 |
Correct |
244 ms |
36520 KB |
Output is correct |
22 |
Correct |
226 ms |
34496 KB |
Output is correct |
23 |
Correct |
244 ms |
33672 KB |
Output is correct |
24 |
Correct |
241 ms |
32836 KB |
Output is correct |
25 |
Correct |
253 ms |
32512 KB |
Output is correct |
26 |
Correct |
286 ms |
32520 KB |
Output is correct |
27 |
Correct |
275 ms |
32324 KB |
Output is correct |
28 |
Correct |
355 ms |
32276 KB |
Output is correct |
29 |
Correct |
320 ms |
32292 KB |
Output is correct |
30 |
Correct |
318 ms |
32328 KB |
Output is correct |
31 |
Correct |
317 ms |
32476 KB |
Output is correct |
32 |
Correct |
296 ms |
32524 KB |
Output is correct |
33 |
Correct |
313 ms |
32856 KB |
Output is correct |
34 |
Correct |
298 ms |
33600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
10 ms |
212 KB |
Output is correct |
4 |
Execution timed out |
5053 ms |
29184 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
11 ms |
384 KB |
Output is correct |
5 |
Correct |
10 ms |
340 KB |
Output is correct |
6 |
Correct |
9 ms |
340 KB |
Output is correct |
7 |
Correct |
12 ms |
340 KB |
Output is correct |
8 |
Correct |
9 ms |
340 KB |
Output is correct |
9 |
Correct |
24 ms |
340 KB |
Output is correct |
10 |
Correct |
26 ms |
340 KB |
Output is correct |
11 |
Correct |
21 ms |
340 KB |
Output is correct |
12 |
Correct |
45 ms |
340 KB |
Output is correct |
13 |
Correct |
40 ms |
432 KB |
Output is correct |
14 |
Correct |
43 ms |
448 KB |
Output is correct |
15 |
Correct |
16 ms |
432 KB |
Output is correct |
16 |
Correct |
15 ms |
340 KB |
Output is correct |
17 |
Correct |
16 ms |
448 KB |
Output is correct |
18 |
Correct |
21 ms |
444 KB |
Output is correct |
19 |
Correct |
19 ms |
340 KB |
Output is correct |
20 |
Correct |
17 ms |
428 KB |
Output is correct |
21 |
Correct |
22 ms |
340 KB |
Output is correct |
22 |
Correct |
20 ms |
452 KB |
Output is correct |
23 |
Correct |
20 ms |
432 KB |
Output is correct |
24 |
Correct |
20 ms |
340 KB |
Output is correct |
25 |
Correct |
23 ms |
432 KB |
Output is correct |
26 |
Correct |
20 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
300 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
308 KB |
Output is correct |
30 |
Correct |
510 ms |
1100 KB |
Output is correct |
31 |
Correct |
682 ms |
984 KB |
Output is correct |
32 |
Correct |
1029 ms |
1288 KB |
Output is correct |
33 |
Correct |
1028 ms |
1240 KB |
Output is correct |
34 |
Correct |
1008 ms |
1256 KB |
Output is correct |
35 |
Correct |
2350 ms |
1384 KB |
Output is correct |
36 |
Correct |
2270 ms |
1392 KB |
Output is correct |
37 |
Correct |
2217 ms |
1304 KB |
Output is correct |
38 |
Correct |
775 ms |
1288 KB |
Output is correct |
39 |
Correct |
762 ms |
1292 KB |
Output is correct |
40 |
Correct |
749 ms |
1288 KB |
Output is correct |
41 |
Correct |
1942 ms |
1348 KB |
Output is correct |
42 |
Correct |
1927 ms |
1356 KB |
Output is correct |
43 |
Correct |
1905 ms |
1316 KB |
Output is correct |
44 |
Correct |
765 ms |
1376 KB |
Output is correct |
45 |
Correct |
797 ms |
1368 KB |
Output is correct |
46 |
Correct |
803 ms |
1384 KB |
Output is correct |
47 |
Correct |
1930 ms |
1300 KB |
Output is correct |
48 |
Correct |
1902 ms |
1236 KB |
Output is correct |
49 |
Correct |
1923 ms |
1288 KB |
Output is correct |
50 |
Correct |
904 ms |
1272 KB |
Output is correct |
51 |
Correct |
925 ms |
1208 KB |
Output is correct |
52 |
Correct |
923 ms |
1316 KB |
Output is correct |
53 |
Correct |
927 ms |
1336 KB |
Output is correct |
54 |
Correct |
920 ms |
1216 KB |
Output is correct |
55 |
Correct |
918 ms |
1216 KB |
Output is correct |
56 |
Correct |
9 ms |
312 KB |
Output is correct |
57 |
Correct |
5 ms |
820 KB |
Output is correct |
58 |
Correct |
6 ms |
852 KB |
Output is correct |
59 |
Correct |
1 ms |
304 KB |
Output is correct |
60 |
Correct |
1 ms |
212 KB |
Output is correct |
61 |
Correct |
9 ms |
212 KB |
Output is correct |
62 |
Execution timed out |
5086 ms |
30820 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |