Submission #837195

# Submission time Handle Problem Language Result Execution time Memory
837195 2023-08-25T07:52:41 Z veehz Tourism (JOI23_tourism) C++17
28 / 100
5000 ms 21100 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
#define pb push_back

int n, m, q;
vector<vector<int>> adj;
vector<int> dfsPath;
vector<int> mp;

void dfs(int u, int p) {
  dfsPath.pb(u);
  for (auto& v : adj[u]) {
    if (v == p) continue;
    dfs(v, u);
  }
}

namespace LCA {
int l, timer;
vector<int> tin, tout;
vector<vector<int>> up;

void dfs(int u, int p) {
  tin[u] = ++timer;
  up[u][0] = p;
  for (int i = 1; i <= l; i++) up[u][i] = up[up[u][i - 1]][i - 1];
  for (auto& v : adj[u]) {
    if (v != p) dfs(v, u);
  }
  tout[u] = ++timer;
}

/// u is ancestor of v
bool is_ancestor(int u, int v) {
  return tin[u] <= tin[v] && tout[u] >= tout[v];
}

/// returns {lca, distance}
pair<int, int> lca(int u, int v) {
  if (u == v) return {u, 0};

  int dist = 0;
  for (int i = l; i >= 0; i--) {
    if (!is_ancestor(up[u][i], v)) {
      dist += 1 << i;
      u = up[u][i];
    }
  }
  for (int i = l; i >= 0; i--) {
    if (!is_ancestor(up[v][i], u)) {
      dist += 1 << i;
      v = up[v][i];
    }
  }
  if (is_ancestor(u, v)) return {u, dist + 1};
  if (is_ancestor(v, u)) return {v, dist + 1};
  return {up[u][0], dist + 2};
}

void preprocess(int root) {
  tin.resize(n);
  tout.resize(n);
  timer = 0;
  l = ceil(log2(n));
  up.assign(n, vector<int>(l + 1));
  dfs(root, root);
}

}  // namespace LCA

int main() {
  cin >> n >> m >> q;
  adj.assign(n, {});
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    adj[a].pb(b);
    adj[b].pb(a);
  }
  dfs(0, 0);
  mp.resize(n);
  for (int i = 0; i < n; i++) {
    mp[dfsPath[i]] = i;
  }
  // for (auto& x : dfsPath) cout << x << " ";
  // cout << endl;
  // for (auto& x : mp) cout << x << " ";
  // cout << endl;

  vector<int> cities(m);
  for (int i = 0; i < m; i++) cin >> cities[i], cities[i]--;

  LCA::preprocess(0);

  while (q--) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    vector<int> cur(cities.begin() + l, cities.begin() + r + 1);
    sort(cur.begin(), cur.end(),
         [&](const int a, const int b) { return mp[a] < mp[b]; });
    // cout << "Distance between " << cur[0] << " and " << cur.back() << " is "
    //      << LCA::lca(cur[0], cur.back()).second << endl;
    int dist = LCA::lca(cur[0], cur.back()).second;
    for (int i = 0; i < (int)cur.size() - 1; i++) {
      // cout << "Distance between " << cur[i] << " and " << cur[i + 1] << " is "
      //      << LCA::lca(cur[i], cur[i + 1]).second << endl;
      dist += LCA::lca(cur[i], cur[i + 1]).second;
    }

    cout << dist/2 + 1 << endl;
  }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 2 ms 296 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 3 ms 340 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 3 ms 340 KB Output is correct
