답안 #1000522

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1000522 2024-06-17T16:25:44 Z LucaLucaM Tourism (JOI23_tourism) C++17
5 / 100
5000 ms 17772 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]);
    }
    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
      |  ^~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2904 KB Output is correct
2 Correct 1 ms 2908 KB Output is correct
3 Correct 1 ms 2912 KB Output is correct
4 Correct 1 ms 3168 KB Output is correct
5 Correct 1 ms 3172 KB Output is correct
6 Correct 1 ms 3160 KB Output is correct
7 Correct 1 ms 3164 KB Output is correct
8 Correct 1 ms 3164 KB Output is correct
9 Correct 1 ms 3168 KB Output is correct
10 Correct 2 ms 3164 KB Output is correct
11 Correct 1 ms 3164 KB Output is correct
12 Correct 2 ms 3164 KB Output is correct
13 Correct 2 ms 3184 KB Output is correct
14 Correct 2 ms 3168 KB Output is correct
15 Correct 6 ms 3160 KB Output is correct
16 Correct 7 ms 3164 KB Output is correct
17 Correct 8 ms 3164 KB Output is correct
18 Correct 4 ms 3164 KB Output is correct
19 Correct 7 ms 3164 KB Output is correct
20 Correct 5 ms 3160 KB Output is correct
21 Correct 1 ms 3164 KB Output is correct
22 Correct 1 ms 3164 KB Output is correct
23 Correct 1 ms 3164 KB Output is correct
24 Correct 1 ms 2964 KB Output is correct
25 Correct 1 ms 3164 KB Output is correct
26 Correct 1 ms 3164 KB Output is correct
27 Correct 1 ms 2908 KB Output is correct
28 Correct 1 ms 3164 KB Output is correct
29 Correct 1 ms 3168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2904 KB Output is correct
2 Correct 1 ms 2908 KB Output is correct
3 Correct 1 ms 2912 KB Output is correct
4 Correct 1 ms 3168 KB Output is correct
5 Correct 1 ms 3172 KB Output is correct
6 Correct 1 ms 3160 KB Output is correct
7 Correct 1 ms 3164 KB Output is correct
8 Correct 1 ms 3164 KB Output is correct
9 Correct 1 ms 3168 KB Output is correct
10 Correct 2 ms 3164 KB Output is correct
11 Correct 1 ms 3164 KB Output is correct
12 Correct 2 ms 3164 KB Output is correct
13 Correct 2 ms 3184 KB Output is correct
14 Correct 2 ms 3168 KB Output is correct
15 Correct 6 ms 3160 KB Output is correct
16 Correct 7 ms 3164 KB Output is correct
17 Correct 8 ms 3164 KB Output is correct
18 Correct 4 ms 3164 KB Output is correct
19 Correct 7 ms 3164 KB Output is correct
20 Correct 5 ms 3160 KB Output is correct
21 Correct 1 ms 3164 KB Output is correct
22 Correct 1 ms 3164 KB Output is correct
23 Correct 1 ms 3164 KB Output is correct
24 Correct 1 ms 2964 KB Output is correct
25 Correct 1 ms 3164 KB Output is correct
26 Correct 1 ms 3164 KB Output is correct
27 Correct 1 ms 2908 KB Output is correct
28 Correct 1 ms 3164 KB Output is correct
29 Correct 1 ms 3168 KB Output is correct
30 Correct 12 ms 3168 KB Output is correct
31 Correct 21 ms 3368 KB Output is correct
32 Correct 29 ms 3164 KB Output is correct
33 Correct 40 ms 3148 KB Output is correct
34 Correct 25 ms 3160 KB Output is correct
35 Correct 107 ms 3416 KB Output is correct
36 Correct 113 ms 3164 KB Output is correct
37 Correct 67 ms 3164 KB Output is correct
38 Correct 1525 ms 3164 KB Output is correct
39 Correct 1551 ms 3416 KB Output is correct
40 Correct 1684 ms 3164 KB Output is correct
41 Execution timed out 5032 ms 3292 KB Time limit exceeded
42 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2908 KB Output is correct
2 Correct 2 ms 2908 KB Output is correct
3 Correct 6 ms 2968 KB Output is correct
4 Execution timed out 5088 ms 13916 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2908 KB Output is correct
2 Correct 96 ms 10612 KB Output is correct
3 Correct 113 ms 11604 KB Output is correct
4 Correct 121 ms 12372 KB Output is correct
5 Correct 40 ms 17612 KB Output is correct
6 Correct 43 ms 17096 KB Output is correct
7 Correct 44 ms 16984 KB Output is correct
8 Correct 45 ms 16948 KB Output is correct
9 Correct 50 ms 16980 KB Output is correct
10 Correct 67 ms 16980 KB Output is correct
11 Correct 120 ms 16976 KB Output is correct
12 Correct 275 ms 16980 KB Output is correct
13 Correct 776 ms 17056 KB Output is correct
14 Correct 2267 ms 17232 KB Output is correct
15 Execution timed out 5049 ms 17772 KB Time limit exceeded
16 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2904 KB Output is correct
2 Correct 1 ms 2908 KB Output is correct
3 Correct 7 ms 2908 KB Output is correct
4 Execution timed out 5090 ms 12272 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2904 KB Output is correct
2 Correct 1 ms 2908 KB Output is correct
3 Correct 1 ms 2912 KB Output is correct
4 Correct 1 ms 3168 KB Output is correct
5 Correct 1 ms 3172 KB Output is correct
6 Correct 1 ms 3160 KB Output is correct
7 Correct 1 ms 3164 KB Output is correct
8 Correct 1 ms 3164 KB Output is correct
9 Correct 1 ms 3168 KB Output is correct
10 Correct 2 ms 3164 KB Output is correct
11 Correct 1 ms 3164 KB Output is correct
12 Correct 2 ms 3164 KB Output is correct
13 Correct 2 ms 3184 KB Output is correct
14 Correct 2 ms 3168 KB Output is correct
15 Correct 6 ms 3160 KB Output is correct
16 Correct 7 ms 3164 KB Output is correct
17 Correct 8 ms 3164 KB Output is correct
18 Correct 4 ms 3164 KB Output is correct
19 Correct 7 ms 3164 KB Output is correct
20 Correct 5 ms 3160 KB Output is correct
21 Correct 1 ms 3164 KB Output is correct
22 Correct 1 ms 3164 KB Output is correct
23 Correct 1 ms 3164 KB Output is correct
24 Correct 1 ms 2964 KB Output is correct
25 Correct 1 ms 3164 KB Output is correct
26 Correct 1 ms 3164 KB Output is correct
27 Correct 1 ms 2908 KB Output is correct
28 Correct 1 ms 3164 KB Output is correct
29 Correct 1 ms 3168 KB Output is correct
30 Correct 12 ms 3168 KB Output is correct
31 Correct 21 ms 3368 KB Output is correct
32 Correct 29 ms 3164 KB Output is correct
33 Correct 40 ms 3148 KB Output is correct
34 Correct 25 ms 3160 KB Output is correct
35 Correct 107 ms 3416 KB Output is correct
36 Correct 113 ms 3164 KB Output is correct
37 Correct 67 ms 3164 KB Output is correct
38 Correct 1525 ms 3164 KB Output is correct
39 Correct 1551 ms 3416 KB Output is correct
40 Correct 1684 ms 3164 KB Output is correct
41 Execution timed out 5032 ms 3292 KB Time limit exceeded
42 Halted 0 ms 0 KB -