Submission #786975

# Submission time Handle Problem Language Result Execution time Memory
786975 2023-07-18T15:58:15 Z tch1cherin Cat Exercise (JOI23_ho_t4) C++17
100 / 100
301 ms 46336 KB
#include <bits/stdc++.h>
using namespace std;

const int MAX_N = 2e5 + 5, MAX_LOG = 18;
vector<int> G[MAX_N];
int ancestor[MAX_LOG][MAX_N] = {}, depth[MAX_N] = {};

void DFS(int current, int parent) {
  ancestor[0][current] = parent;
  for (int step = 0; step < MAX_LOG - 1; step++) {
    ancestor[step + 1][current] = ancestor[step][ancestor[step][current]];
  }  
  for (int child : G[current]) {
    if (child != parent) {
      depth[child] = depth[current] + 1;
      DFS(child, current);
    }
  }
}

int distance(int u, int v) {
  if (depth[u] < depth[v]) {
    swap(u, v);
  }
  int answer = depth[u] - depth[v];
  for (int step = MAX_LOG - 1; step >= 0; step--) {
    if (depth[u] - (1 << step) >= depth[v]) {
      u = ancestor[step][u];
    }
  }
  if (u == v) {
    return answer;
  }
  for (int step = MAX_LOG - 1; step >= 0; step--) {
    if (ancestor[step][u] != ancestor[step][v]) {
      u = ancestor[step][u];
      v = ancestor[step][v];
      answer += 2 * (1 << step);
    }
  }
  answer += 2;
  return answer;
}

struct dsu {
  vector<int> parent;

  dsu(int size) : parent(size, -1) {}

  int leader(int u) {
    if (parent[u] == -1) {
      return u;
    } else {
      parent[u] = leader(parent[u]);
      return parent[u];
    }
  }

  void unite(int u, int v) {
    u = leader(u);
    v = leader(v);
    if (u != v) {
      if (u < v) {
        swap(u, v);
      }
      parent[v] = u;
    }
  }
};

