Submission #1000521

# Submission time Handle Problem Language Result Execution time Memory
1000521 2024-06-17T16:22:42 Z LucaLucaM Tourism (JOI23_tourism) C++17
5 / 100
5000 ms 17868 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <cstring>
#warning That's the baby, that's not my baby

typedef long long ll;

const int NMAX = 100'000;
const int LGMAX = 18;

std::vector<int> g[NMAX + 1];
int parent[NMAX + 1];
int tin[NMAX + 1], tout[NMAX + 1], timer;

int jump[LGMAX + 1][NMAX + 1];

void dfs(int u) {
  tin[u] = ++timer;
  for (const auto &v : g[u]) {
    if (v != parent[u]) {
      parent[v] = u;
      dfs(v);
    }
  }
  tout[u] = ++timer;
}

bool ancestor(int u, int v) {
  return tin[u] <= tin[v] && tout[v] <= tout[u];
}

int lca(int u, int v) {
  if (ancestor(u, v)) {
    return u;
  }
  for (int k = LGMAX; k >= 0; k--) {
    if (jump[k][u] != 0 && !ancestor(jump[k][u], v)) {
      u = jump[k][u];
    }
  }
  return jump[0][u];
}

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(0);

  int n, m, q;
  std::cin >> n >> m >> q;

  for (int i = 1; i < n; i++) {
    int u, v;
    std::cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }

  dfs(1);

  for (int i = 1; i <= n; i++) {
    jump[0][i] = parent[i];
  }
  for (int j = 1; j <= LGMAX; j++) {
    for (int i = 1; i <= n; i++) {
      jump[j][i] = jump[j - 1][jump[j - 1][i]];
    }
  }

  std::vector<int> a(m);
  for (auto &x : a) {
    std::cin >> x;
  }
  a.insert(a.begin(), 0);

  while (q--) {
    int l, r;
    std::cin >> l >> r;
    std::vector<bool> vis(n + 1, false);
    std::vector<int> nodes;
    for (int i = l; i <= r; i++) {
      nodes.push_back(a[i]);
    }
    std::sort(nodes.begin(), nodes.end(), [&](int x, int y) {
       return tin[x] < tin[y];
    });
    int w = -1;
    for (const auto &node : nodes) {
      if (w == -1) {
        w = node;
      } else {
        while (!ancestor(w, node)) {
          vis[w] = true;
          w = parent[w];
        }
      }
      int u = node;
      while (u != w) {
        vis[u] = true;
        u = parent[u];
      }
      assert(u == w);
      vis[u] = true;
    }
    std::cout << std::count(vis.begin(), vis.end(), true) << '\n';
  }

  return 0;
}

Compilation message

tourism.cpp:6:2: warning: #warning That's the baby, that's not my baby [-Wcpp]
    6 | #warning That's the baby, that's not my baby
      |  ^~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 9052 KB Output is correct