10 Correct 3 ms 340 KB Output is correct
11 Correct 3 ms 340 KB Output is correct
12 Correct 6 ms 340 KB Output is correct
13 Correct 6 ms 300 KB Output is correct
14 Correct 7 ms 300 KB Output is correct
15 Correct 3 ms 340 KB Output is correct
16 Correct 3 ms 340 KB Output is correct
17 Correct 3 ms 388 KB Output is correct
18 Correct 3 ms 340 KB Output is correct
19 Correct 3 ms 340 KB Output is correct
20 Correct 3 ms 300 KB Output is correct
21 Correct 3 ms 340 KB Output is correct
22 Correct 3 ms 340 KB Output is correct
23 Correct 3 ms 340 KB Output is correct
24 Correct 4 ms 340 KB Output is correct
25 Correct 3 ms 340 KB Output is correct
26 Correct 4 ms 468 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 2 ms 296 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 3 ms 340 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 3 ms 340 KB Output is correct
10 Correct 3 ms 340 KB Output is correct
11 Correct 3 ms 340 KB Output is correct
12 Correct 6 ms 340 KB Output is correct
13 Correct 6 ms 300 KB Output is correct
14 Correct 7 ms 300 KB Output is correct
15 Correct 3 ms 340 KB Output is correct
16 Correct 3 ms 340 KB Output is correct
17 Correct 3 ms 388 KB Output is correct
18 Correct 3 ms 340 KB Output is correct
19 Correct 3 ms 340 KB Output is correct
20 Correct 3 ms 300 KB Output is correct
21 Correct 3 ms 340 KB Output is correct
22 Correct 3 ms 340 KB Output is correct
23 Correct 3 ms 340 KB Output is correct
24 Correct 4 ms 340 KB Output is correct
25 Correct 3 ms 340 KB Output is correct
26 Correct 4 ms 468 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 2 ms 340 KB Output is correct
30 Correct 58 ms 596 KB Output is correct
31 Correct 80 ms 612 KB Output is correct
32 Correct 118 ms 692 KB Output is correct
33 Correct 116 ms 812 KB Output is correct
34 Correct 118 ms 688 KB Output is correct
35 Correct 320 ms 676 KB Output is correct
36 Correct 314 ms 672 KB Output is correct
37 Correct 315 ms 676 KB Output is correct
38 Correct 117 ms 752 KB Output is correct
39 Correct 117 ms 752 KB Output is correct
40 Correct 112 ms 760 KB Output is correct
41 Correct 310 ms 748 KB Output is correct
42 Correct 306 ms 724 KB Output is correct
43 Correct 305 ms 736 KB Output is correct
44 Correct 114 ms 720 KB Output is correct
45 Correct 126 ms 708 KB Output is correct
46 Correct 117 ms 728 KB Output is correct
47 Correct 309 ms 696 KB Output is correct
48 Correct 313 ms 696 KB Output is correct
49 Correct 302 ms 700 KB Output is correct
50 Correct 102 ms 692 KB Output is correct
51 Correct 104 ms 696 KB Output is correct
52 Correct 105 ms 596 KB Output is correct
53 Correct 102 ms 696 KB Output is correct
54 Correct 102 ms 716 KB Output is correct
55 Correct 100 ms 708 KB Output is correct
56 Correct 14 ms 340 KB Output is correct
57 Correct 4 ms 548 KB Output is correct
58 Correct 5 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 13 ms 340 KB Output is correct
4 Execution timed out 5018 ms 14676 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 65 ms 10968 KB Output is correct
3 Correct 96 ms 12032 KB Output is correct
4 Correct 93 ms 13520 KB Output is correct
5 Correct 139 ms 20120 KB Output is correct
6 Correct 137 ms 19884 KB Output is correct
7 Correct 157 ms 19884 KB Output is correct
8 Correct 137 ms 19716 KB Output is correct
9 Correct 134 ms 19756 KB Output is correct
10 Correct 139 ms 19760 KB Output is correct
11 Correct 150 ms 19912 KB Output is correct
12 Correct 136 ms 19712 KB Output is correct
13 Correct 137 ms 19820 KB Output is correct
14 Correct 186 ms 20184 KB Output is correct
15 Correct 294 ms 21028 KB Output is correct
16 Correct 205 ms 20000 KB Output is correct
17 Correct 150 ms 20000 KB Output is correct
18 Correct 163 ms 20068 KB Output is correct
19 Correct 137 ms 20128 KB Output is correct
20 Correct 168 ms 20092 KB Output is correct
21 Correct 130 ms 19824 KB Output is correct
22 Correct 171 ms 19800 KB Output is correct
23 Correct 178 ms 19660 KB Output is correct
24 Correct 136 ms 19752 KB Output is correct
25 Correct 159 ms 19852 KB Output is correct
26 Correct 156 ms 19660 KB Output is correct
27 Correct 166 ms 19660 KB Output is correct
28 Correct 169 ms 19788 KB Output is correct
29 Correct 185 ms 19664 KB Output is correct
30 Correct 162 ms 19728 KB Output is correct
31 Correct 182 ms 19844 KB Output is correct
32 Correct 216 ms 20052 KB Output is correct
33 Correct 229 ms 20456 KB Output is correct
34 Correct 258 ms 21100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 300 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 13 ms 312 KB Output is correct
4 Execution timed out 5054 ms 12232 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 2 ms 296 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 3 ms 340 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 3 ms 340 KB Output is correct
10 Correct 3 ms 340 KB Output is correct
11 Correct 3 ms 340 KB Output is correct
12 Correct 6 ms 340 KB Output is correct
13 Correct 6 ms 300 KB Output is correct
14 Correct 7 ms 300 KB Output is correct
15 Correct 3 ms 340 KB Output is correct
16 Correct 3 ms 340 KB Output is correct
17 Correct 3 ms 388 KB Output is correct
18 Correct 3 ms 340 KB Output is correct
19 Correct 3 ms 340 KB Output is correct
20 Correct 3 ms 300 KB Output is correct
21 Correct 3 ms 340 KB Output is correct
22 Correct 3 ms 340 KB Output is correct
23 Correct 3 ms 340 KB Output is correct
24 Correct 4 ms 340 KB Output is correct
25 Correct 3 ms 340 KB Output is correct
26 Correct 4 ms 468 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 2 ms 340 KB Output is correct
30 Correct 58 ms 596 KB Output is correct
31 Correct 80 ms 612 KB Output is correct
32 Correct 118 ms 692 KB Output is correct
33 Correct 116 ms 812 KB Output is correct
34 Correct 118 ms 688 KB Output is correct
35 Correct 320 ms 676 KB Output is correct
36 Correct 314 ms 672 KB Output is correct
37 Correct 315 ms 676 KB Output is correct
38 Correct 117 ms 752 KB Output is correct
39 Correct 117 ms 752 KB Output is correct
40 Correct 112 ms 760 KB Output is correct
41 Correct 310 ms 748 KB Output is correct
42 Correct 306 ms 724 KB Output is correct
43 Correct 305 ms 736 KB Output is correct
44 Correct 114 ms 720 KB Output is correct
45 Correct 126 ms 708 KB Output is correct
46 Correct 117 ms 728 KB Output is correct
47 Correct 309 ms 696 KB Output is correct
48 Correct 313 ms 696 KB Output is correct
49 Correct 302 ms 700 KB Output is correct
50 Correct 102 ms 692 KB Output is correct
51 Correct 104 ms 696 KB Output is correct
52 Correct 105 ms 596 KB Output is correct
53 Correct 102 ms 696 KB Output is correct
54 Correct 102 ms 716 KB Output is correct
55 Correct 100 ms 708 KB Output is correct
56 Correct 14 ms 340 KB Output is correct
57 Correct 4 ms 548 KB Output is correct
58 Correct 5 ms 596 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 1 ms 340 KB Output is correct
61 Correct 13 ms 340 KB Output is correct
62 Execution timed out 5018 ms 14676 KB Time limit exceeded
63 Halted 0 ms 0 KB -