int main() {
  int N;
  cin >> N;
  vector<int> P(N);
  for (int i = 0; i < N; i++) {
    cin >> P[i];
    P[i]--;
  }
  vector<int> position(N);
  for (int i = 0; i < N; i++) {
    position[P[i]] = i;
  }
  for (int i = 0; i < N - 1; i++) {
    int A, B;
    cin >> A >> B;
    A--, B--;
    G[P[A]].push_back(P[B]);
    G[P[B]].push_back(P[A]); 
  }
  DFS(0, 0);
  dsu sets(N);
  vector<long long> component_result(N);
  for (int i = 0; i < N; i++) {
    for (int j : G[i]) {
      if (j < i) {
        j = sets.leader(j);
        component_result[i] = max(component_result[i], component_result[j] + distance(i, j));
        sets.unite(i, j);
      }
    }
  }
  cout << component_result[N - 1] << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 3 ms 5004 KB Output is correct
6 Correct 2 ms 5048 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Correct 2 ms 5040 KB Output is correct
9 Correct 2 ms 5076 KB Output is correct
10 Correct 2 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 3 ms 5004 KB Output is correct
6 Correct 2 ms 5048 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Correct 2 ms 5040 KB Output is correct
9 Correct 2 ms 5076 KB Output is correct
10 Correct 2 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 2 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 3 ms 5004 KB Output is correct
6 Correct 2 ms 5048 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Correct 2 ms 5040 KB Output is correct
9 Correct 2 ms 5076 KB Output is correct
10 Correct 2 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 2 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5132 KB Output is correct
18 Correct 7 ms 5908 KB Output is correct
19 Correct 7 ms 6100 KB Output is correct
20 Correct 8 ms 6228 KB Output is correct
21 Correct 6 ms 5912 KB Output is correct
22 Correct 7 ms 5972 KB Output is correct
23 Correct 7 ms 5972 KB Output is correct
24 Correct 6 ms 5972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 3 ms 5004 KB Output is correct
6 Correct 2 ms 5048 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Correct 2 ms 5040 KB Output is correct
9 Correct 2 ms 5076 KB Output is correct
10 Correct 2 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 2 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5132 KB Output is correct
18 Correct 7 ms 5908 KB Output is correct
19 Correct 7 ms 6100 KB Output is correct
20 Correct 8 ms 6228 KB Output is correct
21 Correct 6 ms 5912 KB Output is correct
22 Correct 7 ms 5972 KB Output is correct
23 Correct 7 ms 5972 KB Output is correct
24 Correct 6 ms 5972 KB Output is correct
25 Correct 3 ms 5004 KB Output is correct
26 Correct 7 ms 6020 KB Output is correct
27 Correct 6 ms 6008 KB Output is correct
28 Correct 7 ms 5984 KB Output is correct
29 Correct 6 ms 5972 KB Output is correct
30 Correct 7 ms 5716 KB Output is correct
31 Correct 7 ms 5780 KB Output is correct
32 Correct 6 ms 5780 KB Output is correct
33 Correct 6 ms 5688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 3 ms 5004 KB Output is correct
6 Correct 2 ms 5048 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Correct 2 ms 5040 KB Output is correct
9 Correct 2 ms 5076 KB Output is correct
10 Correct 2 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 2 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5132 KB Output is correct
18 Correct 7 ms 5908 KB Output is correct
19 Correct 7 ms 6100 KB Output is correct
20 Correct 8 ms 6228 KB Output is correct
21 Correct 6 ms 5912 KB Output is correct
22 Correct 7 ms 5972 KB Output is correct
23 Correct 7 ms 5972 KB Output is correct
24 Correct 6 ms 5972 KB Output is correct
25 Correct 184 ms 40060 KB Output is correct
26 Correct 199 ms 46336 KB Output is correct
27 Correct 175 ms 46336 KB Output is correct
28 Correct 242 ms 45668 KB Output is correct
29 Correct 247 ms 40200 KB Output is correct
30 Correct 243 ms 44156 KB Output is correct
31 Correct 227 ms 42888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 230 ms 33680 KB Output is correct
4 Correct 255 ms 33656 KB Output is correct
5 Correct 244 ms 33596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 3 ms 5004 KB Output is correct
6 Correct 2 ms 5048 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Correct 2 ms 5040 KB Output is correct
9 Correct 2 ms 5076 KB Output is correct
10 Correct 2 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 2 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5132 KB Output is correct
18 Correct 7 ms 5908 KB Output is correct
19 Correct 7 ms 6100 KB Output is correct
20 Correct 8 ms 6228 KB Output is correct
21 Correct 6 ms 5912 KB Output is correct
22 Correct 7 ms 5972 KB Output is correct
23 Correct 7 ms 5972 KB Output is correct
24 Correct 6 ms 5972 KB Output is correct
25 Correct 3 ms 5004 KB Output is correct
26 Correct 7 ms 6020 KB Output is correct
27 Correct 6 ms 6008 KB Output is correct
28 Correct 7 ms 5984 KB Output is correct
29 Correct 6 ms 5972 KB Output is correct
30 Correct 7 ms 5716 KB Output is correct
31 Correct 7 ms 5780 KB Output is correct
32 Correct 6 ms 5780 KB Output is correct
33 Correct 6 ms 5688 KB Output is correct
34 Correct 184 ms 40060 KB Output is correct
35 Correct 199 ms 46336 KB Output is correct
36 Correct 175 ms 46336 KB Output is correct
37 Correct 242 ms 45668 KB Output is correct
38 Correct 247 ms 40200 KB Output is correct
39 Correct 243 ms 44156 KB Output is correct
40 Correct 227 ms 42888 KB Output is correct
41 Correct 2 ms 5076 KB Output is correct
42 Correct 2 ms 5076 KB Output is correct
43 Correct 230 ms 33680 KB Output is correct
44 Correct 255 ms 33656 KB Output is correct
45 Correct 244 ms 33596 KB Output is correct
46 Correct 195 ms 42512 KB Output is correct
47 Correct 201 ms 42696 KB Output is correct
48 Correct 240 ms 42500 KB Output is correct
49 Correct 202 ms 42632 KB Output is correct
50 Correct 301 ms 33920 KB Output is correct
51 Correct 267 ms 33868 KB Output is correct
52 Correct 286 ms 33920 KB Output is correct
53 Correct 285 ms 33932 KB Output is correct