2 Correct 1 ms 9052 KB Output is correct
3 Correct 1 ms 9052 KB Output is correct
4 Correct 2 ms 9052 KB Output is correct
5 Correct 2 ms 9052 KB Output is correct
6 Correct 2 ms 9048 KB Output is correct
7 Correct 2 ms 9052 KB Output is correct
8 Correct 2 ms 9188 KB Output is correct
9 Correct 3 ms 9052 KB Output is correct
10 Correct 3 ms 9052 KB Output is correct
11 Correct 3 ms 9052 KB Output is correct
12 Correct 6 ms 9052 KB Output is correct
13 Correct 5 ms 9052 KB Output is correct
14 Correct 6 ms 9052 KB Output is correct
15 Correct 7 ms 9252 KB Output is correct
16 Correct 9 ms 9252 KB Output is correct
17 Correct 10 ms 9184 KB Output is correct
18 Correct 6 ms 9248 KB Output is correct
19 Correct 9 ms 9052 KB Output is correct
20 Correct 7 ms 9052 KB Output is correct
21 Correct 3 ms 9052 KB Output is correct
22 Correct 3 ms 9052 KB Output is correct
23 Correct 2 ms 9052 KB Output is correct
24 Correct 3 ms 9048 KB Output is correct
25 Correct 3 ms 9048 KB Output is correct
26 Correct 3 ms 9052 KB Output is correct
27 Correct 2 ms 9052 KB Output is correct
28 Correct 1 ms 9052 KB Output is correct
29 Correct 1 ms 9140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 9052 KB Output is correct
2 Correct 1 ms 9052 KB Output is correct
3 Correct 1 ms 9052 KB Output is correct
4 Correct 2 ms 9052 KB Output is correct
5 Correct 2 ms 9052 KB Output is correct
6 Correct 2 ms 9048 KB Output is correct
7 Correct 2 ms 9052 KB Output is correct
8 Correct 2 ms 9188 KB Output is correct
9 Correct 3 ms 9052 KB Output is correct
10 Correct 3 ms 9052 KB Output is correct
11 Correct 3 ms 9052 KB Output is correct
12 Correct 6 ms 9052 KB Output is correct
13 Correct 5 ms 9052 KB Output is correct
14 Correct 6 ms 9052 KB Output is correct
15 Correct 7 ms 9252 KB Output is correct
16 Correct 9 ms 9252 KB Output is correct
17 Correct 10 ms 9184 KB Output is correct
18 Correct 6 ms 9248 KB Output is correct
19 Correct 9 ms 9052 KB Output is correct
20 Correct 7 ms 9052 KB Output is correct
21 Correct 3 ms 9052 KB Output is correct
22 Correct 3 ms 9052 KB Output is correct
23 Correct 2 ms 9052 KB Output is correct
24 Correct 3 ms 9048 KB Output is correct
25 Correct 3 ms 9048 KB Output is correct
26 Correct 3 ms 9052 KB Output is correct
27 Correct 2 ms 9052 KB Output is correct
28 Correct 1 ms 9052 KB Output is correct
29 Correct 1 ms 9140 KB Output is correct
30 Correct 41 ms 9308 KB Output is correct
31 Correct 70 ms 9368 KB Output is correct
32 Correct 97 ms 9308 KB Output is correct
33 Correct 104 ms 9552 KB Output is correct
34 Correct 96 ms 9304 KB Output is correct
35 Correct 303 ms 9308 KB Output is correct
36 Correct 296 ms 9560 KB Output is correct
37 Correct 277 ms 9304 KB Output is correct
38 Correct 1561 ms 9308 KB Output is correct
39 Correct 1496 ms 9308 KB Output is correct
40 Correct 1726 ms 9464 KB Output is correct
41 Execution timed out 5016 ms 9308 KB Time limit exceeded
42 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 9052 KB Output is correct
2 Correct 2 ms 9052 KB Output is correct
3 Correct 13 ms 9248 KB Output is correct
4 Execution timed out 5046 ms 15684 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 9052 KB Output is correct
2 Correct 93 ms 12652 KB Output is correct
3 Correct 113 ms 14424 KB Output is correct
4 Correct 117 ms 14684 KB Output is correct
5 Correct 43 ms 17612 KB Output is correct
6 Correct 43 ms 17108 KB Output is correct
7 Correct 42 ms 16956 KB Output is correct
8 Correct 42 ms 16964 KB Output is correct
9 Correct 47 ms 16976 KB Output is correct
10 Correct 67 ms 16992 KB Output is correct
11 Correct 112 ms 17080 KB Output is correct
12 Correct 259 ms 16988 KB Output is correct
13 Correct 779 ms 17232 KB Output is correct
14 Correct 2273 ms 17488 KB Output is correct
15 Execution timed out 5062 ms 17868 KB Time limit exceeded
16 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 9048 KB Output is correct
2 Correct 2 ms 9052 KB Output is correct
3 Correct 13 ms 9256 KB Output is correct
4 Execution timed out 5075 ms 13520 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 9052 KB Output is correct
2 Correct 1 ms 9052 KB Output is correct
3 Correct 1 ms 9052 KB Output is correct
4 Correct 2 ms 9052 KB Output is correct
5 Correct 2 ms 9052 KB Output is correct
6 Correct 2 ms 9048 KB Output is correct
7 Correct 2 ms 9052 KB Output is correct
8 Correct 2 ms 9188 KB Output is correct
9 Correct 3 ms 9052 KB Output is correct
10 Correct 3 ms 9052 KB Output is correct
11 Correct 3 ms 9052 KB Output is correct
12 Correct 6 ms 9052 KB Output is correct
13 Correct 5 ms 9052 KB Output is correct
14 Correct 6 ms 9052 KB Output is correct
15 Correct 7 ms 9252 KB Output is correct
16 Correct 9 ms 9252 KB Output is correct
17 Correct 10 ms 9184 KB Output is correct
18 Correct 6 ms 9248 KB Output is correct
19 Correct 9 ms 9052 KB Output is correct
20 Correct 7 ms 9052 KB Output is correct
21 Correct 3 ms 9052 KB Output is correct
22 Correct 3 ms 9052 KB Output is correct
23 Correct 2 ms 9052 KB Output is correct
24 Correct 3 ms 9048 KB Output is correct
25 Correct 3 ms 9048 KB Output is correct
26 Correct 3 ms 9052 KB Output is correct
27 Correct 2 ms 9052 KB Output is correct
28 Correct 1 ms 9052 KB Output is correct
29 Correct 1 ms 9140 KB Output is correct
30 Correct 41 ms 9308 KB Output is correct
31 Correct 70 ms 9368 KB Output is correct
32 Correct 97 ms 9308 KB Output is correct
33 Correct 104 ms 9552 KB Output is correct
34 Correct 96 ms 9304 KB Output is correct
35 Correct 303 ms 9308 KB Output is correct
36 Correct 296 ms 9560 KB Output is correct
37 Correct 277 ms 9304 KB Output is correct
38 Correct 1561 ms 9308 KB Output is correct
39 Correct 1496 ms 9308 KB Output is correct
40 Correct 1726 ms 9464 KB Output is correct
41 Execution timed out 5016 ms 9308 KB Time limit exceeded
42 Halted 0 ms 0 KB -